- add gdb pipe support to native win32 (--pipe option)
[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                         handles[n_handles] = (HANDLE)_get_osfhandle(i);
176                         if (handles[n_handles] == INVALID_HANDLE_VALUE) {
177                                 /* socket */
178                                 if (SAFE_FD_ISSET(i, rfds)) {
179                                         FD_SET(i, &sock_read);
180                                 }
181                                 if (SAFE_FD_ISSET(i, wfds)) {
182                                         FD_SET(i, &sock_write);
183                                 }
184                                 if (SAFE_FD_ISSET(i, efds)) {
185                                         FD_SET(i, &sock_except);
186                                 }
187                                 if (i > sock_max_fd) {
188                                         sock_max_fd = i;
189                                 }
190                         } else {
191                                 handle_slot_to_fd[n_handles] = i;
192                                 n_handles++;
193                         }
194                 }
195         }
196
197         if (n_handles == 0) {
198                 /* plain sockets only - let winsock handle the whole thing */
199                 return select(max_fd, rfds, wfds, efds, tv);
200         }
201
202         /* mixture of handles and sockets; lets multiplex between
203          * winsock and waiting on the handles */
204
205         FD_ZERO(&aread);
206         FD_ZERO(&awrite);
207         FD_ZERO(&aexcept);
208         
209         limit = GetTickCount() + ms_total;
210         do {
211                 retcode = 0;
212         
213                 if (sock_max_fd >= 0) {
214                         /* overwrite the zero'd sets here; the select call
215                          * will clear those that are not active */
216                         aread = sock_read;
217                         awrite = sock_write;
218                         aexcept = sock_except;
219
220                         tvslice.tv_sec = 0;
221                         tvslice.tv_usec = 100000;
222
223                         retcode = select(sock_max_fd+1, &aread, &awrite, &aexcept, &tvslice);
224                 }
225                 if (n_handles > 0) {
226                         /* check handles */
227                         DWORD wret;
228
229                         wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
230
231                         if (wret == WAIT_TIMEOUT) {
232                                 /* set retcode to 0; this is the default.
233                                  * select() may have set it to something else,
234                                  * in which case we leave it alone, so this branch
235                                  * does nothing */
236                                 ;
237                         } else if (wret == WAIT_FAILED) {
238                                 if (retcode == 0) {
239                                         retcode = -1;
240                                 }
241                         } else {
242                                 if (retcode < 0) {
243                                         retcode = 0;
244                                 }
245                                 for (i = 0; i < n_handles; i++) {
246                                         if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
247                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
248                                                         DWORD dwBytes;
249                                                         
250                                                         if (PeekNamedPipe((HANDLE)_get_osfhandle(handle_slot_to_fd[i]), NULL, 0, NULL, &dwBytes, NULL))
251                                                         {
252                                                                 /* check to see if gdb pipe has data available */
253                                                                 if (dwBytes)
254                                                                 {
255                                                                         FD_SET(handle_slot_to_fd[i], &aread);
256                                                                         retcode++;
257                                                                 }
258                                                         }
259                                                         else
260                                                         {
261                                                                 FD_SET(handle_slot_to_fd[i], &aread);
262                                                                 retcode++;
263                                                         }
264                                                 }
265                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
266                                                         FD_SET(handle_slot_to_fd[i], &awrite);
267                                                         retcode++;
268                                                 }
269                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
270                                                         FD_SET(handle_slot_to_fd[i], &aexcept);
271                                                         retcode++;
272                                                 }
273                                         }
274                                 }
275                         }
276                 }
277         } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
278
279         if (rfds) {
280                 *rfds = aread;
281         }
282         if (wfds) {
283                 *wfds = awrite;
284         }
285         if (efds) {
286                 *efds = aexcept;
287         }
288
289         return retcode;
290 }
291 #endif