-
-
Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathos_api.c
More file actions
550 lines (461 loc) · 12.9 KB
/
os_api.c
File metadata and controls
550 lines (461 loc) · 12.9 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**
* @file os_api.c
* @brief Operating system abstraction API.
*
* The OS API implements an overridable interface for implementing functions
* that are operating system specific, in addition to a number of hooks which
* allow for customization by the user, like logging.
*/
#include "private_api.h"
#include <ctype.h>
#include <time.h>
void ecs_os_api_impl(ecs_os_api_t *api);
static bool ecs_os_api_initialized = false;
static bool ecs_os_api_initializing = false;
static int ecs_os_api_init_count = 0;
ecs_os_api_t ecs_os_api = {
.flags_ = EcsOsApiHighResolutionTimer | EcsOsApiLogWithColors,
.log_level_ = -1 /* Disable tracing by default, but log warnings/errors */
};
int64_t ecs_os_api_malloc_count = 0;
int64_t ecs_os_api_realloc_count = 0;
int64_t ecs_os_api_calloc_count = 0;
int64_t ecs_os_api_free_count = 0;
void ecs_os_set_api(
ecs_os_api_t *os_api)
{
if (!ecs_os_api_initialized) {
ecs_os_api = *os_api;
ecs_os_api_initialized = true;
}
}
ecs_os_api_t ecs_os_get_api(void) {
return ecs_os_api;
}
void ecs_os_init(void)
{
if (!ecs_os_api_initialized) {
ecs_os_set_api_defaults();
}
if (!(ecs_os_api_init_count ++)) {
if (ecs_os_api.init_) {
ecs_os_api.init_();
}
}
}
void ecs_os_fini(void) {
if (!--ecs_os_api_init_count) {
if (ecs_os_api.fini_) {
ecs_os_api.fini_();
}
}
}
/* Assume every non-glibc Linux target has no execinfo.
This mainly fixes musl support, as musl doesn't define any preprocessor macro specifying its presence. */
#if (defined(ECS_TARGET_LINUX) && !defined(__GLIBC__)) || defined(__COSMOCC__)
#define HAVE_EXECINFO 0
#elif !defined(ECS_TARGET_WINDOWS) && !defined(ECS_TARGET_EM) && !defined(ECS_TARGET_ANDROID)
#define HAVE_EXECINFO 1
#else
#define HAVE_EXECINFO 0
#endif
#if HAVE_EXECINFO
#include <execinfo.h>
#define ECS_BT_BUF_SIZE 100
void flecs_dump_backtrace(
void *stream)
{
int nptrs;
void *buffer[ECS_BT_BUF_SIZE];
char **strings;
nptrs = backtrace(buffer, ECS_BT_BUF_SIZE);
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
return;
}
for (int j = 1; j < nptrs; j++) {
fprintf(stream, "%s\n", strings[j]);
}
free(strings);
}
#else
void flecs_dump_backtrace(
void *stream)
{
(void)stream;
}
#endif
#undef HAVE_EXECINFO_H
static
void flecs_log_msg(
int32_t level,
const char *file,
int32_t line,
const char *msg)
{
FILE *stream = ecs_os_api.log_out_;
if (!stream) {
stream = stdout;
}
bool use_colors = ecs_os_api.flags_ & EcsOsApiLogWithColors;
bool timestamp = ecs_os_api.flags_ & EcsOsApiLogWithTimeStamp;
bool deltatime = ecs_os_api.flags_ & EcsOsApiLogWithTimeDelta;
time_t now = 0;
if (deltatime) {
now = time(NULL);
int64_t delta = 0;
if (ecs_os_api.log_last_timestamp_) {
delta = now - ecs_os_api.log_last_timestamp_;
}
ecs_os_api.log_last_timestamp_ = (int64_t)now;
if (delta) {
if (delta < 10) {
fputs(" ", stream);
}
if (delta < 100) {
fputs(" ", stream);
}
char time_buf[20];
ecs_os_snprintf(time_buf, 20, "%u", (uint32_t)delta);
fputs("+", stream);
fputs(time_buf, stream);
fputs(" ", stream);
} else {
fputs(" ", stream);
}
}
if (timestamp) {
if (!now) {
now = time(NULL);
}
char time_buf[20];
ecs_os_snprintf(time_buf, 20, "%u", (uint32_t)now);
fputs(time_buf, stream);
fputs(" ", stream);
}
if (level >= 4) {
if (use_colors) fputs(ECS_NORMAL, stream);
fputs("jrnl", stream);
} else if (level >= 0) {
if (level == 0) {
if (use_colors) fputs(ECS_MAGENTA, stream);
} else {
if (use_colors) fputs(ECS_GREY, stream);
}
fputs("info", stream);
} else if (level == -2) {
if (use_colors) fputs(ECS_YELLOW, stream);
fputs("warning", stream);
} else if (level == -3) {
if (use_colors) fputs(ECS_RED, stream);
fputs("error", stream);
} else if (level == -4) {
if (use_colors) fputs(ECS_RED, stream);
fputs("fatal", stream);
}
if (use_colors) fputs(ECS_NORMAL, stream);
fputs(": ", stream);
if (level >= 0) {
if (ecs_os_api.log_indent_) {
char indent[32];
int i, indent_count = ecs_os_api.log_indent_;
if (indent_count > 15) indent_count = 15;
for (i = 0; i < indent_count; i ++) {
indent[i * 2] = '|';
indent[i * 2 + 1] = ' ';
}
if (ecs_os_api.log_indent_ != indent_count) {
indent[i * 2 - 2] = '+';
}
indent[i * 2] = '\0';
fputs(indent, stream);
}
}
if (level < 0) {
if (file) {
const char *file_ptr = strrchr(file, '/');
if (!file_ptr) {
file_ptr = strrchr(file, '\\');
}
if (file_ptr) {
file = file_ptr + 1;
}
fputs(file, stream);
fputs(": ", stream);
}
if (line) {
fprintf(stream, "%d: ", line);
}
}
fputs(msg, stream);
fputs("\n", stream);
if (level == -4) {
flecs_dump_backtrace(stream);
}
}
void ecs_os_dbg(
const char *file,
int32_t line,
const char *msg)
{
if (ecs_os_api.log_) {
ecs_os_api.log_(1, file, line, msg);
}
}
void ecs_os_trace(
const char *file,
int32_t line,
const char *msg)
{
if (ecs_os_api.log_) {
ecs_os_api.log_(0, file, line, msg);
}
}
void ecs_os_warn(
const char *file,
int32_t line,
const char *msg)
{
if (ecs_os_api.log_) {
ecs_os_api.log_(-2, file, line, msg);
}
}
void ecs_os_err(
const char *file,
int32_t line,
const char *msg)
{
if (ecs_os_api.log_) {
ecs_os_api.log_(-3, file, line, msg);
}
}
void ecs_os_fatal(
const char *file,
int32_t line,
const char *msg)
{
if (ecs_os_api.log_) {
ecs_os_api.log_(-4, file, line, msg);
}
}
static
void ecs_os_gettime(ecs_time_t *time) {
ecs_assert(ecs_os_has_time() == true, ECS_MISSING_OS_API, NULL);
uint64_t now = ecs_os_now();
uint64_t sec = now / 1000000000;
assert(sec < UINT32_MAX);
assert((now - sec * 1000000000) < UINT32_MAX);
time->sec = (uint32_t)sec;
time->nanosec = (uint32_t)(now - sec * 1000000000);
}
static
void* ecs_os_api_malloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_malloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
return malloc((size_t)size);
}
static
void* ecs_os_api_calloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_calloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
return calloc(1, (size_t)size);
}
static
void* ecs_os_api_realloc(void *ptr, ecs_size_t size) {
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
if (ptr) {
ecs_os_linc(&ecs_os_api_realloc_count);
} else {
/* If not actually reallocing, treat as malloc */
ecs_os_linc(&ecs_os_api_malloc_count);
}
return realloc(ptr, (size_t)size);
}
static
void ecs_os_api_free(void *ptr) {
if (ptr) {
ecs_os_linc(&ecs_os_api_free_count);
}
free(ptr);
}
static
char* ecs_os_api_strdup(const char *str) {
if (str) {
int len = ecs_os_strlen(str);
char *result = ecs_os_malloc(len + 1);
ecs_assert(result != NULL, ECS_OUT_OF_MEMORY, NULL);
ecs_os_strcpy(result, str);
return result;
} else {
return NULL;
}
}
void ecs_os_strset(char **str, const char *value) {
char *old = str[0];
str[0] = ecs_os_strdup(value);
ecs_os_free(old);
}
void ecs_os_perf_trace_push_(
const char *file,
size_t line,
const char *name)
{
if (ecs_os_api.perf_trace_push_) {
ecs_os_api.perf_trace_push_(file, line, name);
}
}
void ecs_os_perf_trace_pop_(
const char *file,
size_t line,
const char *name)
{
if (ecs_os_api.perf_trace_pop_) {
ecs_os_api.perf_trace_pop_(file, line, name);
}
}
/* Replace dots with underscores */
static
char *module_file_base(const char *module, char sep) {
char *base = ecs_os_strdup(module);
ecs_size_t i, len = ecs_os_strlen(base);
for (i = 0; i < len; i ++) {
if (base[i] == '.') {
base[i] = sep;
}
}
return base;
}
static
char* ecs_os_api_module_to_dl(const char *module) {
ecs_strbuf_t lib = ECS_STRBUF_INIT;
/* Best guess, use module name with underscores + OS library extension */
char *file_base = module_file_base(module, '_');
# if defined(ECS_TARGET_LINUX) || defined(ECS_TARGET_FREEBSD)
ecs_strbuf_appendlit(&lib, "lib");
ecs_strbuf_appendstr(&lib, file_base);
ecs_strbuf_appendlit(&lib, ".so");
# elif defined(ECS_TARGET_DARWIN)
ecs_strbuf_appendlit(&lib, "lib");
ecs_strbuf_appendstr(&lib, file_base);
ecs_strbuf_appendlit(&lib, ".dylib");
# elif defined(ECS_TARGET_WINDOWS)
ecs_strbuf_appendstr(&lib, file_base);
ecs_strbuf_appendlit(&lib, ".dll");
# endif
ecs_os_free(file_base);
return ecs_strbuf_get(&lib);
}
static
char* ecs_os_api_module_to_etc(const char *module) {
ecs_strbuf_t lib = ECS_STRBUF_INIT;
/* Best guess, use module name with dashes + /etc */
char *file_base = module_file_base(module, '-');
ecs_strbuf_appendstr(&lib, file_base);
ecs_strbuf_appendlit(&lib, "/etc");
ecs_os_free(file_base);
return ecs_strbuf_get(&lib);
}
void ecs_os_set_api_defaults(void)
{
/* Don't overwrite if already initialized */
if (ecs_os_api_initialized != 0) {
return;
}
if (ecs_os_api_initializing != 0) {
return;
}
ecs_os_api_initializing = true;
/* Memory management */
ecs_os_api.malloc_ = ecs_os_api_malloc;
ecs_os_api.free_ = ecs_os_api_free;
ecs_os_api.realloc_ = ecs_os_api_realloc;
ecs_os_api.calloc_ = ecs_os_api_calloc;
/* Strings */
ecs_os_api.strdup_ = ecs_os_api_strdup;
/* Time */
ecs_os_api.get_time_ = ecs_os_gettime;
/* Logging */
ecs_os_api.log_ = flecs_log_msg;
/* Modules */
if (!ecs_os_api.module_to_dl_) {
ecs_os_api.module_to_dl_ = ecs_os_api_module_to_dl;
}
if (!ecs_os_api.module_to_etc_) {
ecs_os_api.module_to_etc_ = ecs_os_api_module_to_etc;
}
ecs_os_api.abort_ = abort;
# ifdef FLECS_OS_API_IMPL
/* Initialize defaults to OS API IMPL addon, but still allow for overriding
* by the application */
ecs_set_os_api_impl();
ecs_os_api_initialized = false;
# endif
ecs_os_api_initializing = false;
}
bool ecs_os_has_heap(void) {
return
(ecs_os_api.malloc_ != NULL) &&
(ecs_os_api.calloc_ != NULL) &&
(ecs_os_api.realloc_ != NULL) &&
(ecs_os_api.free_ != NULL);
}
bool ecs_os_has_threading(void) {
return
(ecs_os_api.mutex_new_ != NULL) &&
(ecs_os_api.mutex_free_ != NULL) &&
(ecs_os_api.mutex_lock_ != NULL) &&
(ecs_os_api.mutex_unlock_ != NULL) &&
(ecs_os_api.cond_new_ != NULL) &&
(ecs_os_api.cond_free_ != NULL) &&
(ecs_os_api.cond_wait_ != NULL) &&
(ecs_os_api.cond_signal_ != NULL) &&
(ecs_os_api.cond_broadcast_ != NULL) &&
(ecs_os_api.thread_new_ != NULL) &&
(ecs_os_api.thread_join_ != NULL) &&
(ecs_os_api.thread_self_ != NULL);
}
bool ecs_os_has_task_support(void) {
return
(ecs_os_api.mutex_new_ != NULL) &&
(ecs_os_api.mutex_free_ != NULL) &&
(ecs_os_api.mutex_lock_ != NULL) &&
(ecs_os_api.mutex_unlock_ != NULL) &&
(ecs_os_api.cond_new_ != NULL) &&
(ecs_os_api.cond_free_ != NULL) &&
(ecs_os_api.cond_wait_ != NULL) &&
(ecs_os_api.cond_signal_ != NULL) &&
(ecs_os_api.cond_broadcast_ != NULL) &&
(ecs_os_api.task_new_ != NULL) &&
(ecs_os_api.task_join_ != NULL);
}
bool ecs_os_has_time(void) {
return
(ecs_os_api.get_time_ != NULL) &&
(ecs_os_api.sleep_ != NULL) &&
(ecs_os_api.now_ != NULL);
}
bool ecs_os_has_logging(void) {
return (ecs_os_api.log_ != NULL);
}
bool ecs_os_has_dl(void) {
return
(ecs_os_api.dlopen_ != NULL) &&
(ecs_os_api.dlproc_ != NULL) &&
(ecs_os_api.dlclose_ != NULL);
}
bool ecs_os_has_modules(void) {
return
(ecs_os_api.module_to_dl_ != NULL) &&
(ecs_os_api.module_to_etc_ != NULL);
}
#if defined(ECS_TARGET_WINDOWS)
static char error_str[255];
#endif
const char* ecs_os_strerror(int err) {
# if defined(ECS_TARGET_WINDOWS)
strerror_s(error_str, 255, err);
return error_str;
# else
return strerror(err);
# endif
}