From 5a26e1f481fe85f08093a414e27c1056b85c6715 Mon Sep 17 00:00:00 2001 From: Clement Sibille Date: Thu, 9 May 2024 08:58:14 +0900 Subject: Initial commit --- src/lisiblestd/memory.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/lisiblestd/memory.h (limited to 'src/lisiblestd/memory.h') diff --git a/src/lisiblestd/memory.h b/src/lisiblestd/memory.h new file mode 100644 index 0000000..2282206 --- /dev/null +++ b/src/lisiblestd/memory.h @@ -0,0 +1,41 @@ +#ifndef LSTD_MEMORY_H +#define LSTD_MEMORY_H + +#include "types.h" + +typedef struct { + void *(*allocate)(usize size, void *ctx); + void *(*allocate_aligned)(usize alignment, usize size, void *ctx); + void *(*allocate_array)(usize count, usize item_size, void *ctx); + void *(*reallocate)(void *ptr, usize old_size, usize new_size, void *ctx); + void (*free)(void *ptr, void *ctx); + void *ctx; +} Allocator; + +void *Allocator_allocate(Allocator *allocator, usize size); +void *Allocator_allocate_aligned(Allocator *allocator, usize alignment, + usize size); +void *Allocator_allocate_array(Allocator *allocator, usize count, + usize item_size); +void *Allocator_reallocate(Allocator *allocator, void *ptr, usize old_size, + usize new_size); +void Allocator_free(Allocator *allocator, void *ptr); + +extern Allocator system_allocator; + +typedef struct { + usize capacity; + usize size; + u8 *data; +} Arena; + +Allocator Arena_allocator(Arena *arena); +void Arena_init(Arena *arena, Allocator *allocator, usize size); +void *Arena_allocate(Arena *arena, usize size); +void *Arena_allocate_array(Arena *arena, usize count, usize item_size); +void Arena_clear(Arena *arena); +void Arena_deinit(Arena *arena, Allocator *allocator); + +char *memory_clone_string(Allocator *allocator, const char *str); + +#endif // LSTD_MEMORY_H -- cgit v1.2.3