1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2006 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
12 ***************************************************************************/
13 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
20 * will alloc memory and clear it
22 void *clear_malloc(size_t size)
24 void *t = malloc(size);
26 memset(t, 0x00, size);
30 void *fill_malloc(size_t size)
32 void *t = malloc(size);
34 /* We want to initialize memory to some known bad state.
35 * 0 and 0xff yields 0 and -1 as integers, which often
36 * have meaningful values. 0x5555... is not often a valid
37 * integer and is quite easily spotted in the debugger
38 * also it is almost certainly an invalid address */
39 memset(t, 0x55, size);
44 #define IN_REPLACEMENTS_C
57 /* replacements for gettimeofday */
58 #ifndef HAVE_GETTIMEOFDAY
64 #define EPOCHFILETIME (116444736000000000i64)
66 #define EPOCHFILETIME (116444736000000000LL)
69 int gettimeofday(struct timeval *tv, struct timezone *tz)
77 GetSystemTimeAsFileTime(&ft);
78 li.LowPart = ft.dwLowDateTime;
79 li.HighPart = ft.dwHighDateTime;
80 t = li.QuadPart; /* In 100-nanosecond intervals */
81 t -= EPOCHFILETIME; /* Offset to the Epoch time */
82 t /= 10; /* In microseconds */
83 tv->tv_sec = (long)(t / 1000000);
84 tv->tv_usec = (long)(t % 1000000);
92 tz->tz_minuteswest = _timezone / 60;
93 tz->tz_dsttime = _daylight;
100 #endif /* HAVE_GETTIMEOFDAY */
103 size_t strnlen(const char *s, size_t maxlen)
105 const char *end = (const char *)memchr(s, '\0', maxlen);
106 return end ? (size_t) (end - s) : maxlen;
111 char *strndup(const char *s, size_t n)
113 size_t len = strnlen(s, n);
114 char *new = malloc(len + 1);
120 return (char *) memcpy(new, s, len);
125 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
127 DWORD ms_total, limit;
128 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
129 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
130 int n_handles = 0, i;
131 fd_set sock_read, sock_write, sock_except;
132 fd_set aread, awrite, aexcept;
133 int sock_max_fd = -1;
134 struct timeval tvslice;
137 #define SAFE_FD_ISSET(fd, set) (set && FD_ISSET(fd, set))
139 /* calculate how long we need to wait in milliseconds */
143 ms_total = tv->tv_sec * 1000;
144 ms_total += tv->tv_usec / 1000;
148 FD_ZERO(&sock_write);
149 FD_ZERO(&sock_except);
151 /* build an array of handles for non-sockets */
152 for (i = 0; i < max_fd; i++) {
153 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
154 intptr_t handle = (intptr_t) _get_osfhandle(i);
155 handles[n_handles] = (HANDLE)handle;
156 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
158 if (SAFE_FD_ISSET(i, rfds))
159 FD_SET(i, &sock_read);
160 if (SAFE_FD_ISSET(i, wfds))
161 FD_SET(i, &sock_write);
162 if (SAFE_FD_ISSET(i, efds))
163 FD_SET(i, &sock_except);
167 handle_slot_to_fd[n_handles] = i;
173 if (n_handles == 0) {
174 /* plain sockets only - let winsock handle the whole thing */
175 return select(max_fd, rfds, wfds, efds, tv);
178 /* mixture of handles and sockets; lets multiplex between
179 * winsock and waiting on the handles */
185 limit = GetTickCount() + ms_total;
189 if (sock_max_fd >= 0) {
190 /* overwrite the zero'd sets here; the select call
191 * will clear those that are not active */
194 aexcept = sock_except;
197 tvslice.tv_usec = 1000;
199 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
206 wret = MsgWaitForMultipleObjects(n_handles,
212 if (wret == WAIT_TIMEOUT) {
213 /* set retcode to 0; this is the default.
214 * select() may have set it to something else,
215 * in which case we leave it alone, so this branch
218 } else if (wret == WAIT_FAILED) {
224 for (i = 0; i < n_handles; i++) {
225 if (WaitForSingleObject(handles[i], 0) == WAIT_OBJECT_0) {
226 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
228 intptr_t handle = (intptr_t) _get_osfhandle(
229 handle_slot_to_fd[i]);
231 if (PeekNamedPipe((HANDLE)handle, NULL, 0,
232 NULL, &bytes, NULL)) {
233 /* check to see if gdb pipe has data available */
235 FD_SET(handle_slot_to_fd[i], &aread);
239 FD_SET(handle_slot_to_fd[i], &aread);
243 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
244 FD_SET(handle_slot_to_fd[i], &awrite);
247 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
248 FD_SET(handle_slot_to_fd[i], &aexcept);
255 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));