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