Compare commits
33 Commits
b8db582098
...
mem_contex
Author | SHA1 | Date | |
---|---|---|---|
6195b521f5 | |||
be64571b0e | |||
645686ae22 | |||
3f9a908860 | |||
2e93bd794a | |||
39c88505bd | |||
970c588d66 | |||
|
efe8a104de | ||
|
6f799c4330 | ||
|
8a58a1cc48 | ||
|
03d2a59948 | ||
|
5b8e9f8be6 | ||
|
9ec123c41b | ||
|
7039b5437a | ||
|
ca36e0dc35 | ||
|
3fdd897291 | ||
|
08703b465c | ||
|
49ce26a6c2 | ||
feada0b31c | |||
|
0c07437ef2 | ||
|
063bc03974 | ||
|
09af7ec734 | ||
|
04e86d355d | ||
|
7bfaf53d4e | ||
|
958df4b55a | ||
|
9179f9beaa | ||
|
7aaeb91fe1 | ||
|
df4315cdcc | ||
|
b3a174f26c | ||
6fc0b2987e | |||
76222d31d4 | |||
7948d3fd1a | |||
1094a9fefb |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.cache
|
||||
.vscode
|
||||
compile_commands.json
|
||||
libwizapp.so
|
||||
libwapp.so
|
||||
|
6
compile
6
compile
@@ -5,14 +5,14 @@ INCLUDE="\
|
||||
-Ialiases \
|
||||
-Icpath/include \
|
||||
-Idstr/include \
|
||||
$(find mem/include/ -type d | xargs -I{} printf "-I{} ") \
|
||||
$(find mem/include -type d | xargs -I{} echo -n "-I{} ") \
|
||||
"
|
||||
SRC="\
|
||||
cpath/src/*.c \
|
||||
dstr/src/*.c \
|
||||
mem/src/*/*.c \
|
||||
"
|
||||
CFLAGS="-shared -fPIC -Wall -Werror -pedantic"
|
||||
OUT="libwizapp.so"
|
||||
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
||||
OUT="libwapp.so"
|
||||
|
||||
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)
|
||||
|
@@ -3,15 +3,23 @@
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
#define NUMPARTS(...) \
|
||||
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
||||
|
||||
#define cpath_join_path(DST, ...) \
|
||||
#define wapp_cpath_join_path(DST, ...) \
|
||||
join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
|
||||
#define cpath_dirname(DST, PATH) dirup(DST, 1, PATH)
|
||||
#define cpath_dirup(DST, COUNT, PATH) dirup(DST, COUNT, PATH)
|
||||
#define wapp_cpath_dirname(DST, PATH) dirup(DST, 1, PATH)
|
||||
#define wapp_cpath_dirup(DST, COUNT, PATH) dirup(DST, COUNT, PATH)
|
||||
|
||||
void join_path(char *dst, u64 count, ...);
|
||||
void dirup(char *dst, u64 levels, const char *path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !PATH_UTILS_H
|
||||
|
@@ -2,21 +2,30 @@
|
||||
#define DSTR_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
typedef struct dstr String;
|
||||
|
||||
String *dstr_with_capacity(u64 capacity);
|
||||
String *dstr_from_string(const char *str);
|
||||
void dstr_update(String **dst, const char *src);
|
||||
void dstr_free(String **str);
|
||||
void dstr_concat(String **dst, const char *src);
|
||||
void dstr_append(String **dst, char c);
|
||||
void dstr_resize(String **str);
|
||||
void dstr_clear(String *str);
|
||||
void dstr_print(const String *str);
|
||||
i64 dstr_find(const String *str, const char *substr);
|
||||
u64 dstr_length(const String *str);
|
||||
u64 dstr_capacity(const String *str);
|
||||
const char *dstr_to_cstr(const String *str);
|
||||
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator);
|
||||
String *wapp_dstr_from_string(const char *str, const Allocator *allocator);
|
||||
void wapp_dstr_update(String **dst, const char *src);
|
||||
void wapp_dstr_free(String **str);
|
||||
void wapp_dstr_concat(String **dst, const char *src);
|
||||
void wapp_dstr_append(String **dst, char c);
|
||||
void wapp_dstr_resize(String **str);
|
||||
void wapp_dstr_clear(String *str);
|
||||
void wapp_dstr_print(const String *str);
|
||||
i64 wapp_dstr_find(const String *str, const char *substr);
|
||||
u64 wapp_dstr_length(const String *str);
|
||||
u64 wapp_dstr_capacity(const String *str);
|
||||
const char *wapp_dstr_to_cstr(const String *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !DSTR_H
|
||||
|
@@ -1,5 +1,7 @@
|
||||
#include "dstr.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_libc.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -9,26 +11,34 @@
|
||||
#define CAPACITY_SCALAR 8
|
||||
|
||||
struct dstr {
|
||||
Allocator allocator;
|
||||
u64 capacity;
|
||||
u64 size;
|
||||
char buf[];
|
||||
};
|
||||
|
||||
String *dstr_with_capacity(u64 capacity) {
|
||||
String *out = (String *)malloc(sizeof(String) + capacity + 1);
|
||||
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator) {
|
||||
Allocator alloc;
|
||||
if (allocator) {
|
||||
alloc = *allocator;
|
||||
} else {
|
||||
alloc = wapp_mem_libc_allocator();
|
||||
}
|
||||
|
||||
String *out =
|
||||
(String *)wapp_mem_allocator_alloc(&alloc, sizeof(String) + capacity + 1);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->allocator = alloc;
|
||||
out->capacity = capacity;
|
||||
out->size = 0;
|
||||
memset(out->buf, 0, capacity + 1);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
String *dstr_from_string(const char *str) {
|
||||
String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -37,19 +47,18 @@ String *dstr_from_string(const char *str) {
|
||||
|
||||
u64 capacity = length * CAPACITY_SCALAR;
|
||||
|
||||
String *out = dstr_with_capacity(capacity);
|
||||
|
||||
String *out = wapp_dstr_with_capacity(capacity, allocator);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->size = length;
|
||||
strncpy(out->buf, str, length);
|
||||
strncpy(out->buf, str, length + 1);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void dstr_update(String **dst, const char *src) {
|
||||
void wapp_dstr_update(String **dst, const char *src) {
|
||||
if (!dst || !(*dst)) {
|
||||
return;
|
||||
}
|
||||
@@ -58,45 +67,44 @@ void dstr_update(String **dst, const char *src) {
|
||||
|
||||
String *str = *dst;
|
||||
|
||||
if (length <= str->capacity) {
|
||||
if (length < str->capacity) {
|
||||
memset(str->buf, 0, str->capacity);
|
||||
|
||||
str->size = length;
|
||||
|
||||
strncpy(str->buf, src, length);
|
||||
strncpy(str->buf, src, length + 1);
|
||||
} else {
|
||||
u64 capacity = length * CAPACITY_SCALAR;
|
||||
|
||||
String *tmp = (String *)realloc(*dst, sizeof(String) + capacity + 1);
|
||||
|
||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||
&(str->allocator), *dst, sizeof(String) + capacity + 1);
|
||||
if (!tmp) {
|
||||
return;
|
||||
}
|
||||
|
||||
tmp->capacity = capacity;
|
||||
tmp->size = length;
|
||||
strncpy(tmp->buf, src, length);
|
||||
strncpy(tmp->buf, src, length + 1);
|
||||
|
||||
*dst = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void dstr_free(String **str) {
|
||||
void wapp_dstr_free(String **str) {
|
||||
if (!str || !(*str)) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
String *str_ptr = *str;
|
||||
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
||||
}
|
||||
|
||||
void dstr_concat(String **dst, const char *src) {
|
||||
void wapp_dstr_concat(String **dst, const char *src) {
|
||||
if (!dst || !(*dst)) {
|
||||
return;
|
||||
}
|
||||
|
||||
u64 src_length = strlen(src);
|
||||
|
||||
if (src_length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -107,12 +115,12 @@ void dstr_concat(String **dst, const char *src) {
|
||||
memset(str, 0, new_length + 1);
|
||||
|
||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||
strncat(str, src, src_length);
|
||||
strncat(str, src, new_length + 1 - (*dst)->size);
|
||||
|
||||
dstr_update(dst, str);
|
||||
wapp_dstr_update(dst, str);
|
||||
}
|
||||
|
||||
void dstr_append(String **dst, char c) {
|
||||
void wapp_dstr_append(String **dst, char c) {
|
||||
if (!dst || !(*dst)) {
|
||||
return;
|
||||
}
|
||||
@@ -125,18 +133,19 @@ void dstr_append(String **dst, char c) {
|
||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||
str[(*dst)->size] = c;
|
||||
|
||||
dstr_update(dst, str);
|
||||
wapp_dstr_update(dst, str);
|
||||
}
|
||||
|
||||
void dstr_resize(String **str) {
|
||||
void wapp_dstr_resize(String **str) {
|
||||
if (!str || !(*str)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String *str_ptr = *str;
|
||||
u64 capacity = (*str)->size;
|
||||
|
||||
String *tmp = (String *)realloc(*str, sizeof(String) + capacity + 1);
|
||||
|
||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
|
||||
if (!tmp) {
|
||||
return;
|
||||
}
|
||||
@@ -146,7 +155,7 @@ void dstr_resize(String **str) {
|
||||
*str = tmp;
|
||||
}
|
||||
|
||||
void dstr_clear(String *str) {
|
||||
void wapp_dstr_clear(String *str) {
|
||||
if (!str || str->size == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -155,38 +164,33 @@ void dstr_clear(String *str) {
|
||||
str->size = 0;
|
||||
}
|
||||
|
||||
void dstr_print(const String *str) {
|
||||
void wapp_dstr_print(const String *str) {
|
||||
if (!str) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s\n", str->buf);
|
||||
printf("%.*s\n", (i32)str->size, str->buf);
|
||||
}
|
||||
|
||||
i64 dstr_find(const String *str, const char *substr) {
|
||||
i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||
if (!str || !substr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
u64 substr_length = strlen(substr);
|
||||
|
||||
if (substr_length == 0 || substr_length > str->size) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char buf[substr_length + 1];
|
||||
memset(buf, 0, substr_length + 1);
|
||||
|
||||
const char *s1;
|
||||
for (u64 i = 0; i < str->size; ++i) {
|
||||
if (i + substr_length >= str->size) {
|
||||
if (i + substr_length > str->size) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (u64 j = 0; j < substr_length; ++j) {
|
||||
buf[j] = str->buf[i + j];
|
||||
}
|
||||
s1 = &(str->buf[i]);
|
||||
|
||||
if (strcmp(buf, substr) == 0) {
|
||||
if (strncmp(s1, substr, substr_length) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -194,7 +198,7 @@ i64 dstr_find(const String *str, const char *substr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
u64 dstr_length(const String *str) {
|
||||
u64 wapp_dstr_length(const String *str) {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
@@ -202,7 +206,7 @@ u64 dstr_length(const String *str) {
|
||||
return str->size;
|
||||
}
|
||||
|
||||
u64 dstr_capacity(const String *str) {
|
||||
u64 wapp_dstr_capacity(const String *str) {
|
||||
if (!str) {
|
||||
return 0;
|
||||
}
|
||||
@@ -210,7 +214,7 @@ u64 dstr_capacity(const String *str) {
|
||||
return str->capacity;
|
||||
}
|
||||
|
||||
const char *dstr_to_cstr(const String *str) {
|
||||
const char *wapp_dstr_to_cstr(const String *str) {
|
||||
if (!str) {
|
||||
return "";
|
||||
}
|
||||
|
40
mem/include/allocator/mem_allocator.h
Normal file
40
mem/include/allocator/mem_allocator.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_ALLOCATOR_H
|
@@ -2,14 +2,27 @@
|
||||
#define MEM_ARENA_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
typedef struct growing_arena Arena;
|
||||
|
||||
bool mem_arena_init(Arena **arena, u64 base_capacity);
|
||||
void *mem_arena_alloc(Arena *arena, u64 size);
|
||||
void *mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
||||
void mem_arena_clear(Arena *arena);
|
||||
void mem_arena_free(Arena **arena);
|
||||
Allocator wapp_mem_arena_allocator(const Arena *arena);
|
||||
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity);
|
||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment);
|
||||
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 size);
|
||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 size,
|
||||
u64 alignment);
|
||||
void wapp_mem_arena_clear(Arena *arena);
|
||||
void wapp_mem_arena_free(Arena **arena);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_ARENA_H
|
||||
|
32
mem/include/ctx/mem_ctx.h
Normal file
32
mem/include/ctx/mem_ctx.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef MEM_CTX_H
|
||||
#define MEM_CTX_H
|
||||
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_arena.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
typedef enum {
|
||||
CTX_DEST_BUFFER_MAIN,
|
||||
CTX_DEST_BUFFER_TEMP,
|
||||
|
||||
COUNT_CTX_DEST_BUFFER,
|
||||
} CTXDestBuffer;
|
||||
|
||||
Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer);
|
||||
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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_CTX_H
|
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
|
@@ -3,6 +3,14 @@
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
void *mem_util_align_forward(void *ptr, u64 alignment);
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
void *wapp_mem_util_align_forward(void *ptr, u64 alignment);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // !MEM_UTILS_H
|
||||
|
45
mem/src/allocator/mem_allocator.c
Normal file
45
mem/src/allocator/mem_allocator.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "mem_allocator.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
@@ -1,14 +1,33 @@
|
||||
#include "mem_arena.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_utils.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef DEFAULT_ALIGNMENT
|
||||
// Why 2 * sizeof(void *) instead of sizeof(void *)
|
||||
// https://handmade.network/forums/t/6860-alignment_arena_allocator
|
||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
||||
|
||||
#define HDR_MAGIC_BYTE_COUNT 8
|
||||
#define HDR_MAGIC \
|
||||
{ 0x57, 0x41, 0x41, 0x52, 0x4e, 0x48, 0x44, 0x52 }
|
||||
#define MAX_HDR_SEARCH_LENGTH 256
|
||||
|
||||
#define ARENA_MINIMUM_CAPACITY 1024
|
||||
|
||||
typedef struct arena_alloc_hdr ArenaAllocHDR;
|
||||
struct arena_alloc_hdr {
|
||||
u8 magic[HDR_MAGIC_BYTE_COUNT];
|
||||
u64 alloc_size;
|
||||
u64 alignment;
|
||||
u8 *alloc_start;
|
||||
};
|
||||
|
||||
typedef struct base_arena BaseArena;
|
||||
struct base_arena {
|
||||
u8 *buf;
|
||||
@@ -24,35 +43,54 @@ struct growing_arena {
|
||||
u64 initial_capacity;
|
||||
};
|
||||
|
||||
internal BaseArena *find_arena_from_pointer(const Arena *arena, void *ptr);
|
||||
|
||||
internal bool base_arena_init(BaseArena *arena, u64 capacity);
|
||||
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
||||
u64 alignment);
|
||||
internal void base_arena_clear(BaseArena *arena);
|
||||
internal void base_arena_free(BaseArena *arena);
|
||||
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr);
|
||||
|
||||
internal void *mem_arena_alloc(u64 size, void *alloc_obj);
|
||||
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
||||
void *alloc_obj);
|
||||
internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj);
|
||||
internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment,
|
||||
void *alloc_obj);
|
||||
|
||||
// PUBLIC API
|
||||
|
||||
bool mem_arena_init(Arena **arena, u64 base_capacity) {
|
||||
if (!arena || *arena) {
|
||||
Allocator wapp_mem_arena_allocator(const Arena *arena) {
|
||||
return (Allocator){
|
||||
.obj = (void *)arena,
|
||||
.alloc = mem_arena_alloc,
|
||||
.alloc_aligned = mem_arena_alloc_aligned,
|
||||
.realloc = mem_arena_realloc,
|
||||
.realloc_aligned = mem_arena_realloc_aligned,
|
||||
.free = NULL,
|
||||
};
|
||||
}
|
||||
|
||||
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity) {
|
||||
if (!arena || *arena || base_capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*arena = (Arena *)malloc(sizeof(Arena));
|
||||
*arena = (Arena *)calloc(1, sizeof(Arena));
|
||||
Arena *arena_ptr = *arena;
|
||||
if (!arena_ptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(arena_ptr, 0, sizeof(Arena));
|
||||
|
||||
arena_ptr->active_arena = (BaseArena *)malloc(sizeof(BaseArena));
|
||||
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||
if (!(arena_ptr->active_arena)) {
|
||||
wapp_mem_arena_free(arena);
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(arena_ptr->active_arena, 0, sizeof(BaseArena));
|
||||
|
||||
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
||||
wapp_mem_arena_free(arena);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -62,11 +100,11 @@ bool mem_arena_init(Arena **arena, u64 base_capacity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void *mem_arena_alloc(Arena *arena, u64 size) {
|
||||
return mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
|
||||
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
||||
}
|
||||
|
||||
void *mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
if (!arena || !(arena->active_arena)) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -76,13 +114,11 @@ void *mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
if (arena->active_arena->next) {
|
||||
arena->active_arena = arena->active_arena->next;
|
||||
} else {
|
||||
arena->active_arena->next = (BaseArena *)malloc(sizeof(BaseArena));
|
||||
arena->active_arena->next = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||
if (!(arena->active_arena->next)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(arena->active_arena->next, 0, sizeof(BaseArena));
|
||||
|
||||
if (!base_arena_init(arena->active_arena->next,
|
||||
arena->initial_capacity)) {
|
||||
free(arena->active_arena->next);
|
||||
@@ -106,31 +142,80 @@ void *mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||
return output;
|
||||
}
|
||||
|
||||
void mem_arena_clear(Arena *arena) {
|
||||
void *wapp_mem_arena_realloc(Arena *arena, void *ptr, u64 size) {
|
||||
return wapp_mem_arena_realloc_aligned(arena, ptr, size, DEFAULT_ALIGNMENT);
|
||||
}
|
||||
|
||||
void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 size,
|
||||
u64 alignment) {
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
BaseArena *base_arena = find_arena_from_pointer(arena, ptr);
|
||||
if (!base_arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ArenaAllocHDR *header = find_alloc_header(base_arena, ptr);
|
||||
if (!header) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (header->alloc_start + header->alloc_size == base_arena->offset) {
|
||||
// Avoid allocating new pointer and copying memory if pointer is at the end
|
||||
// of the arena
|
||||
i64 diff = size - header->alloc_size;
|
||||
|
||||
u8 *new_offset = base_arena->offset + diff;
|
||||
|
||||
u8 *clear_start = diff < 0 ? new_offset : base_arena->offset;
|
||||
memset(clear_start, 0, llabs(diff));
|
||||
|
||||
header->alloc_size = size;
|
||||
base_arena->offset = new_offset;
|
||||
|
||||
return header->alloc_start;
|
||||
}
|
||||
|
||||
void *new_alloc = wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||
if (!new_alloc) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u64 to_copy = size < header->alloc_size ? size : header->alloc_size;
|
||||
|
||||
memcpy(new_alloc, ptr, to_copy);
|
||||
|
||||
return new_alloc;
|
||||
}
|
||||
|
||||
void wapp_mem_arena_clear(Arena *arena) {
|
||||
if (!arena) {
|
||||
return;
|
||||
}
|
||||
|
||||
BaseArena *new_active = NULL;
|
||||
BaseArena *last_active = NULL;
|
||||
while (arena->active_arena) {
|
||||
base_arena_clear(arena->active_arena);
|
||||
|
||||
arena->active_arena = arena->active_arena->prev;
|
||||
last_active = arena->active_arena;
|
||||
|
||||
if (arena->active_arena) {
|
||||
new_active = arena->active_arena;
|
||||
}
|
||||
arena->active_arena = arena->active_arena->prev;
|
||||
}
|
||||
|
||||
arena->active_arena = new_active;
|
||||
arena->active_arena = last_active;
|
||||
}
|
||||
|
||||
void mem_arena_free(Arena **arena) {
|
||||
void wapp_mem_arena_free(Arena **arena) {
|
||||
if (!arena) {
|
||||
return;
|
||||
}
|
||||
|
||||
Arena *arena_ptr = *arena;
|
||||
if (!arena_ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
BaseArena *current;
|
||||
BaseArena *next;
|
||||
@@ -170,20 +255,44 @@ void mem_arena_free(Arena **arena) {
|
||||
|
||||
// INTERNAL FUNCTIONS
|
||||
|
||||
internal BaseArena *find_arena_from_pointer(const Arena *arena, void *ptr) {
|
||||
if (!arena || !ptr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Ensure pointer is not out of bounds
|
||||
u8 *alloc = (u8 *)ptr;
|
||||
BaseArena *active = arena->active_arena;
|
||||
|
||||
if (alloc > active->buf + arena->initial_capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < arena->count; ++i) {
|
||||
if (alloc >= active->buf && alloc < active->buf + arena->initial_capacity) {
|
||||
return (BaseArena *)active;
|
||||
}
|
||||
|
||||
active = active->prev;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
internal bool base_arena_init(BaseArena *arena, u64 capacity) {
|
||||
if (!arena || arena->buf) {
|
||||
if (!arena || arena->buf || capacity == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
u64 alloc_size = sizeof(u8) * capacity;
|
||||
u64 arena_capacity =
|
||||
capacity >= ARENA_MINIMUM_CAPACITY ? capacity : ARENA_MINIMUM_CAPACITY;
|
||||
|
||||
arena->buf = (u8 *)malloc(alloc_size);
|
||||
arena->buf = (u8 *)calloc(arena_capacity, sizeof(u8));
|
||||
if (!(arena->buf)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(arena->buf, 0, alloc_size);
|
||||
arena->capacity = capacity;
|
||||
arena->capacity = arena_capacity;
|
||||
arena->offset = arena->buf;
|
||||
arena->prev = arena->next = NULL;
|
||||
|
||||
@@ -196,12 +305,23 @@ internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u8 *output = mem_util_align_forward((void *)(arena->offset), alignment);
|
||||
u8 *start_offset = arena->offset;
|
||||
u8 *alloc_start = arena->offset + sizeof(ArenaAllocHDR);
|
||||
|
||||
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||
if (output + size >= arena->buf + arena->capacity) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
arena->offset += size;
|
||||
ArenaAllocHDR *header = (ArenaAllocHDR *)start_offset;
|
||||
*header = (ArenaAllocHDR){
|
||||
.magic = HDR_MAGIC,
|
||||
.alloc_size = size,
|
||||
.alignment = alignment,
|
||||
.alloc_start = output,
|
||||
};
|
||||
|
||||
arena->offset = output + size;
|
||||
|
||||
return (void *)output;
|
||||
}
|
||||
@@ -228,3 +348,59 @@ internal void base_arena_free(BaseArena *arena) {
|
||||
arena->capacity = 0;
|
||||
arena->prev = arena->next = NULL;
|
||||
}
|
||||
|
||||
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr) {
|
||||
persistent const u8 magic[HDR_MAGIC_BYTE_COUNT] = HDR_MAGIC;
|
||||
|
||||
u8 *current = (u8 *)alloc_ptr;
|
||||
|
||||
u8 *max_search_end = current - MAX_HDR_SEARCH_LENGTH;
|
||||
u8 *arena_buf_start = arena->buf;
|
||||
|
||||
u8 *search_end =
|
||||
max_search_end > arena_buf_start ? max_search_end : arena_buf_start;
|
||||
|
||||
bool match;
|
||||
for (; current >= search_end; --current) {
|
||||
match = true;
|
||||
|
||||
for (u64 i = 0; i < HDR_MAGIC_BYTE_COUNT; ++i) {
|
||||
if (current[i] != magic[i]) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match) {
|
||||
return (ArenaAllocHDR *)current;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
internal void *mem_arena_alloc(u64 size, void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
|
||||
return wapp_mem_arena_alloc(arena, size);
|
||||
}
|
||||
|
||||
internal void *mem_arena_alloc_aligned(u64 size, u64 alignment,
|
||||
void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
|
||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||
}
|
||||
|
||||
internal void *mem_arena_realloc(void *ptr, u64 size, void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
|
||||
return wapp_mem_arena_realloc(arena, ptr, size);
|
||||
}
|
||||
|
||||
internal void *mem_arena_realloc_aligned(void *ptr, u64 size, u64 alignment,
|
||||
void *alloc_obj) {
|
||||
Arena *arena = (Arena *)alloc_obj;
|
||||
|
||||
return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment);
|
||||
}
|
||||
|
106
mem/src/ctx/mem_ctx.c
Normal file
106
mem/src/ctx/mem_ctx.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "mem_ctx.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_arena.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct mem_ctx MemCTX;
|
||||
struct mem_ctx {
|
||||
Arena *main;
|
||||
Arena *temp;
|
||||
bool main_initialised;
|
||||
bool temp_initialised;
|
||||
};
|
||||
|
||||
internal MemCTX g_context = {0};
|
||||
|
||||
internal Arena *get_arena(CTXDestBuffer buffer);
|
||||
|
||||
Allocator wapp_mem_ctx_allocator(CTXDestBuffer buffer) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
return wapp_mem_arena_allocator(arena);
|
||||
}
|
||||
|
||||
void wapp_mem_ctx_init(u64 main_buf_capacity, u64 temp_buf_capacity) {
|
||||
g_context.main_initialised =
|
||||
wapp_mem_arena_init(&g_context.main, main_buf_capacity);
|
||||
g_context.temp_initialised =
|
||||
wapp_mem_arena_init(&g_context.temp, temp_buf_capacity);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_alloc(CTXDestBuffer buffer, u64 size) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wapp_mem_arena_alloc(arena, size);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size,
|
||||
u64 alignment) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
wapp_mem_arena_clear(arena);
|
||||
}
|
||||
|
||||
void wapp_mem_ctx_free(void) {
|
||||
wapp_mem_arena_free(&(g_context.main));
|
||||
g_context.main_initialised = false;
|
||||
|
||||
wapp_mem_arena_free(&(g_context.temp));
|
||||
g_context.temp_initialised = false;
|
||||
}
|
||||
|
||||
internal Arena *get_arena(CTXDestBuffer buffer) {
|
||||
Arena *output = NULL;
|
||||
|
||||
switch (buffer) {
|
||||
case CTX_DEST_BUFFER_MAIN:
|
||||
if (g_context.main_initialised) {
|
||||
output = g_context.main;
|
||||
}
|
||||
break;
|
||||
case CTX_DEST_BUFFER_TEMP:
|
||||
if (g_context.temp_initialised) {
|
||||
output = g_context.temp;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
assert(false && "Not all context destination buffers are handled");
|
||||
break;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
48
mem/src/libc/mem_libc.c
Normal file
48
mem/src/libc/mem_libc.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#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) {
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
internal void mem_libc_free(void **ptr, void *alloc_obj) {
|
||||
if (!ptr || !(*ptr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(*ptr);
|
||||
*ptr = NULL;
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
|
||||
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }
|
||||
|
||||
void *mem_util_align_forward(void *ptr, u64 alignment) {
|
||||
void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
|
||||
if (!ptr) {
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user