Compare commits
49 Commits
f1b29d1c3f
...
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 | |||
| b8db582098 | |||
| a169fe3654 | |||
| 9513c05b75 | |||
| 633632105b | |||
| 5a0aad5d52 | |||
| 819d457048 | |||
| 5cbe7f2612 | |||
| 2be4536850 | |||
| 7e8de5f5fb | |||
| 293b16af31 | |||
| 0a82702503 | |||
| 36a4c04a5b | |||
| ad5d76d3cc | |||
| d5c19e2c58 | |||
| fc951c8bd8 | |||
| 75acf7da5d |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.cache
|
||||||
|
.vscode
|
||||||
|
compile_commands.json
|
||||||
|
libwapp.so
|
||||||
@@ -17,11 +17,14 @@
|
|||||||
#define f64 double
|
#define f64 double
|
||||||
#define f128 long double
|
#define f128 long double
|
||||||
|
|
||||||
#define INTERNAL static
|
#define uptr uintptr_t
|
||||||
#define PERSISTENT static
|
#define iptr intptr_t
|
||||||
|
|
||||||
|
#define internal static
|
||||||
|
#define persistent static
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#define CLASS_MEMBER static
|
#define class_mem static
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
#endif // !ALIASES_H
|
#endif // !ALIASES_H
|
||||||
|
|||||||
18
compile
Executable file
18
compile
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
CC=clang
|
||||||
|
INCLUDE="\
|
||||||
|
-Ialiases \
|
||||||
|
-Icpath/include \
|
||||||
|
-Idstr/include \
|
||||||
|
$(find mem/include -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
|
"
|
||||||
|
SRC="\
|
||||||
|
cpath/src/*.c \
|
||||||
|
dstr/src/*.c \
|
||||||
|
mem/src/*/*.c \
|
||||||
|
"
|
||||||
|
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
||||||
|
OUT="libwapp.so"
|
||||||
|
|
||||||
|
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)
|
||||||
@@ -3,16 +3,23 @@
|
|||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
#define NUMPARTS(...) \
|
#define NUMPARTS(...) \
|
||||||
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
||||||
|
|
||||||
#define JOIN_PATH(DST, ...) join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
|
#define wapp_cpath_join_path(DST, ...) \
|
||||||
|
join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
|
||||||
|
#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 join_path(char *dst, u64 count, ...);
|
||||||
|
|
||||||
#define DIRNAME(DST, PATH) dirup(DST, 1, PATH)
|
|
||||||
#define DIRUP(DST, COUNT, PATH) dirup(DST, COUNT, PATH)
|
|
||||||
|
|
||||||
void dirup(char *dst, u64 levels, const char *path);
|
void dirup(char *dst, u64 levels, const char *path);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
#endif // !PATH_UTILS_H
|
#endif // !PATH_UTILS_H
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)
|
#if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||||
INTERNAL char path_sep = '/';
|
internal char path_sep = '/';
|
||||||
#elif defined(_WIN32) || defined(_WIN64)
|
#elif defined(_WIN32) || defined(_WIN64)
|
||||||
INTERNAL char path_sep = '\\';
|
internal char path_sep = '\\';
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void join_root_and_leaf(const char *root, const char *leaf, char *dst);
|
void join_root_and_leaf(const char *root, const char *leaf, char *dst);
|
||||||
@@ -32,7 +32,10 @@ void dirup(char *dst, u64 levels, const char *path) {
|
|||||||
u64 end_index = 0;
|
u64 end_index = 0;
|
||||||
u64 sep_count = 0;
|
u64 sep_count = 0;
|
||||||
|
|
||||||
u64 length = strlen(path);
|
u64 full_length;
|
||||||
|
u64 length;
|
||||||
|
length = full_length = strlen(path);
|
||||||
|
|
||||||
if (path[length - 1] == path_sep) {
|
if (path[length - 1] == path_sep) {
|
||||||
--length;
|
--length;
|
||||||
}
|
}
|
||||||
@@ -52,7 +55,13 @@ void dirup(char *dst, u64 levels, const char *path) {
|
|||||||
end_index = 0;
|
end_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(dst, path, end_index);
|
if (dst == path) {
|
||||||
|
memset(&dst[end_index], 0, full_length - end_index);
|
||||||
|
} else {
|
||||||
|
u64 dst_length = strlen(dst);
|
||||||
|
memset(dst, 0, dst_length);
|
||||||
|
strncpy(dst, path, end_index);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void join_root_and_leaf(const char *root, const char *leaf, char *dst) {
|
void join_root_and_leaf(const char *root, const char *leaf, char *dst) {
|
||||||
|
|||||||
@@ -2,21 +2,30 @@
|
|||||||
#define DSTR_H
|
#define DSTR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
|
|
||||||
typedef struct dstring dstr_t;
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
dstr_t *dstr_with_capacity(u64 capacity);
|
typedef struct dstr String;
|
||||||
dstr_t *dstr_from_string(const char *str);
|
|
||||||
void dstr_update(dstr_t **dst, const char *src);
|
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator);
|
||||||
void dstr_free(dstr_t **str);
|
String *wapp_dstr_from_string(const char *str, const Allocator *allocator);
|
||||||
void dstr_concat(dstr_t **dst, const char *src);
|
void wapp_dstr_update(String **dst, const char *src);
|
||||||
void dstr_append(dstr_t **dst, char c);
|
void wapp_dstr_free(String **str);
|
||||||
void dstr_resize(dstr_t **str);
|
void wapp_dstr_concat(String **dst, const char *src);
|
||||||
void dstr_clear(dstr_t *str);
|
void wapp_dstr_append(String **dst, char c);
|
||||||
void dstr_print(const dstr_t *str);
|
void wapp_dstr_resize(String **str);
|
||||||
i64 dstr_find(const dstr_t *str, const char *substr);
|
void wapp_dstr_clear(String *str);
|
||||||
u64 dstr_length(const dstr_t *str);
|
void wapp_dstr_print(const String *str);
|
||||||
u64 dstr_capacity(const dstr_t *str);
|
i64 wapp_dstr_find(const String *str, const char *substr);
|
||||||
const char *dstr_to_cstr(const dstr_t *str);
|
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
|
#endif // !DSTR_H
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "dstr.h"
|
#include "dstr.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
|
#include "mem_libc.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -8,27 +10,35 @@
|
|||||||
// constantly reallocate
|
// constantly reallocate
|
||||||
#define CAPACITY_SCALAR 8
|
#define CAPACITY_SCALAR 8
|
||||||
|
|
||||||
struct dstring {
|
struct dstr {
|
||||||
|
Allocator allocator;
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
u64 size;
|
u64 size;
|
||||||
char buf[];
|
char buf[];
|
||||||
};
|
};
|
||||||
|
|
||||||
dstr_t *dstr_with_capacity(u64 capacity) {
|
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator) {
|
||||||
dstr_t *out = (dstr_t *)malloc(sizeof(dstr_t) + capacity + 1);
|
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) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out->allocator = alloc;
|
||||||
out->capacity = capacity;
|
out->capacity = capacity;
|
||||||
out->size = 0;
|
out->size = 0;
|
||||||
memset(out->buf, 0, capacity + 1);
|
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
dstr_t *dstr_from_string(const char *str) {
|
String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -37,66 +47,64 @@ dstr_t *dstr_from_string(const char *str) {
|
|||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
dstr_t *out = dstr_with_capacity(capacity);
|
String *out = wapp_dstr_with_capacity(capacity, allocator);
|
||||||
|
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->size = length;
|
out->size = length;
|
||||||
strncpy(out->buf, str, length);
|
strncpy(out->buf, str, length + 1);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_update(dstr_t **dst, const char *src) {
|
void wapp_dstr_update(String **dst, const char *src) {
|
||||||
if (!dst || !(*dst)) {
|
if (!dst || !(*dst)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 length = strlen(src);
|
u64 length = strlen(src);
|
||||||
|
|
||||||
dstr_t *str = *dst;
|
String *str = *dst;
|
||||||
|
|
||||||
if (length <= str->capacity) {
|
if (length < str->capacity) {
|
||||||
memset(str->buf, 0, str->capacity);
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
str->size = length;
|
str->size = length;
|
||||||
|
|
||||||
strncpy(str->buf, src, length);
|
strncpy(str->buf, src, length + 1);
|
||||||
} else {
|
} else {
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
|
||||||
dstr_t *tmp = (dstr_t *)realloc(*dst, sizeof(dstr_t) + capacity + 1);
|
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||||
|
&(str->allocator), *dst, sizeof(String) + capacity + 1);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
tmp->capacity = capacity;
|
||||||
tmp->size = length;
|
tmp->size = length;
|
||||||
strncpy(tmp->buf, src, length);
|
strncpy(tmp->buf, src, length + 1);
|
||||||
|
|
||||||
*dst = tmp;
|
*dst = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_free(dstr_t **str) {
|
void wapp_dstr_free(String **str) {
|
||||||
if (!str || !(*str)) {
|
if (!str || !(*str)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(*str);
|
String *str_ptr = *str;
|
||||||
*str = NULL;
|
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_concat(dstr_t **dst, const char *src) {
|
void wapp_dstr_concat(String **dst, const char *src) {
|
||||||
if (!dst || !(*dst)) {
|
if (!dst || !(*dst)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 src_length = strlen(src);
|
u64 src_length = strlen(src);
|
||||||
|
|
||||||
if (src_length == 0) {
|
if (src_length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -107,12 +115,12 @@ void dstr_concat(dstr_t **dst, const char *src) {
|
|||||||
memset(str, 0, new_length + 1);
|
memset(str, 0, new_length + 1);
|
||||||
|
|
||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
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(dstr_t **dst, char c) {
|
void wapp_dstr_append(String **dst, char c) {
|
||||||
if (!dst || !(*dst)) {
|
if (!dst || !(*dst)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -125,18 +133,19 @@ void dstr_append(dstr_t **dst, char c) {
|
|||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
str[(*dst)->size] = c;
|
str[(*dst)->size] = c;
|
||||||
|
|
||||||
dstr_update(dst, str);
|
wapp_dstr_update(dst, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_resize(dstr_t **str) {
|
void wapp_dstr_resize(String **str) {
|
||||||
if (!str || !(*str)) {
|
if (!str || !(*str)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String *str_ptr = *str;
|
||||||
u64 capacity = (*str)->size;
|
u64 capacity = (*str)->size;
|
||||||
|
|
||||||
dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1);
|
String *tmp = (String *)wapp_mem_allocator_realloc(
|
||||||
|
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -146,7 +155,7 @@ void dstr_resize(dstr_t **str) {
|
|||||||
*str = tmp;
|
*str = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_clear(dstr_t *str) {
|
void wapp_dstr_clear(String *str) {
|
||||||
if (!str || str->size == 0) {
|
if (!str || str->size == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -155,38 +164,33 @@ void dstr_clear(dstr_t *str) {
|
|||||||
str->size = 0;
|
str->size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dstr_print(const dstr_t *str) {
|
void wapp_dstr_print(const String *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", str->buf);
|
printf("%.*s\n", (i32)str->size, str->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
i64 dstr_find(const dstr_t *str, const char *substr) {
|
i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||||
if (!str || !substr) {
|
if (!str || !substr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 substr_length = strlen(substr);
|
u64 substr_length = strlen(substr);
|
||||||
|
|
||||||
if (substr_length == 0 || substr_length > str->size) {
|
if (substr_length == 0 || substr_length > str->size) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[substr_length + 1];
|
const char *s1;
|
||||||
memset(buf, 0, substr_length + 1);
|
|
||||||
|
|
||||||
for (u64 i = 0; i < str->size; ++i) {
|
for (u64 i = 0; i < str->size; ++i) {
|
||||||
if (i + substr_length >= str->size) {
|
if (i + substr_length > str->size) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (u64 j = 0; j < substr_length; ++j) {
|
s1 = &(str->buf[i]);
|
||||||
buf[j] = str->buf[i + j];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(buf, substr) == 0) {
|
if (strncmp(s1, substr, substr_length) == 0) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -194,7 +198,7 @@ i64 dstr_find(const dstr_t *str, const char *substr) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 dstr_length(const dstr_t *str) {
|
u64 wapp_dstr_length(const String *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -202,7 +206,7 @@ u64 dstr_length(const dstr_t *str) {
|
|||||||
return str->size;
|
return str->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 dstr_capacity(const dstr_t *str) {
|
u64 wapp_dstr_capacity(const String *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -210,7 +214,7 @@ u64 dstr_capacity(const dstr_t *str) {
|
|||||||
return str->capacity;
|
return str->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *dstr_to_cstr(const dstr_t *str) {
|
const char *wapp_dstr_to_cstr(const String *str) {
|
||||||
if (!str) {
|
if (!str) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
INCLUDE=" \
|
|
||||||
-Ialiases \
|
|
||||||
-Icpath/include \
|
|
||||||
-Idstr/include \
|
|
||||||
"
|
|
||||||
|
|
||||||
SRC=" \
|
|
||||||
cpath/src/*.c \
|
|
||||||
dstr/src/*.c \
|
|
||||||
"
|
|
||||||
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
|
||||||
28
mem/include/arena/mem_arena.h
Normal file
28
mem/include/arena/mem_arena.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef MEM_ARENA_H
|
||||||
|
#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;
|
||||||
|
|
||||||
|
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
|
||||||
16
mem/include/util/mem_utils.h
Normal file
16
mem/include/util/mem_utils.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef MEM_UTILS_H
|
||||||
|
#define MEM_UTILS_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
406
mem/src/arena/mem_arena.c
Normal file
406
mem/src/arena/mem_arena.c
Normal file
@@ -0,0 +1,406 @@
|
|||||||
|
#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;
|
||||||
|
u8 *offset;
|
||||||
|
u64 capacity;
|
||||||
|
BaseArena *prev;
|
||||||
|
BaseArena *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct growing_arena {
|
||||||
|
BaseArena *active_arena;
|
||||||
|
u64 count;
|
||||||
|
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
|
||||||
|
|
||||||
|
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 *)calloc(1, sizeof(Arena));
|
||||||
|
Arena *arena_ptr = *arena;
|
||||||
|
if (!arena_ptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||||
|
if (!(arena_ptr->active_arena)) {
|
||||||
|
wapp_mem_arena_free(arena);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
||||||
|
wapp_mem_arena_free(arena);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena_ptr->count = 1;
|
||||||
|
arena_ptr->initial_capacity = base_capacity;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *wapp_mem_arena_alloc(Arena *arena, u64 size) {
|
||||||
|
return wapp_mem_arena_alloc_aligned(arena, size, DEFAULT_ALIGNMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
||||||
|
if (!arena || !(arena->active_arena)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *output = base_arena_alloc_aligned(arena->active_arena, size, alignment);
|
||||||
|
if (!output) {
|
||||||
|
if (arena->active_arena->next) {
|
||||||
|
arena->active_arena = arena->active_arena->next;
|
||||||
|
} else {
|
||||||
|
arena->active_arena->next = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||||
|
if (!(arena->active_arena->next)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!base_arena_init(arena->active_arena->next,
|
||||||
|
arena->initial_capacity)) {
|
||||||
|
free(arena->active_arena->next);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena->active_arena->next->prev = arena->active_arena;
|
||||||
|
arena->active_arena = arena->active_arena->next;
|
||||||
|
|
||||||
|
++(arena->count);
|
||||||
|
}
|
||||||
|
|
||||||
|
output = base_arena_alloc_aligned(arena->active_arena, size, alignment);
|
||||||
|
if (!output) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(output, 0, size);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 *last_active = NULL;
|
||||||
|
while (arena->active_arena) {
|
||||||
|
base_arena_clear(arena->active_arena);
|
||||||
|
|
||||||
|
last_active = arena->active_arena;
|
||||||
|
|
||||||
|
arena->active_arena = arena->active_arena->prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena->active_arena = last_active;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_mem_arena_free(Arena **arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Arena *arena_ptr = *arena;
|
||||||
|
if (!arena_ptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseArena *current;
|
||||||
|
BaseArena *next;
|
||||||
|
BaseArena *prev;
|
||||||
|
|
||||||
|
current = arena_ptr->active_arena->next;
|
||||||
|
while (current) {
|
||||||
|
next = current->next;
|
||||||
|
|
||||||
|
base_arena_free(current);
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = arena_ptr->active_arena->prev;
|
||||||
|
while (current) {
|
||||||
|
prev = current->prev;
|
||||||
|
|
||||||
|
base_arena_free(current);
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
current = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_arena_free(arena_ptr->active_arena);
|
||||||
|
|
||||||
|
free(arena_ptr->active_arena);
|
||||||
|
arena_ptr->active_arena = NULL;
|
||||||
|
|
||||||
|
arena_ptr->count = 0;
|
||||||
|
arena_ptr->initial_capacity = 0;
|
||||||
|
|
||||||
|
free(*arena);
|
||||||
|
*arena = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 || capacity == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 arena_capacity =
|
||||||
|
capacity >= ARENA_MINIMUM_CAPACITY ? capacity : ARENA_MINIMUM_CAPACITY;
|
||||||
|
|
||||||
|
arena->buf = (u8 *)calloc(arena_capacity, sizeof(u8));
|
||||||
|
if (!(arena->buf)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
arena->capacity = arena_capacity;
|
||||||
|
arena->offset = arena->buf;
|
||||||
|
arena->prev = arena->next = NULL;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
||||||
|
u64 alignment) {
|
||||||
|
if (!arena) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void base_arena_clear(BaseArena *arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(arena->buf, 0, arena->offset - arena->buf);
|
||||||
|
arena->offset = arena->buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void base_arena_free(BaseArena *arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arena->buf) {
|
||||||
|
free(arena->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
arena->buf = arena->offset = NULL;
|
||||||
|
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;
|
||||||
|
}
|
||||||
28
mem/src/util/mem_utils.c
Normal file
28
mem/src/util/mem_utils.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "mem_utils.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
internal bool is_power_of_two(u64 num) { return (num & (num - 1)) == 0; }
|
||||||
|
|
||||||
|
void *wapp_mem_util_align_forward(void *ptr, u64 alignment) {
|
||||||
|
if (!ptr) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(is_power_of_two(alignment));
|
||||||
|
|
||||||
|
uptr p = (uptr)ptr;
|
||||||
|
uptr align = (uptr)alignment;
|
||||||
|
|
||||||
|
// Similar to p % align, but it's a faster implementation that works fine
|
||||||
|
// because align is guaranteed to be a power of 2
|
||||||
|
uptr modulo = p & (align - 1);
|
||||||
|
|
||||||
|
if (modulo != 0) {
|
||||||
|
p += align - modulo;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (void *)p;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user