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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* Copyright 2010-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include <bx/os.h>
#include <bx/uint32_t.h>
#include <bx/string.h>
#if !BX_PLATFORM_NONE
#include <stdio.h>
#include <sys/stat.h>
#if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
# include <windows.h>
# include <psapi.h>
#elif BX_PLATFORM_ANDROID \
|| BX_PLATFORM_EMSCRIPTEN \
|| BX_PLATFORM_BSD \
|| BX_PLATFORM_HURD \
|| BX_PLATFORM_IOS \
|| BX_PLATFORM_LINUX \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_OSX \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_RPI \
|| BX_PLATFORM_STEAMLINK
# include <sched.h> // sched_yield
# if BX_PLATFORM_BSD \
|| BX_PLATFORM_IOS \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_OSX \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_STEAMLINK
# include <pthread.h> // mach_port_t
# endif // BX_PLATFORM_*
# include <time.h> // nanosleep
# if !BX_PLATFORM_PS4 && !BX_PLATFORM_NACL
# include <dlfcn.h> // dlopen, dlclose, dlsym
# endif // !BX_PLATFORM_PS4 && !BX_PLATFORM_NACL
# if BX_PLATFORM_ANDROID
# include <malloc.h> // mallinfo
# elif BX_PLATFORM_LINUX \
|| BX_PLATFORM_RPI \
|| BX_PLATFORM_STEAMLINK
# include <unistd.h> // syscall
# include <sys/syscall.h>
# elif BX_PLATFORM_OSX
# include <mach/mach.h> // mach_task_basic_info
# elif BX_PLATFORM_HURD
# include <pthread/pthread.h> // pthread_self
# elif BX_PLATFORM_ANDROID
# include "debug.h" // getTid is not implemented...
# endif // BX_PLATFORM_ANDROID
#endif // BX_PLATFORM_
#if BX_CRT_MSVC
# include <direct.h> // _getcwd
#else
# include <unistd.h> // getcwd
#endif // BX_CRT_MSVC
namespace bx
{
void sleep(uint32_t _ms)
{
#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
::Sleep(_ms);
#elif BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
BX_UNUSED(_ms);
debugOutput("sleep is not implemented"); debugBreak();
#else
timespec req = {(time_t)_ms/1000, (long)((_ms%1000)*1000000)};
timespec rem = {0, 0};
::nanosleep(&req, &rem);
#endif // BX_PLATFORM_
}
void yield()
{
#if BX_PLATFORM_WINDOWS
::SwitchToThread();
#elif BX_PLATFORM_XBOX360
::Sleep(0);
#elif BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
debugOutput("yield is not implemented"); debugBreak();
#else
::sched_yield();
#endif // BX_PLATFORM_
}
uint32_t getTid()
{
#if BX_PLATFORM_WINDOWS
return ::GetCurrentThreadId();
#elif BX_PLATFORM_LINUX || BX_PLATFORM_RPI || BX_PLATFORM_STEAMLINK
return (pid_t)::syscall(SYS_gettid);
#elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
return (mach_port_t)::pthread_mach_thread_np(pthread_self() );
#elif BX_PLATFORM_BSD || BX_PLATFORM_NACL
// Casting __nc_basic_thread_data*... need better way to do this.
return *(uint32_t*)::pthread_self();
#elif BX_PLATFORM_HURD
return (pthread_t)::pthread_self();
#else
//# pragma message "not implemented."
debugOutput("getTid is not implemented"); debugBreak();
return 0;
#endif //
}
size_t getProcessMemoryUsed()
{
#if BX_PLATFORM_ANDROID
struct mallinfo mi = mallinfo();
return mi.uordblks;
#elif BX_PLATFORM_LINUX || BX_PLATFORM_HURD
FILE* file = fopen("/proc/self/statm", "r");
if (NULL == file)
{
return 0;
}
long pages = 0;
int items = fscanf(file, "%*s%ld", &pages);
fclose(file);
return 1 == items
? pages * sysconf(_SC_PAGESIZE)
: 0
;
#elif BX_PLATFORM_OSX
# if defined(MACH_TASK_BASIC_INFO)
mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
int const result = task_info(mach_task_self()
, MACH_TASK_BASIC_INFO
, (task_info_t)&info
, &infoCount
);
# else // MACH_TASK_BASIC_INFO
task_basic_info info;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
int const result = task_info(mach_task_self()
, TASK_BASIC_INFO
, (task_info_t)&info
, &infoCount
);
# endif // MACH_TASK_BASIC_INFO
if (KERN_SUCCESS != result)
{
return 0;
}
return info.resident_size;
#elif BX_PLATFORM_WINDOWS
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(GetCurrentProcess()
, &pmc
, sizeof(pmc)
);
return pmc.WorkingSetSize;
#else
return 0;
#endif // BX_PLATFORM_*
}
void* dlopen(const char* _filePath)
{
#if BX_PLATFORM_WINDOWS
return (void*)::LoadLibraryA(_filePath);
#elif BX_PLATFORM_EMSCRIPTEN \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_filePath);
return NULL;
#else
return ::dlopen(_filePath, RTLD_LOCAL|RTLD_LAZY);
#endif // BX_PLATFORM_
}
void dlclose(void* _handle)
{
#if BX_PLATFORM_WINDOWS
::FreeLibrary( (HMODULE)_handle);
#elif BX_PLATFORM_EMSCRIPTEN \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_handle);
#else
::dlclose(_handle);
#endif // BX_PLATFORM_
}
void* dlsym(void* _handle, const char* _symbol)
{
#if BX_PLATFORM_WINDOWS
return (void*)::GetProcAddress( (HMODULE)_handle, _symbol);
#elif BX_PLATFORM_EMSCRIPTEN \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_handle, _symbol);
return NULL;
#else
return ::dlsym(_handle, _symbol);
#endif // BX_PLATFORM_
}
bool getenv(const char* _name, char* _out, uint32_t* _inOutSize)
{
#if BX_PLATFORM_WINDOWS
DWORD len = ::GetEnvironmentVariableA(_name, _out, *_inOutSize);
bool result = len != 0 && len < *_inOutSize;
*_inOutSize = len;
return result;
#elif BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_name, _out, _inOutSize);
return false;
#else
const char* ptr = ::getenv(_name);
uint32_t len = 0;
bool result = false;
if (NULL != ptr)
{
len = (uint32_t)strnlen(ptr);
result = len != 0 && len < *_inOutSize;
if (len < *_inOutSize)
{
strlncpy(_out, len, ptr);
}
}
*_inOutSize = len;
return result;
#endif // BX_PLATFORM_
}
void setenv(const char* _name, const char* _value)
{
#if BX_PLATFORM_WINDOWS
::SetEnvironmentVariableA(_name, _value);
#elif BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_name, _value);
#else
::setenv(_name, _value, 1);
#endif // BX_PLATFORM_
}
void unsetenv(const char* _name)
{
#if BX_PLATFORM_WINDOWS
::SetEnvironmentVariableA(_name, NULL);
#elif BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_name);
#else
::unsetenv(_name);
#endif // BX_PLATFORM_
}
int chdir(const char* _path)
{
#if BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_path);
return -1;
#elif BX_CRT_MSVC
return ::_chdir(_path);
#else
return ::chdir(_path);
#endif // BX_COMPILER_
}
char* pwd(char* _buffer, uint32_t _size)
{
#if BX_PLATFORM_PS4 \
|| BX_PLATFORM_XBOXONE \
|| BX_PLATFORM_WINRT
BX_UNUSED(_buffer, _size);
return NULL;
#elif BX_CRT_MSVC
return ::_getcwd(_buffer, (int)_size);
#else
return ::getcwd(_buffer, _size);
#endif // BX_COMPILER_
}
bool getTempPath(char* _out, uint32_t* _inOutSize)
{
#if BX_PLATFORM_WINDOWS
uint32_t len = ::GetTempPathA(*_inOutSize, _out);
bool result = len != 0 && len < *_inOutSize;
*_inOutSize = len;
return result;
#else
static const char* s_tmp[] =
{
"TMPDIR",
"TMP",
"TEMP",
"TEMPDIR",
NULL
};
for (const char** tmp = s_tmp; *tmp != NULL; ++tmp)
{
uint32_t len = *_inOutSize;
*_out = '\0';
bool result = getenv(*tmp, _out, &len);
if (result
&& len != 0
&& len < *_inOutSize)
{
*_inOutSize = len;
return result;
}
}
FileInfo fi;
if (stat("/tmp", fi)
&& FileInfo::Directory == fi.m_type)
{
strlncpy(_out, *_inOutSize, "/tmp");
*_inOutSize = 4;
return true;
}
return false;
#endif // BX_PLATFORM_*
}
bool stat(const char* _filePath, FileInfo& _fileInfo)
{
_fileInfo.m_size = 0;
_fileInfo.m_type = FileInfo::Count;
#if BX_COMPILER_MSVC
struct ::_stat64 st;
int32_t result = ::_stat64(_filePath, &st);
if (0 != result)
{
return false;
}
if (0 != (st.st_mode & _S_IFREG) )
{
_fileInfo.m_type = FileInfo::Regular;
}
else if (0 != (st.st_mode & _S_IFDIR) )
{
_fileInfo.m_type = FileInfo::Directory;
}
#else
struct ::stat st;
int32_t result = ::stat(_filePath, &st);
if (0 != result)
{
return false;
}
if (0 != (st.st_mode & S_IFREG) )
{
_fileInfo.m_type = FileInfo::Regular;
}
else if (0 != (st.st_mode & S_IFDIR) )
{
_fileInfo.m_type = FileInfo::Directory;
}
#endif // BX_COMPILER_MSVC
_fileInfo.m_size = st.st_size;
return true;
}
void* exec(const char* const* _argv)
{
#if BX_PLATFORM_LINUX || BX_PLATFORM_HURD
pid_t pid = fork();
if (0 == pid)
{
int result = execvp(_argv[0], const_cast<char *const*>(&_argv[1]) );
BX_UNUSED(result);
return NULL;
}
return (void*)uintptr_t(pid);
#elif BX_PLATFORM_WINDOWS
STARTUPINFO si;
memSet(&si, 0, sizeof(STARTUPINFO) );
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
memSet(&pi, 0, sizeof(PROCESS_INFORMATION) );
int32_t total = 0;
for (uint32_t ii = 0; NULL != _argv[ii]; ++ii)
{
total += (int32_t)strnlen(_argv[ii]) + 1;
}
char* temp = (char*)alloca(total);
int32_t len = 0;
for(uint32_t ii = 0; NULL != _argv[ii]; ++ii)
{
len += snprintf(&temp[len], bx::uint32_imax(0, total-len)
, "%s "
, _argv[ii]
);
}
bool ok = !!CreateProcessA(_argv[0]
, temp
, NULL
, NULL
, false
, 0
, NULL
, NULL
, &si
, &pi
);
if (ok)
{
return pi.hProcess;
}
return NULL;
#else
BX_UNUSED(_argv);
return NULL;
#endif // BX_PLATFORM_LINUX || BX_PLATFORM_HURD
}
} // namespace bx
#endif // !BX_PLATFORM_NONE
|