Compare commits
3 Commits
ca36e0dc35
...
5b8e9f8be6
Author | SHA1 | Date | |
---|---|---|---|
|
5b8e9f8be6 | ||
|
9ec123c41b | ||
|
7039b5437a |
31
mem/include/allocator/mem_allocator.h
Normal file
31
mem/include/allocator/mem_allocator.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef MEM_ALLOCATOR_H
|
||||
#define MEM_ALLOCATOR_H
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
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);
|
||||
|
||||
typedef struct allocator Allocator;
|
||||
struct allocator {
|
||||
void *obj;
|
||||
MemAllocFunc *alloc;
|
||||
MemAllocAlignedFunc *alloc_aligned;
|
||||
MemReallocFunc *realloc;
|
||||
MemReallocAlignedFunc *realloc_aligned;
|
||||
MemFreeFunc *free;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_ALLOCATOR_H
|
@ -17,6 +17,9 @@ typedef enum {
|
||||
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity);
|
||||
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size);
|
||||
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size, u64 alignment);
|
||||
void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size);
|
||||
void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size,
|
||||
u64 alignment);
|
||||
void wapp_mem_ctx_clear(CTXDestBuffer buffer);
|
||||
void wapp_mem_ctx_free(void);
|
||||
|
||||
|
15
mem/include/libc/mem_libc.h
Normal file
15
mem/include/libc/mem_libc.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef MEM_LIBC_H
|
||||
#define MEM_LIBC_H
|
||||
|
||||
#include "mem_allocator.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
Allocator wapp_mem_libc_allocator(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_LIBC_H
|
@ -42,6 +42,25 @@ void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size,
|
||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wapp_mem_arena_realloc(arena, ptr, size);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size,
|
||||
u64 alignment) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment);
|
||||
}
|
||||
|
||||
void wapp_mem_ctx_clear(CTXDestBuffer buffer) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
|
46
mem/src/libc/mem_libc.c
Normal file
46
mem/src/libc/mem_libc.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include "mem_libc.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
internal void *mem_libc_alloc(u64 size, void *alloc_obj);
|
||||
internal void *mem_libc_alloc_aligned(u64 size, u64 alignment, void *alloc_obj);
|
||||
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj);
|
||||
internal void mem_libc_free(void *ptr, void *alloc_obj);
|
||||
|
||||
Allocator wapp_mem_libc_allocator(void) {
|
||||
return (Allocator){
|
||||
.obj = NULL,
|
||||
.alloc = mem_libc_alloc,
|
||||
.alloc_aligned = mem_libc_alloc_aligned,
|
||||
.realloc = mem_libc_realloc,
|
||||
.realloc_aligned = NULL,
|
||||
.free = mem_libc_free,
|
||||
};
|
||||
}
|
||||
|
||||
internal void *mem_libc_alloc(u64 size, void *alloc_obj) {
|
||||
return calloc(1, size);
|
||||
}
|
||||
|
||||
internal void *mem_libc_alloc_aligned(u64 size, u64 alignment,
|
||||
void *alloc_obj) {
|
||||
void *output = aligned_alloc(alignment, size);
|
||||
if (output) {
|
||||
memset(output, 0, size);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
internal void *mem_libc_realloc(void *ptr, u64 size, void *alloc_obj) {
|
||||
void *output = realloc(ptr, size);
|
||||
if (output) {
|
||||
memset(output, 0, size);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
internal void mem_libc_free(void *ptr, void *alloc_obj) { free(ptr); }
|
Loading…
Reference in New Issue
Block a user