60 Commits

Author SHA1 Message Date
b59aedad89 Add BasicString and StringView 2024-04-29 23:22:59 +01:00
3f5e3558b9 Add BasicString 2024-04-29 23:06:22 +01:00
bb2db0d30d Return StringUpdate from the dstr update functions 2024-04-29 22:55:40 +01:00
e75846a507 Update dstr to use Arena 2024-04-29 22:48:10 +01:00
05e56d67ea Reorganise the code 2024-04-29 21:42:33 +01:00
7164156c35 Simplify Arena 2024-04-28 22:59:37 +01:00
e67e1df9a5 Rename arena free functions 2024-04-28 22:04:23 +01:00
e206b4f4a6 Remove Allocator functions from Arena 2024-04-24 23:40:10 +01:00
05bfa73509 Revert "Simplify Arena allocator"
This reverts commit d4313fb8e4.
2024-04-24 23:18:19 +01:00
d4313fb8e4 Simplify Arena allocator 2024-04-24 23:10:55 +01:00
92db9206cc Remove complicated memory abstractions 2024-04-24 22:51:42 +01:00
6195b521f5 Set minimum capacity for arena 2024-04-21 23:59:00 +01:00
be64571b0e Pass allocator as const * 2024-03-31 17:29:23 +01:00
645686ae22 Remove unused variable 2024-03-31 17:05:04 +01:00
3f9a908860 Address some minor bugs in dstr 2024-03-31 17:00:16 +01:00
2e93bd794a Switch to using Allocator in dstr 2024-03-31 17:00:01 +01:00
39c88505bd Fix bug with libc allocator where memory was set to 0 on reallocation 2024-03-31 16:58:38 +01:00
970c588d66 Pass const Arena* to Allocator constuctor instead of Arena* 2024-03-25 07:52:49 +00:00
Abdelrahman Said
efe8a104de Add allocator functions 2024-03-24 21:06:41 +00:00
Abdelrahman Said
6f799c4330 Remove alloc_obj and pass double pointer to free function 2024-03-24 21:05:32 +00:00
Abdelrahman Said
8a58a1cc48 Implement Allocator wrapper for memory context 2024-03-24 20:30:14 +00:00
Abdelrahman Said
03d2a59948 Implement Allocator wrapper for Arena 2024-03-24 20:25:51 +00:00
Abdelrahman Said
5b8e9f8be6 Wrap libc memory functions in Allocator interface 2024-03-24 19:25:07 +00:00
Abdelrahman Said
9ec123c41b Start implementing allocator abstraction 2024-03-24 19:24:54 +00:00
Abdelrahman Said
7039b5437a Implement memory context realloc function 2024-03-24 19:24:36 +00:00
Abdelrahman Said
ca36e0dc35 Fix bug in the function that searches for the allocation header 2024-03-24 17:24:33 +00:00
Abdelrahman Said
3fdd897291 Implement realloc function for Arena 2024-03-24 14:57:21 +00:00
Abdelrahman Said
08703b465c Add arena allocation header 2024-03-24 13:55:13 +00:00
Abdelrahman Said
49ce26a6c2 Switch to using calloc instead of malloc 2024-03-24 11:25:52 +00:00
feada0b31c Add -O3 to compile flags 2024-03-24 08:10:40 +00:00
Abdelrahman Said
0c07437ef2 Implement memory context with main and temp arenas 2024-03-24 07:59:46 +00:00
Abdelrahman Said
063bc03974 Fix bug with clearing arena function 2024-03-24 07:49:49 +00:00
Abdelrahman Said
09af7ec734 Update .gitignore 2024-03-24 07:21:08 +00:00
Abdelrahman Said
04e86d355d Rename output name in compile script 2024-03-24 07:20:50 +00:00
Abdelrahman Said
7bfaf53d4e Return false when initialising arena with 0 capacity 2024-03-24 07:15:39 +00:00
Abdelrahman Said
958df4b55a Update .gitignore 2024-03-24 07:09:17 +00:00
Abdelrahman Said
9179f9beaa Ensure arena has been allocated before attempting to free 2024-03-24 06:37:02 +00:00
Abdelrahman Said
7aaeb91fe1 Update compile script 2024-03-24 06:21:22 +00:00
Abdelrahman Said
df4315cdcc Use echo instead of printf in compile script 2024-03-24 05:52:29 +00:00
Abdelrahman Said
b3a174f26c Update compile script 2024-03-24 05:45:13 +00:00
6fc0b2987e Add extern "C" to all header files 2024-03-21 23:06:27 +00:00
76222d31d4 Namespace all functions 2024-03-21 07:26:26 +00:00
7948d3fd1a Ensure arena is freed when initialisation fails 2024-02-24 23:01:26 +00:00
1094a9fefb Add comment for default alignment 2024-02-24 22:59:25 +00:00
b8db582098 Implement growing arena 2024-02-24 20:27:06 +00:00
a169fe3654 Return NULL when aligning forward if ptr is NULL 2024-02-24 20:26:37 +00:00
9513c05b75 Handle NULL pointers in base arena 2024-02-24 18:44:43 +00:00
633632105b Implement base arena allocator 2024-02-24 18:40:25 +00:00
5a0aad5d52 Add memory utilities 2024-02-24 18:40:05 +00:00
819d457048 Update build script 2024-02-24 18:39:41 +00:00
5cbe7f2612 Remove example_build 2024-02-24 17:15:35 +00:00
2be4536850 Add aliases for pointer integer types 2024-02-24 15:36:54 +00:00
7e8de5f5fb Rename struct String to struct dstr 2024-02-24 15:04:22 +00:00
293b16af31 Switch to PascalCase datatype names 2024-02-24 15:02:47 +00:00
0a82702503 Add .gitignore 2024-02-24 14:59:22 +00:00
36a4c04a5b Add build scripts for development 2024-02-24 14:59:07 +00:00
ad5d76d3cc Update namespace for cpath functions 2024-02-24 14:51:03 +00:00
d5c19e2c58 Namespace the path functions 2024-02-24 14:47:18 +00:00
fc951c8bd8 Update static aliases to be lower case 2024-02-24 14:46:52 +00:00
75acf7da5d Fix bug with the dirup function 2024-02-24 14:46:32 +00:00
17 changed files with 607 additions and 278 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.cache
.vscode
compile_commands.json
libwapp.so

3
build Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
bear -- ./compile $@

13
compile Executable file
View 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)

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}

View File

@@ -1,12 +0,0 @@
#!/bin/bash
INCLUDE=" \
-Ialiases \
-Icpath/include \
-Idstr/include \
"
SRC=" \
cpath/src/*.c \
dstr/src/*.c \
"

View File

@@ -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

View File

@@ -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
View 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
View 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
View 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
View 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
View 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

View 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
View 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
View 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