-
Notifications
You must be signed in to change notification settings - Fork 2
/
djl_os.hxx
325 lines (272 loc) · 7.96 KB
/
djl_os.hxx
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
319
320
321
322
323
/*
These are some utilities and abstractions for building on Windows and Linux
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <conio.h>
#include <direct.h>
#include <intrin.h>
#include <io.h>
#define not_inlined __declspec(noinline)
#define force_inlined __forceinline
inline void sleep_ms( uint64_t ms ) { SleepEx( (DWORD) ms, FALSE ); }
inline bool file_exists( char const * pfile )
{
uint32_t attr = GetFileAttributesA( pfile );
return ( ( INVALID_FILE_ATTRIBUTES != attr ) && ( ! ( FILE_ATTRIBUTE_DIRECTORY & attr ) ) );
} //file_exists
inline void bump_thread_priority() { SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST ); }
inline void set_process_affinity( uint64_t processAffinityMask )
{
SetProcessAffinityMask( (HANDLE) -1, processAffinityMask );
}
#elif defined( WATCOM )
#include <io.h>
#define MAX_PATH 255
#define not_inlined
#define force_inlined __inline
inline void sleep_ms( uint64_t ms ) {}
inline bool file_exists( char const * pfile )
{
FILE * fp = fopen( pfile, "r" );
bool exists = false;
if ( fp )
{
fclose( fp );
exists = true;
}
return exists;
} //file_exists
inline void bump_thread_priority() {}
inline void set_process_affinity( uint64_t processAffinityMask ) {}
inline int getpid() { return 0; }
#define _countof( X ) ( sizeof( X ) / sizeof( X[0] ) )
inline void swap( uint8_t & a, uint8_t & b ) { uint8_t c = a; a = b; b = c; }
#else // Linux, MacOS, etc.
#ifndef OLDGCC
#include <termios.h>
#endif
#include <thread>
#include <sched.h>
#include <unistd.h>
#include <ctype.h>
#define not_inlined __attribute__ ((noinline))
#define force_inlined inline
inline void bump_thread_priority() {}
inline void set_process_affinity( uint64_t processAffinityMask )
{
#if !defined(__APPLE__) && !defined( OLDGCC )
cpu_set_t mask;
CPU_ZERO( &mask );
for ( long l = 0; l < 32; l++ )
{
int b = ( 1 << l );
if ( 0 != ( b & processAffinityMask ) )
CPU_SET( l, &mask );
}
// this does nothing on WSL 1 or 2 except make you believe it might work until you actually check
int status = sched_setaffinity( 0, sizeof( mask ), &mask );
#endif
} //set_process_affinity
template < typename T, size_t N > size_t _countof( T ( & arr )[ N ] ) { return std::extent< T[ N ] >::value; }
#define _stricmp strcasecmp
#define MAX_PATH 1024
inline char * strupr( char * s )
{
for ( char * t = s; *t; t++ )
*t = toupper( *t );
return s;
} //strupr
inline char * _strupr( char * s ) { return strupr( s ); }
inline char * strlwr( char * s )
{
for ( char * t = s; *t; t++ )
*t = tolower( *t );
return s;
} //strlwr
inline uint64_t _abs64( int64_t x ) { return ( x > 0 ) ? x : -x; }
inline char * _strlwr( char * s ) { return strlwr( s ); }
inline void sleep_ms( uint64_t ms )
{
uint64_t total_ns = ms * 1000000;
long ns = (long) ( total_ns % 1000000000 );
long sec = (long) ( total_ns / 1000000000 );
struct timespec ts = { sec, ns };
#ifndef OLDGCC
nanosleep( &ts, 0 );
#endif
} //sleep_ms
inline bool file_exists( char const * pfile )
{
FILE * fp = fopen( pfile, "r" );
bool exists = false;
if ( fp )
{
fclose( fp );
exists = true;
}
return exists;
} //file_exists
#endif
template <class T> inline T get_max( T a, T b )
{
if ( a > b )
return a;
return b;
} //get_max
template <class T> inline T get_min( T a, T b )
{
if ( a < b )
return a;
return b;
} //get_min
template <class T> inline T round_up( T x, T multiple )
{
if ( 0 == multiple )
return x;
T remainder = x % multiple;
if ( 0 == remainder )
return x;
return x + multiple - remainder;
} //round_up
inline const char * target_platform()
{
#if defined( __riscv ) // g++ on linux
return "riscv";
#elif defined( __amd64 ) // g++ on linux
return "amd64";
#elif defined( __aarch64__ ) // g++ on linux
return "arm64";
#elif defined( _M_AMD64 ) // msft on Windows
return "amd64";
#elif defined( _M_ARM64 ) // msft on Windows
return "arm64";
#elif defined( WATCOM ) // WATCOM for 8086
return "8086";
#elif defined( _M_IX86 ) // msft on Windows 32-bit
return "x86";
#elif defined( __ARM_32BIT_STATE ) // ARM32 on Raspberry PI (and more)
return "arm32";
#else
return "(other)";
#endif
} //target_platform
inline const char * build_type()
{
#ifdef NDEBUG
return "release";
#else
return "debug";
#endif
} //build_type
inline const char * compiler_used()
{
static char acver[ 100 ];
#if defined( __GNUC__ )
return "g++";
#elif defined( _MSC_VER )
snprintf( acver, sizeof( acver ), "msft C++ ver %u", _MSC_VER );
return acver;
#elif defined( __clang__ )
return "clang";
#elif defined( WATCOM )
return "watcom";
#else
return "unknown";
#endif
} //compiler_used
inline const char * build_platform()
{
#if defined( __APPLE__ )
return "apple";
#elif defined( __linux )
return "linux";
#elif defined( _WIN32 )
return "windows";
#elif defined( WATCOM )
return "windows";
#else
return "unknown";
#endif
} //build_platform
inline const char * build_string()
{
static char bs[ 320 ];
snprintf( bs, sizeof( bs ), "Built for %s %s on %c%c %c%c%c %s %s by %s on %s\n",
target_platform(), build_type(), __DATE__[4], __DATE__[5],
__DATE__[0], __DATE__[1], __DATE__[2], &__DATE__[7], __TIME__, compiler_used(), build_platform() );
return bs;
} //build_string
#if defined( __GNUC__ ) || defined( __clang__ )
#define assume_false return( 0 ) // clearly terrible, but this code will never execute. ever.
#define assume_false_return return // clearly terrible, but this code will never execute. ever.
#elif defined( WATCOM )
#define assume_false return( 0 ) // clearly terrible, but this code will never execute. ever.
#define __assume( x )
#else
#define assume_false __assume( false )
#define assume_false_return __assume( false )
#endif
inline long portable_filelen( int descriptor )
{
#ifdef _WIN32
long current = _lseek( descriptor, 0, SEEK_CUR );
long len = _lseek( descriptor, 0, SEEK_END );
_lseek( descriptor, current, SEEK_SET );
#else
long current = lseek( descriptor, 0, SEEK_CUR );
long len = lseek( descriptor, 0, SEEK_END );
lseek( descriptor, current, SEEK_SET );
#endif
return len;
} //portable_filelen
inline long portable_filelen( FILE * fp )
{
long current = ftell( fp );
fseek( fp, 0, SEEK_END );
long len = ftell( fp );
fseek( fp, current, SEEK_SET );
return len;
} //portable_filelen
inline long portable_filelen( const char * p )
{
FILE * fp = fopen( p, "r" );
if ( 0 != fp )
{
long len = portable_filelen( fp );
fclose( fp );
return len;
}
return 0;
} //portable_filelen
class CFile
{
private:
FILE * fp;
public:
CFile( FILE * file ) : fp( file ) {}
~CFile() { close(); }
FILE * get() { return fp; }
void close()
{
if ( NULL != fp )
{
fclose( fp );
fp = NULL;
}
}
};
inline char printable( uint8_t x )
{
if ( x < ' ' || x >= 127 )
return ' ';
return x;
} //printable