binarybuffer: fix whitespace related issues
[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 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
27
28 #include <stdlib.h>
29 #include <string.h>
30 /*
31 * clear_malloc
32 *
33 * will alloc memory and clear it
34 */
35 void *clear_malloc(size_t size)
36 {
37 void *t = malloc(size);
38 if (t != NULL)
39 {
40 memset(t, 0x00, size);
41 }
42 return t;
43 }
44
45 void *fill_malloc(size_t size)
46 {
47 void *t = malloc(size);
48 if (t != NULL)
49 {
50 /* We want to initialize memory to some known bad state. */
51 /* 0 and 0xff yields 0 and -1 as integers, which often */
52 /* have meaningful values. 0x5555... is not often a valid */
53 /* integer and is quite easily spotted in the debugger */
54 /* also it is almost certainly an invalid address */
55 memset(t, 0x55, size);
56 }
57 return t;
58 }
59
60 #define IN_REPLACEMENTS_C
61 #ifdef HAVE_CONFIG_H
62 #include "config.h"
63 #endif
64 #ifdef HAVE_STRINGS_H
65 #include <strings.h>
66 #endif
67
68 #ifdef _WIN32
69 #include <io.h>
70 #endif
71
72 /* replacements for gettimeofday */
73 #ifndef HAVE_GETTIMEOFDAY
74
75 /* Windows */
76 #ifdef _WIN32
77
78 #ifndef __GNUC__
79 #define EPOCHFILETIME (116444736000000000i64)
80 #else
81 #define EPOCHFILETIME (116444736000000000LL)
82 #endif
83
84 int gettimeofday(struct timeval *tv, struct timezone *tz)
85 {
86 FILETIME ft;
87 LARGE_INTEGER li;
88 __int64 t;
89 static int tzflag;
90
91 if (tv)
92 {
93 GetSystemTimeAsFileTime(&ft);
94 li.LowPart = ft.dwLowDateTime;
95 li.HighPart = ft.dwHighDateTime;
96 t = li.QuadPart; /* In 100-nanosecond intervals */
97 t -= EPOCHFILETIME; /* Offset to the Epoch time */
98 t /= 10; /* In microseconds */
99 tv->tv_sec = (long)(t / 1000000);
100 tv->tv_usec = (long)(t % 1000000);
101 }
102
103 if (tz)
104 {
105 if (!tzflag)
106 {
107 _tzset();
108 tzflag++;
109 }
110 tz->tz_minuteswest = _timezone / 60;
111 tz->tz_dsttime = _daylight;
112 }
113
114 return 0;
115 }
116 #endif /* _WIN32 */
117
118 #endif /* HAVE_GETTIMEOFDAY */
119
120 #ifndef HAVE_STRNLEN
121 size_t strnlen(const char *s, size_t maxlen)
122 {
123 const char *end= (const char *)memchr(s, '\0', maxlen);
124 return end ? (size_t) (end - s) : maxlen;
125 }
126 #endif
127
128 #ifndef HAVE_STRNDUP
129 char* strndup(const char *s, size_t n)
130 {
131 size_t len = strnlen (s, n);
132 char *new = (char *) malloc (len + 1);
133
134 if (new == NULL)
135 return NULL;
136
137 new[len] = '\0';
138 return (char *) memcpy (new, s, len);
139 }
140 #endif
141
142 #ifdef _WIN32
143 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
144 {
145 DWORD ms_total, limit;
146 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
147 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
148 int n_handles = 0, i;
149 fd_set sock_read, sock_write, sock_except;
150 fd_set aread, awrite, aexcept;
151 int sock_max_fd = -1;
152 struct timeval tvslice;
153 int retcode;
154
155 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
156
157 /* calculate how long we need to wait in milliseconds */
158 if (tv == NULL) {
159 ms_total = INFINITE;
160 } else {
161 ms_total = tv->tv_sec * 1000;
162 ms_total += tv->tv_usec / 1000;
163 }
164
165 FD_ZERO(&sock_read);
166 FD_ZERO(&sock_write);
167 FD_ZERO(&sock_except);
168
169 /* build an array of handles for non-sockets */
170 for (i = 0; i < max_fd; i++) {
171 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
172 intptr_t handle = (intptr_t) _get_osfhandle(i);
173 handles[n_handles] = (HANDLE)handle;
174 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
175 /* socket */
176 if (SAFE_FD_ISSET(i, rfds)) {
177 FD_SET(i, &sock_read);
178 }
179 if (SAFE_FD_ISSET(i, wfds)) {
180 FD_SET(i, &sock_write);
181 }
182 if (SAFE_FD_ISSET(i, efds)) {
183 FD_SET(i, &sock_except);
184 }
185 if (i > sock_max_fd) {
186 sock_max_fd = i;
187 }
188 } else {
189 handle_slot_to_fd[n_handles] = i;
190 n_handles++;
191 }
192 }
193 }
194
195 if (n_handles == 0) {
196 /* plain sockets only - let winsock handle the whole thing */
197 return select(max_fd, rfds, wfds, efds, tv);
198 }
199
200 /* mixture of handles and sockets; lets multiplex between
201 * winsock and waiting on the handles */
202
203 FD_ZERO(&aread);
204 FD_ZERO(&awrite);
205 FD_ZERO(&aexcept);
206
207 limit = GetTickCount() + ms_total;
208 do {
209 retcode = 0;
210
211 if (sock_max_fd >= 0) {
212 /* overwrite the zero'd sets here; the select call
213 * will clear those that are not active */
214 aread = sock_read;
215 awrite = sock_write;
216 aexcept = sock_except;
217
218 tvslice.tv_sec = 0;
219 tvslice.tv_usec = 100000;
220
221 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
222 }
223 if (n_handles > 0) {
224 /* check handles */
225 DWORD wret;
226
227 wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
228
229 if (wret == WAIT_TIMEOUT) {
230 /* set retcode to 0; this is the default.
231 * select() may have set it to something else,
232 * in which case we leave it alone, so this branch
233 * does nothing */
234 ;
235 } else if (wret == WAIT_FAILED) {
236 if (retcode == 0) {
237 retcode = -1;
238 }
239 } else {
240 if (retcode < 0) {
241 retcode = 0;
242 }
243 for (i = 0; i < n_handles; i++) {
244 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
245 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
246 DWORD dwBytes;
247 intptr_t handle = (intptr_t) _get_osfhandle(handle_slot_to_fd[i]);
248
249 if (PeekNamedPipe((HANDLE)handle, NULL, 0, NULL, &dwBytes, NULL))
250 {
251 /* check to see if gdb pipe has data available */
252 if (dwBytes)
253 {
254 FD_SET(handle_slot_to_fd[i], &aread);
255 retcode++;
256 }
257 }
258 else
259 {
260 FD_SET(handle_slot_to_fd[i], &aread);
261 retcode++;
262 }
263 }
264 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
265 FD_SET(handle_slot_to_fd[i], &awrite);
266 retcode++;
267 }
268 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
269 FD_SET(handle_slot_to_fd[i], &aexcept);
270 retcode++;
271 }
272 }
273 }
274 }
275 }
276 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
277
278 if (rfds) {
279 *rfds = aread;
280 }
281 if (wfds) {
282 *wfds = awrite;
283 }
284 if (efds) {
285 *efds = aexcept;
286 }
287
288 return retcode;
289 }
290 #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)