target/arm_adi_v5: fix sync CSW cache on apreg write
[openocd.git] / src / helper / replacements.c
blobb4bb94f06e43398e99771ea3ec05a4bf786d30e8
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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
24 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
26 #include <stdlib.h>
27 #include <string.h>
29 * clear_malloc
31 * will alloc memory and clear it
33 void *clear_malloc(size_t size)
35 void *t = malloc(size);
36 if (t != NULL)
37 memset(t, 0x00, size);
38 return t;
41 void *fill_malloc(size_t size)
43 void *t = malloc(size);
44 if (t != NULL) {
45 /* We want to initialize memory to some known bad state.
46 * 0 and 0xff yields 0 and -1 as integers, which often
47 * have meaningful values. 0x5555... is not often a valid
48 * integer and is quite easily spotted in the debugger
49 * also it is almost certainly an invalid address */
50 memset(t, 0x55, size);
52 return t;
55 #define IN_REPLACEMENTS_C
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 #ifdef HAVE_STRINGS_H
60 #include <strings.h>
61 #endif
63 #ifdef _WIN32
64 #include <io.h>
65 #endif
67 /* replacements for gettimeofday */
68 #ifndef HAVE_GETTIMEOFDAY
70 /* Windows */
71 #ifdef _WIN32
73 #ifndef __GNUC__
74 #define EPOCHFILETIME (116444736000000000i64)
75 #else
76 #define EPOCHFILETIME (116444736000000000LL)
77 #endif
79 int gettimeofday(struct timeval *tv, struct timezone *tz)
81 FILETIME ft;
82 LARGE_INTEGER li;
83 __int64 t;
84 static int tzflag;
86 if (tv) {
87 GetSystemTimeAsFileTime(&ft);
88 li.LowPart = ft.dwLowDateTime;
89 li.HighPart = ft.dwHighDateTime;
90 t = li.QuadPart; /* In 100-nanosecond intervals */
91 t -= EPOCHFILETIME; /* Offset to the Epoch time */
92 t /= 10; /* In microseconds */
93 tv->tv_sec = (long)(t / 1000000);
94 tv->tv_usec = (long)(t % 1000000);
97 if (tz) {
98 if (!tzflag) {
99 _tzset();
100 tzflag++;
102 tz->tz_minuteswest = _timezone / 60;
103 tz->tz_dsttime = _daylight;
106 return 0;
108 #endif /* _WIN32 */
110 #endif /* HAVE_GETTIMEOFDAY */
112 #ifndef HAVE_STRNLEN
113 size_t strnlen(const char *s, size_t maxlen)
115 const char *end = (const char *)memchr(s, '\0', maxlen);
116 return end ? (size_t) (end - s) : maxlen;
118 #endif
120 #ifndef HAVE_STRNDUP
121 char *strndup(const char *s, size_t n)
123 size_t len = strnlen(s, n);
124 char *new = malloc(len + 1);
126 if (new == NULL)
127 return NULL;
129 new[len] = '\0';
130 return (char *) memcpy(new, s, len);
132 #endif
134 #ifdef _WIN32
135 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
137 DWORD ms_total, limit;
138 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
139 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
140 int n_handles = 0, i;
141 fd_set sock_read, sock_write, sock_except;
142 fd_set aread, awrite, aexcept;
143 int sock_max_fd = -1;
144 struct timeval tvslice;
145 int retcode;
147 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
149 /* calculate how long we need to wait in milliseconds */
150 if (tv == NULL)
151 ms_total = INFINITE;
152 else {
153 ms_total = tv->tv_sec * 1000;
154 ms_total += tv->tv_usec / 1000;
157 FD_ZERO(&sock_read);
158 FD_ZERO(&sock_write);
159 FD_ZERO(&sock_except);
161 /* build an array of handles for non-sockets */
162 for (i = 0; i < max_fd; i++) {
163 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
164 intptr_t handle = (intptr_t) _get_osfhandle(i);
165 handles[n_handles] = (HANDLE)handle;
166 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
167 /* socket */
168 if (SAFE_FD_ISSET(i, rfds))
169 FD_SET(i, &sock_read);
170 if (SAFE_FD_ISSET(i, wfds))
171 FD_SET(i, &sock_write);
172 if (SAFE_FD_ISSET(i, efds))
173 FD_SET(i, &sock_except);
174 if (i > sock_max_fd)
175 sock_max_fd = i;
176 } else {
177 handle_slot_to_fd[n_handles] = i;
178 n_handles++;
183 if (n_handles == 0) {
184 /* plain sockets only - let winsock handle the whole thing */
185 return select(max_fd, rfds, wfds, efds, tv);
188 /* mixture of handles and sockets; lets multiplex between
189 * winsock and waiting on the handles */
191 FD_ZERO(&aread);
192 FD_ZERO(&awrite);
193 FD_ZERO(&aexcept);
195 limit = GetTickCount() + ms_total;
196 do {
197 retcode = 0;
199 if (sock_max_fd >= 0) {
200 /* overwrite the zero'd sets here; the select call
201 * will clear those that are not active */
202 aread = sock_read;
203 awrite = sock_write;
204 aexcept = sock_except;
206 tvslice.tv_sec = 0;
207 tvslice.tv_usec = 1000;
209 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
212 if (n_handles > 0) {
213 /* check handles */
214 DWORD wret;
216 wret = MsgWaitForMultipleObjects(n_handles,
217 handles,
218 FALSE,
219 retcode > 0 ? 0 : 1,
220 QS_ALLEVENTS);
222 if (wret == WAIT_TIMEOUT) {
223 /* set retcode to 0; this is the default.
224 * select() may have set it to something else,
225 * in which case we leave it alone, so this branch
226 * does nothing */
228 } else if (wret == WAIT_FAILED) {
229 if (retcode == 0)
230 retcode = -1;
231 } else {
232 if (retcode < 0)
233 retcode = 0;
234 for (i = 0; i < n_handles; i++) {
235 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
236 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
237 DWORD dwBytes;
238 intptr_t handle = (intptr_t) _get_osfhandle(
239 handle_slot_to_fd[i]);
241 if (PeekNamedPipe((HANDLE)handle, NULL, 0,
242 NULL, &dwBytes, NULL)) {
243 /* check to see if gdb pipe has data available */
244 if (dwBytes) {
245 FD_SET(handle_slot_to_fd[i], &aread);
246 retcode++;
248 } else {
249 FD_SET(handle_slot_to_fd[i], &aread);
250 retcode++;
253 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
254 FD_SET(handle_slot_to_fd[i], &awrite);
255 retcode++;
257 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
258 FD_SET(handle_slot_to_fd[i], &aexcept);
259 retcode++;
265 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
267 if (rfds)
268 *rfds = aread;
269 if (wfds)
270 *wfds = awrite;
271 if (efds)
272 *efds = aexcept;
274 return retcode;
276 #endif
278 #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
279 #include <libusb.h>
280 /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
281 * The libusb_error enum is compatible down to v0.9.1
283 const char *libusb_error_name(int error_code)
285 enum libusb_error error = error_code;
286 switch (error) {
287 case LIBUSB_SUCCESS:
288 return "LIBUSB_SUCCESS";
289 case LIBUSB_ERROR_IO:
290 return "LIBUSB_ERROR_IO";
291 case LIBUSB_ERROR_INVALID_PARAM:
292 return "LIBUSB_ERROR_INVALID_PARAM";
293 case LIBUSB_ERROR_ACCESS:
294 return "LIBUSB_ERROR_ACCESS";
295 case LIBUSB_ERROR_NO_DEVICE:
296 return "LIBUSB_ERROR_NO_DEVICE";
297 case LIBUSB_ERROR_NOT_FOUND:
298 return "LIBUSB_ERROR_NOT_FOUND";
299 case LIBUSB_ERROR_BUSY:
300 return "LIBUSB_ERROR_BUSY";
301 case LIBUSB_ERROR_TIMEOUT:
302 return "LIBUSB_ERROR_TIMEOUT";
303 case LIBUSB_ERROR_OVERFLOW:
304 return "LIBUSB_ERROR_OVERFLOW";
305 case LIBUSB_ERROR_PIPE:
306 return "LIBUSB_ERROR_PIPE";
307 case LIBUSB_ERROR_INTERRUPTED:
308 return "LIBUSB_ERROR_INTERRUPTED";
309 case LIBUSB_ERROR_NO_MEM:
310 return "LIBUSB_ERROR_NO_MEM";
311 case LIBUSB_ERROR_NOT_SUPPORTED:
312 return "LIBUSB_ERROR_NOT_SUPPORTED";
313 case LIBUSB_ERROR_OTHER:
314 return "LIBUSB_ERROR_OTHER";
316 return "**UNKNOWN**";
318 #endif