-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwin32fixes.h
318 lines (250 loc) · 7.88 KB
/
win32fixes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Modified by Henry Rawas (henryr@schakra.com)
* - make it compatible with Visual Studio builds
* - added wstrtod to handle INF, NAN
* - added support for using IOCP with sockets
*/
#ifndef WIN32FIXES_H
#define WIN32FIXES_H
#ifdef WIN32
#ifndef _WIN32
#define _WIN32
#endif
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOGDI
#define __USE_W32_SOCKETS
#include "fmacros.h"
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <signal.h>
#include <sys/types.h>
#ifndef FD_SETSIZE
#define FD_SETSIZE 16000
#endif
#include <winsock2.h> /* setsocketopt */
#include <ws2tcpip.h>
#include <windows.h>
#include <float.h>
#include <fcntl.h> /* _O_BINARY */
#include <limits.h> /* INT_MAX */
#include <process.h>
#include <sys/types.h>
#define fseeko fseeko64
#define ftello ftello64
#define inline __inline
#undef ftruncate
#define ftruncate replace_ftruncate
#ifndef off64_t
#define off64_t off_t
#endif
int replace_ftruncate(int fd, off64_t length);
#define snprintf _snprintf
#define ftello64 _ftelli64
#define fseeko64 _fseeki64
#define strcasecmp _stricmp
#define strtoll _strtoi64
#define isnan _isnan
#define isfinite _finite
#define isinf(x) (!_finite(x))
#define lseek64 lseek
/* following defined to choose little endian byte order */
#define __i386__ 1
#if !defined(va_copy)
#define va_copy(d,s) d = (s)
#endif
#define sleep(x) Sleep((x)*1000)
#ifndef __RTL_GENRANDOM
#define __RTL_GENRANDOM 1
typedef BOOLEAN (_stdcall* RtlGenRandomFunc)(void * RandomBuffer, ULONG RandomBufferLength);
#endif
RtlGenRandomFunc RtlGenRandom;
#define random() (long)replace_random()
#define rand() replace_random()
int replace_random();
#if !defined(ssize_t)
typedef int ssize_t;
#endif
#if !defined(mode_t)
#define mode_t long
#endif
#if !defined(u_int32_t)
/* sha1 */
typedef unsigned __int32 u_int32_t;
#endif
/* Redis calls usleep(1) to give thread some time
* Sleep(0) should do the same on windows
* In other cases, usleep is called with milisec resolution,
* which can be directly translated to winapi Sleep() */
#undef usleep
#define usleep(x) (x == 1) ? Sleep(0) : Sleep((int)((x)/1000))
#define pipe(fds) _pipe(fds, 8192, _O_BINARY|_O_NOINHERIT)
/* Processes */
#define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
#define WAIT_T int
#define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */
#define WCOREDUMP(x) 0
#define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */
#define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */
#define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */
#define WIFSTOPPED(x) 0
#define WNOHANG 1
/* file mapping */
#define PROT_READ 1
#define PROT_WRITE 2
#define MAP_FAILED (void *) -1
#define MAP_SHARED 1
#define MAP_PRIVATE 2
/* rusage */
#define RUSAGE_SELF 0
#define RUSAGE_CHILDREN (-1)
#ifndef _RUSAGE_T_
#define _RUSAGE_T_
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
};
#endif
int getrusage(int who, struct rusage * rusage);
/* Signals */
#define SIGNULL 0 /* Null Check access to pid*/
#define SIGHUP 1 /* Hangup Terminate; can be trapped*/
#define SIGINT 2 /* Interrupt Terminate; can be trapped */
#define SIGQUIT 3 /* Quit Terminate with core dump; can be trapped */
#define SIGTRAP 5
#define SIGBUS 7
#define SIGKILL 9 /* Kill Forced termination; cannot be trapped */
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15 /* Terminate Terminate; can be trapped */
#define SIGSTOP 17
#define SIGTSTP 18
#define SIGCONT 19
#define SIGCHLD 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGABRT 22
/* #define SIGSTOP 24 /*Pause the process; cannot be trapped*/
/* #define SIGTSTP 25 /*Terminal stop Pause the process; can be trapped*/
/* #define SIGCONT 26 */
#define SIGWINCH 28
#define SIGUSR1 30
#define SIGUSR2 31
#define ucontext_t void*
#define SA_NOCLDSTOP 0x00000001u
#define SA_NOCLDWAIT 0x00000002u
#define SA_SIGINFO 0x00000004u
#define SA_ONSTACK 0x08000000u
#define SA_RESTART 0x10000000u
#define SA_NODEFER 0x40000000u
#define SA_RESETHAND 0x80000000u
#define SA_NOMASK SA_NODEFER
#define SA_ONESHOT SA_RESETHAND
#define SA_RESTORER 0x04000000
#define sigemptyset(pset) (*(pset) = 0)
#define sigfillset(pset) (*(pset) = (unsigned int)-1)
#define sigaddset(pset, num) (*(pset) |= (1L<<(num)))
#define sigdelset(pset, num) (*(pset) &= ~(1L<<(num)))
#define sigismember(pset, num) (*(pset) & (1L<<(num)))
#ifndef SIG_SETMASK
#define SIG_SETMASK (0)
#define SIG_BLOCK (1)
#define SIG_UNBLOCK (2)
#endif /*SIG_SETMASK*/
typedef void (*__p_sig_fn_t)(int);
typedef int pid_t;
#ifndef _SIGSET_T_
#define _SIGSET_T_
#ifdef _WIN64
typedef unsigned long long _sigset_t;
#else
typedef unsigned long _sigset_t;
#endif
# define sigset_t _sigset_t
#endif /* _SIGSET_T_ */
struct sigaction {
int sa_flags;
sigset_t sa_mask;
__p_sig_fn_t sa_handler;
__p_sig_fn_t sa_sigaction;
};
int sigaction(int sig, struct sigaction *in, struct sigaction *out);
/* Sockets */
#ifndef ECONNRESET
#define ECONNRESET WSAECONNRESET
#endif
#ifndef EINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
#endif
#ifndef ETIMEDOUT
#define ETIMEDOUT WSAETIMEDOUT
#endif
#define setsockopt(a,b,c,d,e) replace_setsockopt(a,b,c,d,e)
int replace_setsockopt(int socket, int level, int optname,
const void *optval, socklen_t optlen);
#define rename(a,b) replace_rename(a,b)
int replace_rename(const char *src, const char *dest);
//threads avoiding pthread.h
#define pthread_mutex_t CRITICAL_SECTION
#define pthread_attr_t ssize_t
#define pthread_mutex_init(a,b) (InitializeCriticalSectionAndSpinCount((a), 0x80000400),0)
#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
#define pthread_mutex_lock EnterCriticalSection
#define pthread_mutex_unlock LeaveCriticalSection
#define pthread_equal(t1, t2) ((t1) == (t2))
#define pthread_attr_init(x) (*(x) = 0)
#define pthread_attr_getstacksize(x, y) (*(y) = *(x))
#define pthread_attr_setstacksize(x, y) (*(x) = y)
#define pthread_t u_int
int pthread_create(pthread_t *thread, const void *unused,
void *(*start_routine)(void*), void *arg);
pthread_t pthread_self(void);
typedef struct {
CRITICAL_SECTION waiters_lock;
LONG waiters;
int was_broadcast;
HANDLE sema;
HANDLE continue_broadcast;
} pthread_cond_t;
int pthread_cond_init(pthread_cond_t *cond, const void *unused);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_detach (pthread_t thread);
/* Misc Unix -> Win32 */
int kill(pid_t pid, int sig);
int fsync (int fd);
pid_t wait3(int *stat_loc, int options, void *rusage);
int w32initWinSock(void);
/* int inet_aton(const char *cp_arg, struct in_addr *addr) */
/* redis-check-dump */
void *mmap(void *start, size_t length, int prot, int flags, int fd, off offset);
int munmap(void *start, size_t length);
int fork(void);
int gettimeofday(struct timeval *tv, struct timezone *tz);
/* strtod does not handle Inf and Nan
We need to do the check before calling strtod */
#undef strtod
#define strtod(nptr, eptr) wstrtod((nptr), (eptr))
double wstrtod(const char *nptr, char **eptr);
/* structs and functions for using IOCP with windows sockets */
/* need callback on write complete. aeWinSendReq is used to pass parameters */
typedef struct aeWinSendReq {
void *client;
void *data;
char *buf;
int len;
} aeWinSendReq;
int aeWinSocketAttach(int fd);
int aeWinSocketDetach(int fd, int shutd);
int aeWinReceiveDone(int fd);
int aeWinSocketSend(int fd, char *buf, int len, int flags,
void *eventLoop, void *client, void *data, void *proc);
int aeWinListen(SOCKET sock, int backlog);
int aeWinAccept(int fd, struct sockaddr *sa, socklen_t *len);
int strerror_r(int err, char* buf, size_t buflen);
char *wsa_strerror(int err);
#endif /* WIN32 */
#endif /* WIN32FIXES_H */