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 ***************************************************************************/
26 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
33 * will alloc memory and clear it
35 void *clear_malloc(size_t size
)
37 void *t
= malloc(size
);
39 memset(t
, 0x00, size
);
43 void *fill_malloc(size_t size
)
45 void *t
= malloc(size
);
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
);
57 #define IN_REPLACEMENTS_C
69 /* replacements for gettimeofday */
70 #ifndef HAVE_GETTIMEOFDAY
76 #define EPOCHFILETIME (116444736000000000i64)
78 #define EPOCHFILETIME (116444736000000000LL)
81 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
89 GetSystemTimeAsFileTime(&ft
);
90 li
.LowPart
= ft
.dwLowDateTime
;
91 li
.HighPart
= ft
.dwHighDateTime
;
92 t
= li
.QuadPart
; /* In 100-nanosecond
94 t
-= EPOCHFILETIME
; /* Offset to the Epoch time
96 t
/= 10; /* In microseconds
98 tv
->tv_sec
= (long)(t
/ 1000000);
99 tv
->tv_usec
= (long)(t
% 1000000);
107 tz
->tz_minuteswest
= _timezone
/ 60;
108 tz
->tz_dsttime
= _daylight
;
115 #endif /* HAVE_GETTIMEOFDAY */
118 size_t strnlen(const char *s
, size_t maxlen
)
120 const char *end
= (const char *)memchr(s
, '\0', maxlen
);
121 return end
? (size_t) (end
- s
) : maxlen
;
126 char *strndup(const char *s
, size_t n
)
128 size_t len
= strnlen(s
, n
);
129 char *new = (char *) malloc(len
+ 1);
135 return (char *) memcpy(new, s
, len
);
140 int win_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
, struct timeval
*tv
)
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
;
152 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
154 /* calculate how long we need to wait in milliseconds */
158 ms_total
= tv
->tv_sec
* 1000;
159 ms_total
+= tv
->tv_usec
/ 1000;
163 FD_ZERO(&sock_write
);
164 FD_ZERO(&sock_except
);
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
) {
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
);
182 handle_slot_to_fd
[n_handles
] = i
;
188 if (n_handles
== 0) {
189 /* plain sockets only - let winsock handle the whole thing */
190 return select(max_fd
, rfds
, wfds
, efds
, tv
);
193 /* mixture of handles and sockets; lets multiplex between
194 * winsock and waiting on the handles */
200 limit
= GetTickCount() + ms_total
;
204 if (sock_max_fd
>= 0) {
205 /* overwrite the zero'd sets here; the select call
206 * will clear those that are not active */
209 aexcept
= sock_except
;
212 tvslice
.tv_usec
= 100000;
214 retcode
= select(sock_max_fd
+ 1, &aread
, &awrite
, &aexcept
, &tvslice
);
220 wret
= MsgWaitForMultipleObjects(n_handles
,
223 retcode
> 0 ? 0 : 100,
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
232 } else if (wret
== WAIT_FAILED
) {
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
)) {
242 intptr_t handle
= (intptr_t) _get_osfhandle(
243 handle_slot_to_fd
[i
]);
245 if (PeekNamedPipe((HANDLE
)handle
, NULL
, 0,
246 NULL
, &dwBytes
, NULL
)) {
247 /* check to see if gdb pipe has data
250 FD_SET(handle_slot_to_fd
[i
],
255 FD_SET(handle_slot_to_fd
[i
],
260 if (SAFE_FD_ISSET(handle_slot_to_fd
[i
], wfds
)) {
261 FD_SET(handle_slot_to_fd
[i
], &awrite
);
264 if (SAFE_FD_ISSET(handle_slot_to_fd
[i
], efds
)) {
265 FD_SET(handle_slot_to_fd
[i
], &aexcept
);
272 } while (retcode
== 0 && (ms_total
== INFINITE
|| GetTickCount() < limit
));