Introduces wrappers around memory allocation functions in `memory.h` that should be used instead of the standard C ones. These never return NULL and, with the exception of `mem_realloc()`, zero-initialize the allocated memory like `calloc()` does. All allocations made with the memory.h API must be deallocated with `mem_free()`. Although standard `free()` will work on some platforms, it's not portable (currently it won't work on Windows). Likewise, `mem_free()` must not be used to free foreign allocations. The standard C allocation functions are now diagnosed as deprecated. They are, however, available with the `libc_` prefix in case interfacing with foreign APIs is required. So far they are only used to implement `memory.h`. Perhaps the most important change is the introduction of the `ALLOC()`, `ALLOC_ARRAY()`, and `ALLOC_FLEX()` macros. They take a type as a parameter, and allocate enough memory with the correct alignment for that type. That includes overaligned types as well. In most circumstances you should prefer to use these macros. See the `memory.h` header for some usage examples.
35 lines
788 B
C
35 lines
788 B
C
/*
|
|
* This software is licensed under the terms of the MIT License.
|
|
* See COPYING for further information.
|
|
* ---
|
|
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
|
|
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
|
|
*/
|
|
|
|
#include "taisei.h"
|
|
|
|
#include "crap.h"
|
|
#include "assert.h"
|
|
|
|
#include <SDL_thread.h>
|
|
|
|
static_assert(sizeof(void*) == sizeof(void (*)(void)), "Can't store function pointers in void* :(");
|
|
|
|
void inherit_missing_pointers(uint num, void *dest[num], void *const base[num]) {
|
|
for(uint i = 0; i < num; ++i) {
|
|
if(dest[i] == NULL) {
|
|
dest[i] = base[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
SDL_threadID main_thread_id = 0;
|
|
|
|
bool is_main_thread(void) {
|
|
if(main_thread_id == 0) {
|
|
return true;
|
|
}
|
|
|
|
SDL_threadID tid = SDL_ThreadID();
|
|
return main_thread_id == tid;
|
|
}
|