Compare commits
60 Commits
f1b29d1c3f
...
growing_ar
| Author | SHA1 | Date | |
|---|---|---|---|
| b59aedad89 | |||
| 3f5e3558b9 | |||
| bb2db0d30d | |||
| e75846a507 | |||
| 05e56d67ea | |||
| 7164156c35 | |||
| e67e1df9a5 | |||
| e206b4f4a6 | |||
| 05bfa73509 | |||
| d4313fb8e4 | |||
| 92db9206cc | |||
| 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
|
||||||
13
compile
Executable file
13
compile
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
CC=clang
|
||||||
|
INCLUDE="\
|
||||||
|
$(find src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
|
"
|
||||||
|
SRC="\
|
||||||
|
$(find src -type f -name *.c | xargs -I{} echo -n "{} ") \
|
||||||
|
"
|
||||||
|
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
||||||
|
OUT="libwapp.so"
|
||||||
|
|
||||||
|
(set -x ; $CC $CFLAGS $INCLUDE $SRC -o $OUT)
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef PATH_UTILS_H
|
|
||||||
#define PATH_UTILS_H
|
|
||||||
|
|
||||||
#include "aliases.h"
|
|
||||||
|
|
||||||
#define NUMPARTS(...) \
|
|
||||||
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
|
||||||
|
|
||||||
#define JOIN_PATH(DST, ...) join_path(DST, NUMPARTS(__VA_ARGS__), __VA_ARGS__)
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
#endif // !PATH_UTILS_H
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
#ifndef DSTR_H
|
|
||||||
#define DSTR_H
|
|
||||||
|
|
||||||
#include "aliases.h"
|
|
||||||
|
|
||||||
typedef struct dstring dstr_t;
|
|
||||||
|
|
||||||
dstr_t *dstr_with_capacity(u64 capacity);
|
|
||||||
dstr_t *dstr_from_string(const char *str);
|
|
||||||
void dstr_update(dstr_t **dst, const char *src);
|
|
||||||
void dstr_free(dstr_t **str);
|
|
||||||
void dstr_concat(dstr_t **dst, const char *src);
|
|
||||||
void dstr_append(dstr_t **dst, char c);
|
|
||||||
void dstr_resize(dstr_t **str);
|
|
||||||
void dstr_clear(dstr_t *str);
|
|
||||||
void dstr_print(const dstr_t *str);
|
|
||||||
i64 dstr_find(const dstr_t *str, const char *substr);
|
|
||||||
u64 dstr_length(const dstr_t *str);
|
|
||||||
u64 dstr_capacity(const dstr_t *str);
|
|
||||||
const char *dstr_to_cstr(const dstr_t *str);
|
|
||||||
|
|
||||||
#endif // !DSTR_H
|
|
||||||
219
dstr/src/dstr.c
219
dstr/src/dstr.c
@@ -1,219 +0,0 @@
|
|||||||
#include "dstr.h"
|
|
||||||
#include "aliases.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
// Use this scalar to allocate extra memory in order to avoid having to
|
|
||||||
// constantly reallocate
|
|
||||||
#define CAPACITY_SCALAR 8
|
|
||||||
|
|
||||||
struct dstring {
|
|
||||||
u64 capacity;
|
|
||||||
u64 size;
|
|
||||||
char buf[];
|
|
||||||
};
|
|
||||||
|
|
||||||
dstr_t *dstr_with_capacity(u64 capacity) {
|
|
||||||
dstr_t *out = (dstr_t *)malloc(sizeof(dstr_t) + capacity + 1);
|
|
||||||
|
|
||||||
if (!out) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->capacity = capacity;
|
|
||||||
out->size = 0;
|
|
||||||
memset(out->buf, 0, capacity + 1);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
dstr_t *dstr_from_string(const char *str) {
|
|
||||||
if (!str) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 length = strlen(str);
|
|
||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
|
||||||
|
|
||||||
dstr_t *out = dstr_with_capacity(capacity);
|
|
||||||
|
|
||||||
if (!out) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
out->size = length;
|
|
||||||
strncpy(out->buf, str, length);
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_update(dstr_t **dst, const char *src) {
|
|
||||||
if (!dst || !(*dst)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 length = strlen(src);
|
|
||||||
|
|
||||||
dstr_t *str = *dst;
|
|
||||||
|
|
||||||
if (length <= str->capacity) {
|
|
||||||
memset(str->buf, 0, str->capacity);
|
|
||||||
|
|
||||||
str->size = length;
|
|
||||||
|
|
||||||
strncpy(str->buf, src, length);
|
|
||||||
} else {
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
|
||||||
|
|
||||||
dstr_t *tmp = (dstr_t *)realloc(*dst, sizeof(dstr_t) + capacity + 1);
|
|
||||||
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
|
||||||
tmp->size = length;
|
|
||||||
strncpy(tmp->buf, src, length);
|
|
||||||
|
|
||||||
*dst = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_free(dstr_t **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(*str);
|
|
||||||
*str = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_concat(dstr_t **dst, const char *src) {
|
|
||||||
if (!dst || !(*dst)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 src_length = strlen(src);
|
|
||||||
|
|
||||||
if (src_length == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 new_length = (*dst)->size + src_length;
|
|
||||||
|
|
||||||
char str[new_length + 1];
|
|
||||||
memset(str, 0, new_length + 1);
|
|
||||||
|
|
||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
|
||||||
strncat(str, src, src_length);
|
|
||||||
|
|
||||||
dstr_update(dst, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_append(dstr_t **dst, char c) {
|
|
||||||
if (!dst || !(*dst)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 new_length = (*dst)->size + 1;
|
|
||||||
|
|
||||||
char str[new_length + 1];
|
|
||||||
memset(str, 0, new_length + 1);
|
|
||||||
|
|
||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
|
||||||
str[(*dst)->size] = c;
|
|
||||||
|
|
||||||
dstr_update(dst, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_resize(dstr_t **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 capacity = (*str)->size;
|
|
||||||
|
|
||||||
dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1);
|
|
||||||
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
|
||||||
|
|
||||||
*str = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_clear(dstr_t *str) {
|
|
||||||
if (!str || str->size == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(str->buf, 0, str->capacity);
|
|
||||||
str->size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dstr_print(const dstr_t *str) {
|
|
||||||
if (!str) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n", str->buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
i64 dstr_find(const dstr_t *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);
|
|
||||||
|
|
||||||
for (u64 i = 0; i < str->size; ++i) {
|
|
||||||
if (i + substr_length >= str->size) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (u64 j = 0; j < substr_length; ++j) {
|
|
||||||
buf[j] = str->buf[i + j];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(buf, substr) == 0) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 dstr_length(const dstr_t *str) {
|
|
||||||
if (!str) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 dstr_capacity(const dstr_t *str) {
|
|
||||||
if (!str) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *dstr_to_cstr(const dstr_t *str) {
|
|
||||||
if (!str) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return str->buf;
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
INCLUDE=" \
|
|
||||||
-Ialiases \
|
|
||||||
-Icpath/include \
|
|
||||||
-Idstr/include \
|
|
||||||
"
|
|
||||||
|
|
||||||
SRC=" \
|
|
||||||
cpath/src/*.c \
|
|
||||||
dstr/src/*.c \
|
|
||||||
"
|
|
||||||
@@ -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
|
||||||
@@ -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) {
|
||||||
25
src/cpath/cpath.h
Normal file
25
src/cpath/cpath.h
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef PATH_UTILS_H
|
||||||
|
#define PATH_UTILS_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#define NUMPARTS(...) \
|
||||||
|
(sizeof((const char *[]){"", __VA_ARGS__}) / sizeof(const char *) - 1)
|
||||||
|
|
||||||
|
#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 dirup(char *dst, u64 levels, const char *path);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !PATH_UTILS_H
|
||||||
233
src/mem/arena/mem_arena.c
Normal file
233
src/mem/arena/mem_arena.c
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
#include "mem_arena.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_utils.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 ARENA_MINIMUM_CAPACITY 1024
|
||||||
|
|
||||||
|
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 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_destroy(BaseArena *arena);
|
||||||
|
|
||||||
|
// PUBLIC API
|
||||||
|
|
||||||
|
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_destroy(arena);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
||||||
|
wapp_mem_arena_destroy(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_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_destroy(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_destroy(current);
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = arena_ptr->active_arena->prev;
|
||||||
|
while (current) {
|
||||||
|
prev = current->prev;
|
||||||
|
|
||||||
|
base_arena_destroy(current);
|
||||||
|
free(current);
|
||||||
|
|
||||||
|
current = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
base_arena_destroy(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 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 *alloc_start = arena->offset;
|
||||||
|
|
||||||
|
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||||
|
if (output + size >= arena->buf + arena->capacity) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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_destroy(BaseArena *arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arena->buf) {
|
||||||
|
free(arena->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
arena->buf = arena->offset = NULL;
|
||||||
|
arena->capacity = 0;
|
||||||
|
arena->prev = arena->next = NULL;
|
||||||
|
}
|
||||||
23
src/mem/arena/mem_arena.h
Normal file
23
src/mem/arena/mem_arena.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef MEM_ARENA_H
|
||||||
|
#define MEM_ARENA_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
typedef struct growing_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_clear(Arena *arena);
|
||||||
|
void wapp_mem_arena_destroy(Arena **arena);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !MEM_ARENA_H
|
||||||
28
src/mem/util/mem_utils.c
Normal file
28
src/mem/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;
|
||||||
|
}
|
||||||
16
src/mem/util/mem_utils.h
Normal file
16
src/mem/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
|
||||||
34
src/strings/basic_strings/basic_strings.h
Normal file
34
src/strings/basic_strings/basic_strings.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef BASIC_STRING_H
|
||||||
|
#define BASIC_STRING_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
typedef struct bstr BasicString;
|
||||||
|
struct bstr {
|
||||||
|
u64 size;
|
||||||
|
const char *buf;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct strvw StringView;
|
||||||
|
struct strvw {
|
||||||
|
const u64 size;
|
||||||
|
const char *buf;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define new_string(STR) \
|
||||||
|
{ .size = strlen(STR), .buf = STR }
|
||||||
|
#define wapp_bstr_new(STR) ((BasicString)new_string(STR))
|
||||||
|
#define wapp_strvw_new(STR) ((StringView)new_string(STR))
|
||||||
|
#define wapp_string_print(STR) (printf("%.*s\n", (i32)STR.size, STR.buf))
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
#endif // !BASIC_STRING_H
|
||||||
174
src/strings/dstr/dstr.c
Normal file
174
src/strings/dstr/dstr.c
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
#include "dstr.h"
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Use this scalar to allocate extra memory in order to avoid having to
|
||||||
|
// constantly reallocate
|
||||||
|
#define CAPACITY_SCALAR 8
|
||||||
|
#define MINIMUM_DSTR_CAPACITY 1024
|
||||||
|
|
||||||
|
struct dstr {
|
||||||
|
u64 capacity;
|
||||||
|
u64 size;
|
||||||
|
char buf[];
|
||||||
|
};
|
||||||
|
|
||||||
|
String *wapp_dstr_with_capacity(u64 capacity, Arena *arena) {
|
||||||
|
if (!arena) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
String *out =
|
||||||
|
(String *)wapp_mem_arena_alloc(arena, sizeof(String) + capacity + 1);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->capacity = capacity;
|
||||||
|
out->size = 0;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
String *wapp_dstr_from_string(const char *str, Arena *arena) {
|
||||||
|
if (!str || !arena) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(str);
|
||||||
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
capacity =
|
||||||
|
capacity >= MINIMUM_DSTR_CAPACITY ? capacity : MINIMUM_DSTR_CAPACITY;
|
||||||
|
|
||||||
|
String *out = wapp_dstr_with_capacity(capacity, arena);
|
||||||
|
if (!out) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
out->size = length;
|
||||||
|
strncpy(out->buf, str, length + 1);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena) {
|
||||||
|
if (!dst || !(*dst)) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 length = strlen(src);
|
||||||
|
|
||||||
|
String *str = *dst;
|
||||||
|
|
||||||
|
if (length >= str->capacity) {
|
||||||
|
if (!arena) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
String *new_str = wapp_dstr_from_string(src, arena);
|
||||||
|
if (!new_str) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
return (StringUpdate){.updated = true, .str = new_str};
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
|
str->size = length;
|
||||||
|
|
||||||
|
strncpy(str->buf, src, length + 1);
|
||||||
|
|
||||||
|
return (StringUpdate){.updated = true, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena) {
|
||||||
|
if (!dst || !(*dst)) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 src_length = strlen(src);
|
||||||
|
if (src_length == 0) {
|
||||||
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 new_length = (*dst)->size + src_length;
|
||||||
|
|
||||||
|
char str[new_length + 1];
|
||||||
|
memset(str, 0, new_length + 1);
|
||||||
|
|
||||||
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
|
strncat(str, src, new_length + 1 - (*dst)->size);
|
||||||
|
|
||||||
|
return wapp_dstr_update(dst, str, arena);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_dstr_clear(String *str) {
|
||||||
|
if (!str || str->size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(str->buf, 0, str->capacity);
|
||||||
|
str->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wapp_dstr_print(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%.*s\n", (i32)str->size, str->buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *s1;
|
||||||
|
for (u64 i = 0; i < str->size; ++i) {
|
||||||
|
if (i + substr_length > str->size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
s1 = &(str->buf[i]);
|
||||||
|
|
||||||
|
if (strncmp(s1, substr, substr_length) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 wapp_dstr_length(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 wapp_dstr_capacity(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *wapp_dstr_to_cstr(const String *str) {
|
||||||
|
if (!str) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return str->buf;
|
||||||
|
}
|
||||||
35
src/strings/dstr/dstr.h
Normal file
35
src/strings/dstr/dstr.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef DSTR_H
|
||||||
|
#define DSTR_H
|
||||||
|
|
||||||
|
#include "aliases.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
typedef struct dstr String;
|
||||||
|
|
||||||
|
typedef struct string_update StringUpdate;
|
||||||
|
struct string_update {
|
||||||
|
bool updated;
|
||||||
|
String *str;
|
||||||
|
};
|
||||||
|
|
||||||
|
String *wapp_dstr_with_capacity(u64 capacity, Arena *arena);
|
||||||
|
String *wapp_dstr_from_string(const char *str, Arena *arena);
|
||||||
|
StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena);
|
||||||
|
StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena);
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user