Restore Allocator implementation
This commit is contained in:
parent
b8f6e5f187
commit
ce537b7494
47
src/core/mem/allocator/mem_allocator.c
Normal file
47
src/core/mem/allocator/mem_allocator.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include "mem_allocator.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#pragma region Allocator API definitions
|
||||
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) {
|
||||
if (!allocator || !(allocator->alloc)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return allocator->alloc(size, allocator->obj);
|
||||
}
|
||||
|
||||
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
|
||||
u64 alignment) {
|
||||
if (!allocator || !(allocator->alloc_aligned)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return allocator->alloc_aligned(size, alignment, allocator->obj);
|
||||
}
|
||||
|
||||
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr,
|
||||
u64 size) {
|
||||
if (!allocator || !(allocator->realloc)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return allocator->realloc(ptr, size, allocator->obj);
|
||||
}
|
||||
|
||||
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
|
||||
u64 size, u64 alignment) {
|
||||
if (!allocator || !(allocator->realloc_aligned)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return allocator->realloc_aligned(ptr, size, alignment, allocator->obj);
|
||||
}
|
||||
|
||||
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr) {
|
||||
if (!allocator || !(allocator->free)) {
|
||||
return;
|
||||
}
|
||||
|
||||
allocator->free(ptr, allocator->obj);
|
||||
}
|
||||
#pragma endregion Allocator API definitions
|
46
src/core/mem/allocator/mem_allocator.h
Normal file
46
src/core/mem/allocator/mem_allocator.h
Normal file
@ -0,0 +1,46 @@
|
||||
#ifndef MEM_ALLOCATOR_H
|
||||
#define MEM_ALLOCATOR_H
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#pragma region Allocator function pointer types
|
||||
typedef void *(MemAllocFunc)(u64 size, void *alloc_obj);
|
||||
typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
|
||||
typedef void *(MemReallocFunc)(void *ptr, u64 size, void *alloc_obj);
|
||||
typedef void *(MemReallocAlignedFunc)(void *ptr, u64 size, u64 alignment,
|
||||
void *alloc_obj);
|
||||
typedef void(MemFreeFunc)(void **ptr, void *alloc_obj);
|
||||
#pragma endregion Allocator function pointer types
|
||||
|
||||
#pragma region Allocator type
|
||||
typedef struct allocator Allocator;
|
||||
struct allocator {
|
||||
void *obj;
|
||||
MemAllocFunc *alloc;
|
||||
MemAllocAlignedFunc *alloc_aligned;
|
||||
MemReallocFunc *realloc;
|
||||
MemReallocAlignedFunc *realloc_aligned;
|
||||
MemFreeFunc *free;
|
||||
};
|
||||
#pragma endregion Allocator type
|
||||
|
||||
#pragma region Allocator API declarations
|
||||
void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size);
|
||||
void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size,
|
||||
u64 alignment);
|
||||
void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr,
|
||||
u64 size);
|
||||
void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr,
|
||||
u64 size, u64 alignment);
|
||||
void wapp_mem_allocator_free(const Allocator *allocator, void **ptr);
|
||||
#pragma endregion Allocator API declarations
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_ALLOCATOR_H
|
Loading…
Reference in New Issue
Block a user