You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

292 lines
7.5 KiB

  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. /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #ifdef HAVE_STRINGS_H
  33. #include <strings.h>
  34. #endif
  35. /*
  36. * clear_malloc
  37. *
  38. * will alloc memory and clear it
  39. */
  40. void *clear_malloc(size_t size)
  41. {
  42. void *t = malloc(size);
  43. if (t!=NULL)
  44. {
  45. memset(t, 0x00, size);
  46. }
  47. return t;
  48. }
  49. void *fill_malloc(size_t size)
  50. {
  51. void *t = malloc(size);
  52. if (t!=NULL)
  53. {
  54. /* We want to initialize memory to some known bad state. */
  55. /* 0 and 0xff yields 0 and -1 as integers, which often */
  56. /* have meaningful values. 0x5555... is not often a valid */
  57. /* integer and is quite easily spotted in the debugger */
  58. /* also it is almost certainly an invalid address */
  59. memset(t, 0x55, size);
  60. }
  61. return t;
  62. }
  63. #include "replacements.h"
  64. #include <stdio.h>
  65. #ifdef _WIN32
  66. #include <io.h>
  67. #endif
  68. /* replacements for gettimeofday */
  69. #ifndef HAVE_GETTIMEOFDAY
  70. /* Windows */
  71. #ifdef _WIN32
  72. #ifndef __GNUC__
  73. #define EPOCHFILETIME (116444736000000000i64)
  74. #else
  75. #define EPOCHFILETIME (116444736000000000LL)
  76. #endif
  77. int gettimeofday(struct timeval *tv, struct timezone *tz)
  78. {
  79. FILETIME ft;
  80. LARGE_INTEGER li;
  81. __int64 t;
  82. static int tzflag;
  83. if (tv)
  84. {
  85. GetSystemTimeAsFileTime(&ft);
  86. li.LowPart = ft.dwLowDateTime;
  87. li.HighPart = ft.dwHighDateTime;
  88. t = li.QuadPart; /* In 100-nanosecond intervals */
  89. t -= EPOCHFILETIME; /* Offset to the Epoch time */
  90. t /= 10; /* In microseconds */
  91. tv->tv_sec = (long)(t / 1000000);
  92. tv->tv_usec = (long)(t % 1000000);
  93. }
  94. if (tz)
  95. {
  96. if (!tzflag)
  97. {
  98. _tzset();
  99. tzflag++;
  100. }
  101. tz->tz_minuteswest = _timezone / 60;
  102. tz->tz_dsttime = _daylight;
  103. }
  104. return 0;
  105. }
  106. #endif /* _WIN32 */
  107. #endif /* HAVE_GETTIMEOFDAY */
  108. #ifndef HAVE_STRNLEN
  109. size_t strnlen(const char *s, size_t maxlen)
  110. {
  111. const char *end= (const char *)memchr(s, '\0', maxlen);
  112. return end ? (size_t) (end - s) : maxlen;
  113. }
  114. #endif
  115. #ifndef HAVE_STRNDUP
  116. char* strndup(const char *s, size_t n)
  117. {
  118. size_t len = strnlen (s, n);
  119. char *new = (char *) malloc (len + 1);
  120. if (new == NULL)
  121. return NULL;
  122. new[len] = '\0';
  123. return (char *) memcpy (new, s, len);
  124. }
  125. #endif
  126. #ifdef _WIN32
  127. int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
  128. {
  129. DWORD ms_total, limit;
  130. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  131. int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
  132. int n_handles = 0, i;
  133. fd_set sock_read, sock_write, sock_except;
  134. fd_set aread, awrite, aexcept;
  135. int sock_max_fd = -1;
  136. struct timeval tvslice;
  137. int retcode;
  138. #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
  139. /* calculate how long we need to wait in milliseconds */
  140. if (tv == NULL) {
  141. ms_total = INFINITE;
  142. } else {
  143. ms_total = tv->tv_sec * 1000;
  144. ms_total += tv->tv_usec / 1000;
  145. }
  146. FD_ZERO(&sock_read);
  147. FD_ZERO(&sock_write);
  148. FD_ZERO(&sock_except);
  149. /* build an array of handles for non-sockets */
  150. for (i = 0; i < max_fd; i++) {
  151. if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
  152. handles[n_handles] = (HANDLE)_get_osfhandle(i);
  153. if (handles[n_handles] == INVALID_HANDLE_VALUE) {
  154. /* socket */
  155. if (SAFE_FD_ISSET(i, rfds)) {
  156. FD_SET(i, &sock_read);
  157. }
  158. if (SAFE_FD_ISSET(i, wfds)) {
  159. FD_SET(i, &sock_write);
  160. }
  161. if (SAFE_FD_ISSET(i, efds)) {
  162. FD_SET(i, &sock_except);
  163. }
  164. if (i > sock_max_fd) {
  165. sock_max_fd = i;
  166. }
  167. } else {
  168. handle_slot_to_fd[n_handles] = i;
  169. n_handles++;
  170. }
  171. }
  172. }
  173. if (n_handles == 0) {
  174. /* plain sockets only - let winsock handle the whole thing */
  175. return select(max_fd, rfds, wfds, efds, tv);
  176. }
  177. /* mixture of handles and sockets; lets multiplex between
  178. * winsock and waiting on the handles */
  179. FD_ZERO(&aread);
  180. FD_ZERO(&awrite);
  181. FD_ZERO(&aexcept);
  182. limit = GetTickCount() + ms_total;
  183. do {
  184. retcode = 0;
  185. if (sock_max_fd >= 0) {
  186. /* overwrite the zero'd sets here; the select call
  187. * will clear those that are not active */
  188. aread = sock_read;
  189. awrite = sock_write;
  190. aexcept = sock_except;
  191. tvslice.tv_sec = 0;
  192. tvslice.tv_usec = 100000;
  193. retcode = select(sock_max_fd+1, &aread, &awrite, &aexcept, &tvslice);
  194. }
  195. if (n_handles > 0) {
  196. /* check handles */
  197. DWORD wret;
  198. wret = MsgWaitForMultipleObjects(n_handles, handles, FALSE, retcode > 0 ? 0 : 100, QS_ALLEVENTS);
  199. if (wret == WAIT_TIMEOUT) {
  200. /* set retcode to 0; this is the default.
  201. * select() may have set it to something else,
  202. * in which case we leave it alone, so this branch
  203. * does nothing */
  204. ;
  205. } else if (wret == WAIT_FAILED) {
  206. if (retcode == 0) {
  207. retcode = -1;
  208. }
  209. } else {
  210. if (retcode < 0) {
  211. retcode = 0;
  212. }
  213. for (i = 0; i < n_handles; i++) {
  214. if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
  215. if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
  216. DWORD dwBytes;
  217. if (PeekNamedPipe((HANDLE)_get_osfhandle(handle_slot_to_fd[i]), NULL, 0, NULL, &dwBytes, NULL))
  218. {
  219. /* check to see if gdb pipe has data available */
  220. if (dwBytes)
  221. {
  222. FD_SET(handle_slot_to_fd[i], &aread);
  223. retcode++;
  224. }
  225. }
  226. else
  227. {
  228. FD_SET(handle_slot_to_fd[i], &aread);
  229. retcode++;
  230. }
  231. }
  232. if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
  233. FD_SET(handle_slot_to_fd[i], &awrite);
  234. retcode++;
  235. }
  236. if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
  237. FD_SET(handle_slot_to_fd[i], &aexcept);
  238. retcode++;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
  245. if (rfds) {
  246. *rfds = aread;
  247. }
  248. if (wfds) {
  249. *wfds = awrite;
  250. }
  251. if (efds) {
  252. *efds = aexcept;
  253. }
  254. return retcode;
  255. }
  256. #endif