]> git.sur5r.net Git - openocd/blob - src/helper/replacements.c
Remove FSF address from GPL notices
[openocd] / 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, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
25
26 #include <stdlib.h>
27 #include <string.h>
28 /*
29  * clear_malloc
30  *
31  * will alloc memory and clear it
32  */
33 void *clear_malloc(size_t size)
34 {
35         void *t = malloc(size);
36         if (t != NULL)
37                 memset(t, 0x00, size);
38         return t;
39 }
40
41 void *fill_malloc(size_t size)
42 {
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);
51         }
52         return t;
53 }
54
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
62
63 #ifdef _WIN32
64 #include <io.h>
65 #endif
66
67 /* replacements for gettimeofday */
68 #ifndef HAVE_GETTIMEOFDAY
69
70 /* Windows */
71 #ifdef _WIN32
72
73 #ifndef __GNUC__
74 #define EPOCHFILETIME (116444736000000000i64)
75 #else
76 #define EPOCHFILETIME (116444736000000000LL)
77 #endif
78
79 int gettimeofday(struct timeval *tv, struct timezone *tz)
80 {
81         FILETIME ft;
82         LARGE_INTEGER li;
83         __int64 t;
84         static int tzflag;
85
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);
95         }
96
97         if (tz) {
98                 if (!tzflag) {
99                         _tzset();
100                         tzflag++;
101                 }
102                 tz->tz_minuteswest = _timezone / 60;
103                 tz->tz_dsttime = _daylight;
104         }
105
106         return 0;
107 }
108 #endif  /* _WIN32 */
109
110 #endif  /* HAVE_GETTIMEOFDAY */
111
112 #ifndef HAVE_STRNLEN
113 size_t strnlen(const char *s, size_t maxlen)
114 {
115         const char *end = (const char *)memchr(s, '\0', maxlen);
116         return end ? (size_t) (end - s) : maxlen;
117 }
118 #endif
119
120 #ifndef HAVE_STRNDUP
121 char *strndup(const char *s, size_t n)
122 {
123         size_t len = strnlen(s, n);
124         char *new = malloc(len + 1);
125
126         if (new == NULL)
127                 return NULL;
128
129         new[len] = '\0';
130         return (char *) memcpy(new, s, len);
131 }
132 #endif
133
134 #ifdef _WIN32
135 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
136 {
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;
146
147 #define SAFE_FD_ISSET(fd, set)  (set != NULL && FD_ISSET(fd, set))
148
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;
155         }
156
157         FD_ZERO(&sock_read);
158         FD_ZERO(&sock_write);
159         FD_ZERO(&sock_except);
160
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++;
179                         }
180                 }
181         }
182
183         if (n_handles == 0) {
184                 /* plain sockets only - let winsock handle the whole thing */
185                 return select(max_fd, rfds, wfds, efds, tv);
186         }
187
188         /* mixture of handles and sockets; lets multiplex between
189          * winsock and waiting on the handles */
190
191         FD_ZERO(&aread);
192         FD_ZERO(&awrite);
193         FD_ZERO(&aexcept);
194
195         limit = GetTickCount() + ms_total;
196         do {
197                 retcode = 0;
198
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;
205
206                         tvslice.tv_sec = 0;
207                         tvslice.tv_usec = 1000;
208
209                         retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
210                 }
211
212                 if (n_handles > 0) {
213                         /* check handles */
214                         DWORD wret;
215
216                         wret = MsgWaitForMultipleObjects(n_handles,
217                                         handles,
218                                         FALSE,
219                                         retcode > 0 ? 0 : 1,
220                                         QS_ALLEVENTS);
221
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 */
227                                 ;
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]);
240
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++;
247                                                                 }
248                                                         } else {
249                                                                 FD_SET(handle_slot_to_fd[i], &aread);
250                                                                 retcode++;
251                                                         }
252                                                 }
253                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
254                                                         FD_SET(handle_slot_to_fd[i], &awrite);
255                                                         retcode++;
256                                                 }
257                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
258                                                         FD_SET(handle_slot_to_fd[i], &aexcept);
259                                                         retcode++;
260                                                 }
261                                         }
262                                 }
263                         }
264                 }
265         } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
266
267         if (rfds)
268                 *rfds = aread;
269         if (wfds)
270                 *wfds = awrite;
271         if (efds)
272                 *efds = aexcept;
273
274         return retcode;
275 }
276 #endif
277
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
282  */
283 const char *libusb_error_name(int error_code)
284 {
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";
315         }
316         return "**UNKNOWN**";
317 }
318 #endif