Finish portability support for AC_HEADER_STDBOOL configure macro.
[openocd.git] / src / helper / replacements.c
1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
31
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37 /*
38 * clear_malloc
39 *
40 * will alloc memory and clear it
41 */
42 void *clear_malloc(size_t size)
43 {
44 void *t = malloc(size);
45 if (t!=NULL)
46 {
47 memset(t, 0x00, size);
48 }
49 return t;
50 }
51
52 void *fill_malloc(size_t size)
53 {
54 void *t = malloc(size);
55 if (t!=NULL)
56 {
57 /* We want to initialize memory to some known bad state. */
58 /* 0 and 0xff yields 0 and -1 as integers, which often */
59 /* have meaningful values. 0x5555... is not often a valid */
60 /* integer and is quite easily spotted in the debugger */
61 /* also it is almost certainly an invalid address */
62 memset(t, 0x55, size);
63 }
64 return t;
65 }
66
67 #define IN_REPLACEMENTS_C
68 #include "replacements.h"
69
70 #include <stdio.h>
71
72 #ifdef _WIN32
73 #include <io.h>
74 #endif
75
76 /* replacements for gettimeofday */
77 #ifndef HAVE_GETTIMEOFDAY
78
79 /* Windows */
80 #ifdef _WIN32
81
82 #ifndef __GNUC__
83 #define EPOCHFILETIME (116444736000000000i64)
84 #else
85 #define EPOCHFILETIME (116444736000000000LL)
86 #endif
87
88 int gettimeofday(struct timeval *tv, struct timezone *tz)
89 {
90 FILETIME ft;
91 LARGE_INTEGER li;
92 __int64 t;
93 static int tzflag;
94
95 if (tv)
96 {
97 GetSystemTimeAsFileTime(&ft);
98 li.LowPart = ft.dwLowDateTime;
99 li.HighPart = ft.dwHighDateTime;
100 t = li.QuadPart; /* In 100-nanosecond intervals */
101 t -= EPOCHFILETIME; /* Offset to the Epoch time */
102 t /= 10; /* In microseconds */
103 tv->tv_sec = (long)(t / 1000000);
104 tv->tv_usec = (long)(t % 1000000);
105 }
106
107 if (tz)
108 {
109 if (!tzflag)
110 {
111 _tzset();
112 tzflag++;
113 }
114 tz->tz_minuteswest = _timezone / 60;
115 tz->tz_dsttime = _daylight;
116 }
117
118 return 0;
119 }
120 #endif /* _WIN32 */
121
122 #endif /* HAVE_GETTIMEOFDAY */
123
124 #ifndef HAVE_STRNLEN
125 size_t strnlen(const char *s, size_t maxlen)
126 {
127 const char *end= (const char *)memchr(s, '\0', maxlen);
128 return end ? (size_t) (end - s) : maxlen;
129 }
130 #endif
131
132 #ifndef HAVE_STRNDUP
133 char* strndup(const char *s, size_t n)
134 {
135 size_t len = strnlen (s, n);
136 char *new = (char *) malloc (len + 1);
137
138 if (new == NULL)
139 return NULL;
140
141 new[len] = '\0';
142 return (char *) memcpy (new, s, len);
143 }
144 #endif
145
146 #ifdef _WIN32
147 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
148 {
149 DWORD ms_total, limit;
150 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
151 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
152 int n_handles = 0, i;
153 fd_set sock_read, sock_write, sock_except;
154 fd_set aread, awrite, aexcept;
155 int sock_max_fd = -1;
156 struct timeval tvslice;
157 int retcode;
158
159 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
160
161 /* calculate how long we need to wait in milliseconds */
162 if (tv == NULL) {
163 ms_total = INFINITE;
164 } else {
165 ms_total = tv->tv_sec * 1000;
166 ms_total += tv->tv_usec / 1000;
167 }
168
169 FD_ZERO(&sock_read);
170 FD_ZERO(&sock_write);
171 FD_ZERO(&sock_except);
172
173 /* build an array of handles for non-sockets */
174 for (i = 0; i < max_fd; i++) {
175 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
176 long handle = _get_osfhandle(i);
177 handles[n_handles] = (HANDLE)handle;
178 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
179 /* socket */
180 if (SAFE_FD_ISSET(i, rfds)) {
181 FD_SET(i, &sock_read);
182 }
183 if (SAFE_FD_ISSET(i, wfds)) {
184 FD_SET(i, &sock_write);
185 }
186 if (SAFE_FD_ISSET(i, efds)) {
187 FD_SET(i, &sock_except);
188 }
189 if (i > sock_max_fd) {
190 sock_max_fd = i;
191 }
192 } else {
193 handle_slot_to_fd[n_handles] = i;
194 n_handles++;
195 }
196 }
197 }
198
199 if (n_handles == 0) {
200 /* plain sockets only - let winsock handle the whole thing */
201 return select(max_fd, rfds, wfds, efds, tv);
202 }
203
204 /* mixture of handles and sockets; lets multiplex between
205 * winsock and waiting on the handles */
206
207 FD_ZERO(&aread);
208 FD_ZERO(&awrite);
209 FD_ZERO(&aexcept);
210
211 limit = GetTickCount() + ms_total;
212 do {
213 retcode = 0;
214
215 if (sock_max_fd >= 0) {
216 /* overwrite the zero'd sets here; the select call
217 * will clear those that are not active */
218 aread = sock_read;
219 awrite = sock_write;
220 aexcept = sock_except;
221
222 tvslice.tv_sec = 0;
223 tvslice.tv_usec = 100000;
224
225 retcode = select(sock_max_fd+1, &aread, &awrite, &aexcept, &tvslice);
226 }
227 if (n_handles > 0) {
228 /* check handles */
229 DWORD wret;
230
231 wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
232
233 if (wret == WAIT_TIMEOUT) {
234 /* set retcode to 0; this is the default.
235 * select() may have set it to something else,
236 * in which case we leave it alone, so this branch
237 * does nothing */
238 ;
239 } else if (wret == WAIT_FAILED) {
240 if (retcode == 0) {
241 retcode = -1;
242 }
243 } else {
244 if (retcode < 0) {
245 retcode = 0;
246 }
247 for (i = 0; i < n_handles; i++) {
248 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
249 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
250 DWORD dwBytes;
251 long handle = _get_osfhandle(handle_slot_to_fd[i]);
252
253 if (PeekNamedPipe((HANDLE)handle, NULL, 0, NULL, &dwBytes, NULL))
254 {
255 /* check to see if gdb pipe has data available */
256 if (dwBytes)
257 {
258 FD_SET(handle_slot_to_fd[i], &aread);
259 retcode++;
260 }
261 }
262 else
263 {
264 FD_SET(handle_slot_to_fd[i], &aread);
265 retcode++;
266 }
267 }
268 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
269 FD_SET(handle_slot_to_fd[i], &awrite);
270 retcode++;
271 }
272 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
273 FD_SET(handle_slot_to_fd[i], &aexcept);
274 retcode++;
275 }
276 }
277 }
278 }
279 }
280 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
281
282 if (rfds) {
283 *rfds = aread;
284 }
285 if (wfds) {
286 *wfds = awrite;
287 }
288 if (efds) {
289 *efds = aexcept;
290 }
291
292 return retcode;
293 }
294 #endif

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)