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 ***************************************************************************/
18 /* define IN_REPLACEMENTS_C before include replacements.h */
19 #define IN_REPLACEMENTS_C
20 #include "replacements.h"
28 * will alloc memory and clear it
30 void *clear_malloc(size_t size
)
32 void *t
= malloc(size
);
34 memset(t
, 0x00, size
);
38 void *fill_malloc(size_t size
)
40 void *t
= malloc(size
);
42 /* We want to initialize memory to some known bad state.
43 * 0 and 0xff yields 0 and -1 as integers, which often
44 * have meaningful values. 0x5555... is not often a valid
45 * integer and is quite easily spotted in the debugger
46 * also it is almost certainly an invalid address */
47 memset(t
, 0x55, size
);
61 /* replacements for gettimeofday */
62 #ifndef HAVE_GETTIMEOFDAY
68 #define EPOCHFILETIME (116444736000000000i64)
70 #define EPOCHFILETIME (116444736000000000LL)
73 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
81 GetSystemTimeAsFileTime(&ft
);
82 li
.LowPart
= ft
.dwLowDateTime
;
83 li
.HighPart
= ft
.dwHighDateTime
;
84 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
85 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
86 t
/= 10; /* In microseconds */
87 tv
->tv_sec
= (long)(t
/ 1000000);
88 tv
->tv_usec
= (long)(t
% 1000000);
96 tz
->tz_minuteswest
= _timezone
/ 60;
97 tz
->tz_dsttime
= _daylight
;
104 #endif /* HAVE_GETTIMEOFDAY */
107 size_t strnlen(const char *s
, size_t maxlen
)
109 const char *end
= (const char *)memchr(s
, '\0', maxlen
);
110 return end
? (size_t) (end
- s
) : maxlen
;
115 char *strndup(const char *s
, size_t n
)
117 size_t len
= strnlen(s
, n
);
118 char *new = malloc(len
+ 1);
124 return (char *) memcpy(new, s
, len
);
129 int win_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*efds
, struct timeval
*tv
)
131 DWORD ms_total
, limit
;
132 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
133 int handle_slot_to_fd
[MAXIMUM_WAIT_OBJECTS
];
134 int n_handles
= 0, i
;
135 fd_set sock_read
, sock_write
, sock_except
;
136 fd_set aread
, awrite
, aexcept
;
137 int sock_max_fd
= -1;
138 struct timeval tvslice
;
141 #define SAFE_FD_ISSET(fd, set) (set && FD_ISSET(fd, set))
143 /* calculate how long we need to wait in milliseconds */
147 ms_total
= tv
->tv_sec
* 1000;
148 ms_total
+= tv
->tv_usec
/ 1000;
152 FD_ZERO(&sock_write
);
153 FD_ZERO(&sock_except
);
155 /* build an array of handles for non-sockets */
156 for (i
= 0; i
< max_fd
; i
++) {
157 if (SAFE_FD_ISSET(i
, rfds
) || SAFE_FD_ISSET(i
, wfds
) || SAFE_FD_ISSET(i
, efds
)) {
158 intptr_t handle
= (intptr_t) _get_osfhandle(i
);
159 handles
[n_handles
] = (HANDLE
)handle
;
160 if (handles
[n_handles
] == INVALID_HANDLE_VALUE
) {
162 if (SAFE_FD_ISSET(i
, rfds
))
163 FD_SET(i
, &sock_read
);
164 if (SAFE_FD_ISSET(i
, wfds
))
165 FD_SET(i
, &sock_write
);
166 if (SAFE_FD_ISSET(i
, efds
))
167 FD_SET(i
, &sock_except
);
171 handle_slot_to_fd
[n_handles
] = i
;
177 if (n_handles
== 0) {
178 /* plain sockets only - let winsock handle the whole thing */
179 return select(max_fd
, rfds
, wfds
, efds
, tv
);
182 /* mixture of handles and sockets; lets multiplex between
183 * winsock and waiting on the handles */
189 limit
= GetTickCount() + ms_total
;
193 if (sock_max_fd
>= 0) {
194 /* overwrite the zero'd sets here; the select call
195 * will clear those that are not active */
198 aexcept
= sock_except
;
201 tvslice
.tv_usec
= 1000;
203 retcode
= select(sock_max_fd
+ 1, &aread
, &awrite
, &aexcept
, &tvslice
);
210 wret
= MsgWaitForMultipleObjects(n_handles
,
216 if (wret
== WAIT_TIMEOUT
) {
217 /* set retcode to 0; this is the default.
218 * select() may have set it to something else,
219 * in which case we leave it alone, so this branch
222 } else if (wret
== WAIT_FAILED
) {
228 for (i
= 0; i
< n_handles
; i
++) {
229 if (WaitForSingleObject(handles
[i
], 0) == WAIT_OBJECT_0
) {
230 if (SAFE_FD_ISSET(handle_slot_to_fd
[i
], rfds
)) {
232 intptr_t handle
= (intptr_t) _get_osfhandle(
233 handle_slot_to_fd
[i
]);
235 if (PeekNamedPipe((HANDLE
)handle
, NULL
, 0,
236 NULL
, &bytes
, NULL
)) {
237 /* check to see if gdb pipe has data available */
239 FD_SET(handle_slot_to_fd
[i
], &aread
);
243 FD_SET(handle_slot_to_fd
[i
], &aread
);
247 if (SAFE_FD_ISSET(handle_slot_to_fd
[i
], wfds
)) {
248 FD_SET(handle_slot_to_fd
[i
], &awrite
);
251 if (SAFE_FD_ISSET(handle_slot_to_fd
[i
], efds
)) {
252 FD_SET(handle_slot_to_fd
[i
], &aexcept
);
259 } while (retcode
== 0 && (ms_total
== INFINITE
|| GetTickCount() < limit
));