taisei/src/util/crap.h
Andrei Alexeyev b6978178b1
memory: use custom memory allocation wrappers
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.
2023-01-18 13:23:22 +01:00

45 lines
1,008 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>.
*/
#pragma once
#include "taisei.h"
#include <SDL.h>
void inherit_missing_pointers(uint num, void *dest[num], void *const base[num]) attr_nonnull(2, 3);
bool is_main_thread(void);
typedef union FloatBits {
float val;
uint32_t bits;
} FloatBits;
typedef union DoubleBits {
double val;
uint64_t bits;
} DoubleBits;
INLINE uint32_t float_to_bits(float f) {
return ((FloatBits) { .val = f }).bits;
}
INLINE float bits_to_float(uint32_t i) {
return ((FloatBits) { .bits = i }).val;
}
INLINE uint64_t double_to_bits(double d) {
return ((DoubleBits) { .val = d }).bits;
}
INLINE double bits_to_double(uint64_t i) {
return ((DoubleBits) { .bits = i }).val;
}
extern SDL_threadID main_thread_id;
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*(arr)))