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