1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
30 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
40 * will alloc memory and clear it
42 void *clear_malloc(size_t size)
44 void *t = malloc(size);
47 memset(t, 0x00, size);
52 void *fill_malloc(size_t size)
54 void *t = malloc(size);
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);
67 #define IN_REPLACEMENTS_C
68 #include "replacements.h"
76 /* replacements for gettimeofday */
77 #ifndef HAVE_GETTIMEOFDAY
83 #define EPOCHFILETIME (116444736000000000i64)
85 #define EPOCHFILETIME (116444736000000000LL)
88 int gettimeofday(struct timeval *tv, struct timezone *tz)
97 GetSystemTimeAsFileTime(&ft);
98 li.LowPart = ft.dwLowDateTime;
99 li.HighPart = ft.dwHighDateTime;
100 t = li.QuadPart; /* In 100-nanosecond intervals */
101 t -= EPOCHFILETIME; /* Offset to the Epoch time */
102 t /= 10; /* In microseconds */
103 tv->tv_sec = (long)(t / 1000000);
104 tv->tv_usec = (long)(t % 1000000);
114 tz->tz_minuteswest = _timezone / 60;
115 tz->tz_dsttime = _daylight;
122 #endif /* HAVE_GETTIMEOFDAY */
125 size_t strnlen(const char *s, size_t maxlen)
127 const char *end= (const char *)memchr(s, '\0', maxlen);
128 return end ? (size_t) (end - s) : maxlen;
133 char* strndup(const char *s, size_t n)
135 size_t len = strnlen (s, n);
136 char *new = (char *) malloc (len + 1);
142 return (char *) memcpy (new, s, len);
147 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
149 DWORD ms_total, limit;
150 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
151 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
152 int n_handles = 0, i;
153 fd_set sock_read, sock_write, sock_except;
154 fd_set aread, awrite, aexcept;
155 int sock_max_fd = -1;
156 struct timeval tvslice;
159 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
161 /* calculate how long we need to wait in milliseconds */
165 ms_total = tv->tv_sec * 1000;
166 ms_total += tv->tv_usec / 1000;
170 FD_ZERO(&sock_write);
171 FD_ZERO(&sock_except);
173 /* build an array of handles for non-sockets */
174 for (i = 0; i < max_fd; i++) {
175 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
176 long handle = _get_osfhandle(i);
177 handles[n_handles] = (HANDLE)handle;
178 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
180 if (SAFE_FD_ISSET(i, rfds)) {
181 FD_SET(i, &sock_read);
183 if (SAFE_FD_ISSET(i, wfds)) {
184 FD_SET(i, &sock_write);
186 if (SAFE_FD_ISSET(i, efds)) {
187 FD_SET(i, &sock_except);
189 if (i > sock_max_fd) {
193 handle_slot_to_fd[n_handles] = i;
199 if (n_handles == 0) {
200 /* plain sockets only - let winsock handle the whole thing */
201 return select(max_fd, rfds, wfds, efds, tv);
204 /* mixture of handles and sockets; lets multiplex between
205 * winsock and waiting on the handles */
211 limit = GetTickCount() + ms_total;
215 if (sock_max_fd >= 0) {
216 /* overwrite the zero'd sets here; the select call
217 * will clear those that are not active */
220 aexcept = sock_except;
223 tvslice.tv_usec = 100000;
225 retcode = select(sock_max_fd+1, &aread, &awrite, &aexcept, &tvslice);
231 wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
233 if (wret == WAIT_TIMEOUT) {
234 /* set retcode to 0; this is the default.
235 * select() may have set it to something else,
236 * in which case we leave it alone, so this branch
239 } else if (wret == WAIT_FAILED) {
247 for (i = 0; i < n_handles; i++) {
248 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
249 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
251 long handle = _get_osfhandle(handle_slot_to_fd[i]);
253 if (PeekNamedPipe((HANDLE)handle, NULL, 0, NULL, &dwBytes, NULL))
255 /* check to see if gdb pipe has data available */
258 FD_SET(handle_slot_to_fd[i], &aread);
264 FD_SET(handle_slot_to_fd[i], &aread);
268 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
269 FD_SET(handle_slot_to_fd[i], &awrite);
272 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
273 FD_SET(handle_slot_to_fd[i], &aexcept);
280 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));