-
Notifications
You must be signed in to change notification settings - Fork 818
/
compat.h
213 lines (178 loc) · 6.42 KB
/
compat.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
/*
* Copyright 2012-2013 Luke Dashjr
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#ifndef BFG_COMPAT_H
#define BFG_COMPAT_H
#include "config.h"
#include <stdbool.h>
#if !(defined(WIN32) || defined(unix))
#define unix
#endif
#if defined(LL_FOREACH) && !defined(LL_FOREACH2)
// Missing from uthash before 1.9.7
#define LL_DELETE2(head,del,next) \
do { \
LDECLTYPE(head) _tmp; \
if ((head) == (del)) { \
(head)=(head)->next; \
} else { \
_tmp = head; \
while (_tmp->next && (_tmp->next != (del))) { \
_tmp = _tmp->next; \
} \
if (_tmp->next) { \
_tmp->next = ((del)->next); \
} \
} \
} while (0)
#define LL_FOREACH2(head,el,next) \
for(el=head;el;el=(el)->next)
#define LL_FOREACH_SAFE2(head,el,tmp,next) \
for((el)=(head);(el) && (tmp = (el)->next, 1); (el) = tmp)
#define LL_PREPEND2(head,add,next) \
do { \
(add)->next = head; \
head = add; \
} while (0)
#endif
#ifdef WIN32
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <windows.h>
#ifndef __maybe_unused
#define __maybe_unused __attribute__((unused))
#endif
#ifndef timersub
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000; \
} \
} while (0)
#endif
#ifndef timeradd
# define timeradd(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
if ((result)->tv_usec >= 1000000) \
{ \
++(result)->tv_sec; \
(result)->tv_usec -= 1000000; \
} \
} while (0)
#endif
// Some versions of MingW define this, but don't handle the timeval.tv_sec case that we use
#ifdef localtime_r
#undef localtime_r
#endif
// localtime is thread-safe on Windows
// We also use this with timeval.tv_sec, which is incorrectly smaller than time_t on Windows
// Need to cast to time_t* to suppress warning - actual problem shouldn't be possible in practice
#define localtime_r(timep, result) ( \
memcpy(result, \
( \
(sizeof(*timep) == sizeof(time_t)) \
? localtime((time_t*)timep) \
: localtime_convert(*timep) \
), \
sizeof(*result) \
) \
)
static inline
struct tm *localtime_convert(time_t t)
{
return localtime(&t);
}
#endif
#ifndef HAVE_NANOSLEEP
extern void (*timer_set_now)(struct timeval *);
#define cgtime(tvp) timer_set_now(tvp)
static inline int nanosleep(const struct timespec *req, struct timespec *rem)
{
struct timeval tstart;
DWORD msecs;
cgtime(&tstart);
msecs = (req->tv_sec * 1000) + ((999999 + req->tv_nsec) / 1000000);
if (SleepEx(msecs, true) == WAIT_IO_COMPLETION) {
if (rem) {
struct timeval tdone, tnow, tleft;
tdone.tv_sec = tstart.tv_sec + req->tv_sec;
tdone.tv_usec = tstart.tv_usec + ((999 + req->tv_nsec) / 1000);
if (tdone.tv_usec > 1000000) {
tdone.tv_usec -= 1000000;
++tdone.tv_sec;
}
cgtime(&tnow);
if (timercmp(&tnow, &tdone, >))
return 0;
timersub(&tdone, &tnow, &tleft);
rem->tv_sec = tleft.tv_sec;
rem->tv_nsec = tleft.tv_usec * 1000;
}
errno = EINTR;
return -1;
}
return 0;
}
#undef cgtime
#endif
#ifndef HAVE_SLEEP
static inline int sleep(unsigned int secs)
{
struct timespec req, rem;
req.tv_sec = secs;
req.tv_nsec = 0;
if (!nanosleep(&req, &rem))
return 0;
return rem.tv_sec + (rem.tv_nsec ? 1 : 0);
}
#endif
#ifdef WIN32
enum {
PRIO_PROCESS = 0,
};
static inline int setpriority(__maybe_unused int which, __maybe_unused int who, __maybe_unused int prio)
{
return -!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
}
typedef unsigned long int ulong;
typedef unsigned short int ushort;
typedef unsigned int uint;
#ifndef __SUSECONDS_T_TYPE
typedef long suseconds_t;
#endif
#endif /* WIN32 */
#ifndef HAVE_LOG2
# define log2(n) (log(n) / log(2))
#endif
#ifndef HAVE_PTHREAD_CANCEL
// Bionic (Android) is intentionally missing pthread_cancel, so it is implemented using pthread_kill (handled in util.c)
#include <pthread.h>
#include <signal.h>
#define pthread_cancel(pth) pthread_kill(pth, SIGTERM)
extern void pthread_testcancel(void);
#ifndef PTHREAD_CANCEL_ENABLE
#define PTHREAD_CANCEL_ENABLE 0
#define PTHREAD_CANCEL_DISABLE 1
#endif
#ifndef PTHREAD_CANCEL_DEFERRED
#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 1
#endif
#ifndef PTHREAD_CANCELED
#define PTHREAD_CANCELED ((void*)-1)
#endif
#endif
#endif /* __COMPAT_H__ */