Add source locs to allocator procs

This commit is contained in:
Krzosa Karol
2024-01-28 11:21:57 +01:00
parent 32deac08e0
commit 1547ebd5ce
4 changed files with 21 additions and 16 deletions

View File

@@ -23,7 +23,7 @@
#include "../standalone_libraries/table.hpp" #include "../standalone_libraries/table.hpp"
#define ARRAY_ASSERT IO_Assert #define ARRAY_ASSERT IO_Assert
#define ARRAY_Allocator M_Allocator #define ARRAY_Allocator M_Allocator
#define ARRAY_REALLOCATE(allocator, p, size, old_size) M_ReallocNonZeroed(allocator, p, size, old_size) #define ARRAY_REALLOCATE(allocator, p, size, old_size) M_Realloc(allocator, p, size, old_size)
#define ARRAY_DEALLOCATE(allocator, p) M_Dealloc(allocator, p) #define ARRAY_DEALLOCATE(allocator, p) M_Dealloc(allocator, p)
#define ARRAY_SET_DEFAULT_ALLOCATOR \ #define ARRAY_SET_DEFAULT_ALLOCATOR \
if (!allocator.p) allocator = M_GetSystemAllocator(); if (!allocator.p) allocator = M_GetSystemAllocator();

View File

@@ -242,28 +242,28 @@ MA_API void MA_Load(MA_Checkpoint checkpoint) {
MA_PopToPos(checkpoint.arena, checkpoint.pos); MA_PopToPos(checkpoint.arena, checkpoint.pos);
} }
MA_API void *M_AllocNonZeroed(M_Allocator allocator, size_t size) { MA_API void *M__AllocNonZeroed(M_Allocator allocator, size_t size) {
void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size, 0); void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size, 0);
return p; return p;
} }
MA_API void *M_Alloc(M_Allocator allocator, size_t size) { MA_API void *M__Alloc(M_Allocator allocator, size_t size) {
void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size, 0); void *p = allocator.p(allocator.obj, M_AllocatorOp_Allocate, NULL, size, 0);
MA_MemoryZero(p, size); MA_MemoryZero(p, size);
return p; return p;
} }
MA_API void *M_AllocCopy(M_Allocator allocator, void *p, size_t size) { MA_API void *M__AllocCopy(M_Allocator allocator, void *p, size_t size) {
void *copy_buffer = M_AllocNonZeroed(allocator, size); void *copy_buffer = M__AllocNonZeroed(allocator, size);
MA_MemoryCopy(copy_buffer, p, size); MA_MemoryCopy(copy_buffer, p, size);
return copy_buffer; return copy_buffer;
} }
MA_API void M_Dealloc(M_Allocator allocator, void *p) { MA_API void M__Dealloc(M_Allocator allocator, void *p) {
allocator.p(allocator.obj, M_AllocatorOp_Deallocate, p, 0, 0); allocator.p(allocator.obj, M_AllocatorOp_Deallocate, p, 0, 0);
} }
MA_API void *M_ReallocNonZeroed(M_Allocator allocator, void *p, size_t size, size_t old_size) { MA_API void *M__Realloc(M_Allocator allocator, void *p, size_t size, size_t old_size) {
void *result = allocator.p(allocator.obj, M_AllocatorOp_Reallocate, p, size, old_size); void *result = allocator.p(allocator.obj, M_AllocatorOp_Reallocate, p, size, old_size);
// @todo: add old_size? because we can't zero // @todo: add old_size? because we can't zero
return result; return result;

View File

@@ -107,12 +107,18 @@ MA_API void MA_SaveSourceLocEx(const char *file, int line);
#define MA_PushSizeNonZeroed(a, size) (MA_SaveSourceLoc(), MA__PushSizeNonZeroed(a, size)) #define MA_PushSizeNonZeroed(a, size) (MA_SaveSourceLoc(), MA__PushSizeNonZeroed(a, size))
#define MA_PushCopy(a, p, size) (MA_SaveSourceLoc(), MA__PushCopy(a, p, size)) #define MA_PushCopy(a, p, size) (MA_SaveSourceLoc(), MA__PushCopy(a, p, size))
#define MA_PushStringCopy(a, p, size) (MA_SaveSourceLoc(), MA__PushStringCopy(a, p, size)) #define MA_PushStringCopy(a, p, size) (MA_SaveSourceLoc(), MA__PushStringCopy(a, p, size))
#define M_Alloc(a, size) (MA_SaveSourceLoc(), M__Alloc(a, size))
#define M_AllocNonZeroed(a, size) (MA_SaveSourceLoc(), M__AllocNonZeroed(a, size))
#define M_AllocCopy(a, p, size) (MA_SaveSourceLoc(), M__AllocCopy(a, p, size))
#define M_Realloc(a, p, size, old_size) (MA_SaveSourceLoc(), M__Realloc(a, p, size, old_size))
#define M_Dealloc(a, p) (MA_SaveSourceLoc(), M__Dealloc(a, p))
#define MA_PushArrayNonZeroed(a, T, c) (T *)MA__PushSizeNonZeroed(a, sizeof(T) * (c)) #define MA_PushArrayNonZeroed(a, T, c) (T *)MA__PushSizeNonZeroed(a, sizeof(T) * (c))
#define MA_PushStructNonZeroed(a, T) (T *)MA__PushSizeNonZeroed(a, sizeof(T)) #define MA_PushStructNonZeroed(a, T) (T *)MA__PushSizeNonZeroed(a, sizeof(T))
#define MA_PushStruct(a, T) (T *)MA__PushSize(a, sizeof(T)) #define MA_PushStruct(a, T) (T *)MA__PushSize(a, sizeof(T))
#define MA_PushArray(a, T, c) (T *)MA__PushSize(a, sizeof(T) * (c)) #define MA_PushArray(a, T, c) (T *)MA__PushSize(a, sizeof(T) * (c))
#define MA_PushStructCopy(a, T, p) (T *)MA__PushCopy(a, (p), sizeof(T)) #define MA_PushStructCopy(a, T, p) (T *)MA__PushCopy(a, (p), sizeof(T))
#define MA_CheckpointScope(name, InArena) for (MA_Checkpoint name = MA_Save(InArena); name.arena; (MA_Load(name), name.arena = 0))
#define M_AllocStruct(a, T) (T *)M_Alloc((a), sizeof(T)) #define M_AllocStruct(a, T) (T *)M_Alloc((a), sizeof(T))
#define M_AllocArray(a, T, c) (T *)M_Alloc((a), sizeof(T) * (c)) #define M_AllocArray(a, T, c) (T *)M_Alloc((a), sizeof(T) * (c))
@@ -167,17 +173,19 @@ MA_API bool MV_Commit(MV_Memory *m, size_t commit);
MA_API void MV_Deallocate(MV_Memory *m); MA_API void MV_Deallocate(MV_Memory *m);
MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos); MA_API bool MV_DecommitPos(MV_Memory *m, size_t pos);
MA_API void * M_AllocNonZeroed(M_Allocator allocator, size_t size); MA_API void * M__AllocNonZeroed(M_Allocator allocator, size_t size);
MA_API void * M_Alloc(M_Allocator allocator, size_t size); MA_API void * M__Alloc(M_Allocator allocator, size_t size);
MA_API void * M_AllocCopy(M_Allocator allocator, void *p, size_t size); MA_API void * M__AllocCopy(M_Allocator allocator, void *p, size_t size);
MA_API void * M_ReallocNonZeroed(M_Allocator allocator, void *p, size_t size, size_t old_size); MA_API void * M__Realloc(M_Allocator allocator, void *p, size_t size, size_t old_size);
MA_API void M_Dealloc(M_Allocator allocator, void *p); MA_API void M__Dealloc(M_Allocator allocator, void *p);
MA_API M_Allocator M_GetSystemAllocator(void); MA_API M_Allocator M_GetSystemAllocator(void);
MA_API M_Allocator MA_GetExclusiveAllocator(MA_Arena *arena); MA_API M_Allocator MA_GetExclusiveAllocator(MA_Arena *arena);
MA_API M_Allocator MA_GetAllocator(MA_Arena *arena); MA_API M_Allocator MA_GetAllocator(MA_Arena *arena);
// clang-format on // clang-format on
#define MA_CheckpointScope(name, InArena) for (MA_Checkpoint name = MA_Save(InArena); name.arena; (MA_Load(name), name.arena = 0))
#ifndef MA_DISABLE_SCRATCH #ifndef MA_DISABLE_SCRATCH
extern MA_THREAD_LOCAL MA_Arena MA_ScratchArenaPool[]; extern MA_THREAD_LOCAL MA_Arena MA_ScratchArenaPool[];
MA_API MA_Checkpoint MA_GetScratchEx(MA_Arena **conflicts, int conflict_count); MA_API MA_Checkpoint MA_GetScratchEx(MA_Arena **conflicts, int conflict_count);

View File

@@ -159,9 +159,6 @@ UTF_API int64_t UTF_CreateCharFromWidechar(char *buffer, int64_t buffer_size, wc
return outlen; return outlen;
} }
// @todo: the api here is from one side cool but from other kind of weird
// int64_t size = UTF_CreateWidecharFromChar(wcmd, cmd.len + 1, cmd.str, cmd.len);
// the "+ 1" part is bothering me, but if it wrote one past buffer_size, that would be worse
UTF_API int64_t UTF_CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen) { UTF_API int64_t UTF_CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen) {
int64_t outlen = 0; int64_t outlen = 0;
for (int64_t i = 0; i < inlen;) { for (int64_t i = 0; i < inlen;) {