Compare commits
14 Commits
ca36e0dc35
...
mem_contex
Author | SHA1 | Date | |
---|---|---|---|
6195b521f5 | |||
be64571b0e | |||
645686ae22 | |||
3f9a908860 | |||
2e93bd794a | |||
39c88505bd | |||
970c588d66 | |||
|
efe8a104de | ||
|
6f799c4330 | ||
|
8a58a1cc48 | ||
|
03d2a59948 | ||
|
5b8e9f8be6 | ||
|
9ec123c41b | ||
|
7039b5437a |
@@ -2,6 +2,7 @@
|
||||
#define DSTR_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -9,8 +10,8 @@ extern "C" {
|
||||
|
||||
typedef struct dstr String;
|
||||
|
||||
String *wapp_dstr_with_capacity(u64 capacity);
|
||||
String *wapp_dstr_from_string(const char *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);
|
||||
|
@@ -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,24 +11,34 @@
|
||||
#define CAPACITY_SCALAR 8
|
||||
|
||||
struct dstr {
|
||||
Allocator allocator;
|
||||
u64 capacity;
|
||||
u64 size;
|
||||
char buf[];
|
||||
};
|
||||
|
||||
String *wapp_dstr_with_capacity(u64 capacity) {
|
||||
String *out = (String *)calloc(1, 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;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
String *wapp_dstr_from_string(const char *str) {
|
||||
String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -35,14 +47,13 @@ String *wapp_dstr_from_string(const char *str) {
|
||||
|
||||
u64 capacity = length * CAPACITY_SCALAR;
|
||||
|
||||
String *out = wapp_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;
|
||||
}
|
||||
@@ -56,24 +67,24 @@ void wapp_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;
|
||||
}
|
||||
@@ -84,8 +95,8 @@ void wapp_dstr_free(String **str) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
String *str_ptr = *str;
|
||||
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
||||
}
|
||||
|
||||
void wapp_dstr_concat(String **dst, const char *src) {
|
||||
@@ -94,7 +105,6 @@ void wapp_dstr_concat(String **dst, const char *src) {
|
||||
}
|
||||
|
||||
u64 src_length = strlen(src);
|
||||
|
||||
if (src_length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -105,7 +115,7 @@ void wapp_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);
|
||||
|
||||
wapp_dstr_update(dst, str);
|
||||
}
|
||||
@@ -131,10 +141,11 @@ void wapp_dstr_resize(String **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;
|
||||
}
|
||||
@@ -158,7 +169,7 @@ void wapp_dstr_print(const String *str) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%s\n", str->buf);
|
||||
printf("%.*s\n", (i32)str->size, str->buf);
|
||||
}
|
||||
|
||||
i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||
@@ -167,24 +178,19 @@ i64 wapp_dstr_find(const String *str, const char *substr) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
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,6 +2,7 @@
|
||||
#define MEM_ARENA_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -10,6 +11,7 @@ extern "C" {
|
||||
|
||||
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);
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#ifndef MEM_CTX_H
|
||||
#define MEM_CTX_H
|
||||
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_arena.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -14,9 +15,13 @@ typedef enum {
|
||||
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);
|
||||
|
||||
|
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
|
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,5 +1,6 @@
|
||||
#include "mem_arena.h"
|
||||
#include "aliases.h"
|
||||
#include "mem_allocator.h"
|
||||
#include "mem_utils.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
@@ -17,6 +18,8 @@
|
||||
{ 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];
|
||||
@@ -49,8 +52,26 @@ 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;
|
||||
@@ -263,12 +284,15 @@ internal bool base_arena_init(BaseArena *arena, u64 capacity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
arena->buf = (u8 *)calloc(capacity, sizeof(u8));
|
||||
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 = capacity;
|
||||
arena->capacity = arena_capacity;
|
||||
arena->offset = arena->buf;
|
||||
arena->prev = arena->next = NULL;
|
||||
|
||||
@@ -337,7 +361,7 @@ internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr) {
|
||||
max_search_end > arena_buf_start ? max_search_end : arena_buf_start;
|
||||
|
||||
bool match;
|
||||
for (; current >= search_end; --current) {
|
||||
for (; current >= search_end; --current) {
|
||||
match = true;
|
||||
|
||||
for (u64 i = 0; i < HDR_MAGIC_BYTE_COUNT; ++i) {
|
||||
@@ -350,7 +374,33 @@ internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr) {
|
||||
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);
|
||||
}
|
||||
|
@@ -16,6 +16,11 @@ 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);
|
||||
@@ -42,6 +47,25 @@ void *wapp_mem_ctx_alloc_aligned(CTXDestBuffer buffer, u64 size,
|
||||
return wapp_mem_arena_alloc_aligned(arena, size, alignment);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_realloc(CTXDestBuffer buffer, void *ptr, u64 size) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wapp_mem_arena_realloc(arena, ptr, size);
|
||||
}
|
||||
|
||||
void *wapp_mem_ctx_realloc_aligned(CTXDestBuffer buffer, void *ptr, u64 size,
|
||||
u64 alignment) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return wapp_mem_arena_realloc_aligned(arena, ptr, size, alignment);
|
||||
}
|
||||
|
||||
void wapp_mem_ctx_clear(CTXDestBuffer buffer) {
|
||||
Arena *arena = get_arena(buffer);
|
||||
if (!arena) {
|
||||
|
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;
|
||||
}
|
Reference in New Issue
Block a user