Compare commits
7 Commits
e206b4f4a6
...
growing_ar
| Author | SHA1 | Date | |
|---|---|---|---|
| b59aedad89 | |||
| 3f5e3558b9 | |||
| bb2db0d30d | |||
| e75846a507 | |||
| 05e56d67ea | |||
| 7164156c35 | |||
| e67e1df9a5 |
9
compile
9
compile
@@ -2,15 +2,10 @@
|
|||||||
|
|
||||||
CC=clang
|
CC=clang
|
||||||
INCLUDE="\
|
INCLUDE="\
|
||||||
-Ialiases \
|
$(find src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
-Icpath/include \
|
|
||||||
-Idstr/include \
|
|
||||||
$(find mem/include -type d | xargs -I{} echo -n "-I{} ") \
|
|
||||||
"
|
"
|
||||||
SRC="\
|
SRC="\
|
||||||
cpath/src/*.c \
|
$(find src -type f -name *.c | xargs -I{} echo -n "{} ") \
|
||||||
dstr/src/*.c \
|
|
||||||
mem/src/*/*.c \
|
|
||||||
"
|
"
|
||||||
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
CFLAGS="-O3 -shared -fPIC -Wall -Werror -pedantic"
|
||||||
OUT="libwapp.so"
|
OUT="libwapp.so"
|
||||||
|
|||||||
@@ -11,21 +11,8 @@
|
|||||||
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
#define DEFAULT_ALIGNMENT (2 * sizeof(void *))
|
||||||
#endif /* ifndef DEFAULT_ALIGNMENT */
|
#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
|
#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;
|
typedef struct base_arena BaseArena;
|
||||||
struct base_arena {
|
struct base_arena {
|
||||||
u8 *buf;
|
u8 *buf;
|
||||||
@@ -41,14 +28,11 @@ struct growing_arena {
|
|||||||
u64 initial_capacity;
|
u64 initial_capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
internal BaseArena *find_arena_from_pointer(const Arena *arena, void *ptr);
|
|
||||||
|
|
||||||
internal bool base_arena_init(BaseArena *arena, u64 capacity);
|
internal bool base_arena_init(BaseArena *arena, u64 capacity);
|
||||||
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
||||||
u64 alignment);
|
u64 alignment);
|
||||||
internal void base_arena_clear(BaseArena *arena);
|
internal void base_arena_clear(BaseArena *arena);
|
||||||
internal void base_arena_free(BaseArena *arena);
|
internal void base_arena_destroy(BaseArena *arena);
|
||||||
internal ArenaAllocHDR *find_alloc_header(BaseArena *arena, void *alloc_ptr);
|
|
||||||
|
|
||||||
// PUBLIC API
|
// PUBLIC API
|
||||||
|
|
||||||
@@ -65,12 +49,12 @@ bool wapp_mem_arena_init(Arena **arena, u64 base_capacity) {
|
|||||||
|
|
||||||
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
|
arena_ptr->active_arena = (BaseArena *)calloc(1, sizeof(BaseArena));
|
||||||
if (!(arena_ptr->active_arena)) {
|
if (!(arena_ptr->active_arena)) {
|
||||||
wapp_mem_arena_free(arena);
|
wapp_mem_arena_destroy(arena);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
if (!base_arena_init(arena_ptr->active_arena, base_capacity)) {
|
||||||
wapp_mem_arena_free(arena);
|
wapp_mem_arena_destroy(arena);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,54 +106,6 @@ void *wapp_mem_arena_alloc_aligned(Arena *arena, u64 size, u64 alignment) {
|
|||||||
return output;
|
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) {
|
void wapp_mem_arena_clear(Arena *arena) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return;
|
return;
|
||||||
@@ -187,7 +123,7 @@ void wapp_mem_arena_clear(Arena *arena) {
|
|||||||
arena->active_arena = last_active;
|
arena->active_arena = last_active;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_mem_arena_free(Arena **arena) {
|
void wapp_mem_arena_destroy(Arena **arena) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -205,7 +141,7 @@ void wapp_mem_arena_free(Arena **arena) {
|
|||||||
while (current) {
|
while (current) {
|
||||||
next = current->next;
|
next = current->next;
|
||||||
|
|
||||||
base_arena_free(current);
|
base_arena_destroy(current);
|
||||||
free(current);
|
free(current);
|
||||||
|
|
||||||
current = next;
|
current = next;
|
||||||
@@ -215,13 +151,13 @@ void wapp_mem_arena_free(Arena **arena) {
|
|||||||
while (current) {
|
while (current) {
|
||||||
prev = current->prev;
|
prev = current->prev;
|
||||||
|
|
||||||
base_arena_free(current);
|
base_arena_destroy(current);
|
||||||
free(current);
|
free(current);
|
||||||
|
|
||||||
current = prev;
|
current = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
base_arena_free(arena_ptr->active_arena);
|
base_arena_destroy(arena_ptr->active_arena);
|
||||||
|
|
||||||
free(arena_ptr->active_arena);
|
free(arena_ptr->active_arena);
|
||||||
arena_ptr->active_arena = NULL;
|
arena_ptr->active_arena = NULL;
|
||||||
@@ -235,30 +171,6 @@ void wapp_mem_arena_free(Arena **arena) {
|
|||||||
|
|
||||||
// INTERNAL FUNCTIONS
|
// 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) {
|
internal bool base_arena_init(BaseArena *arena, u64 capacity) {
|
||||||
if (!arena || arena->buf || capacity == 0) {
|
if (!arena || arena->buf || capacity == 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -285,22 +197,13 @@ internal void *base_arena_alloc_aligned(BaseArena *arena, u64 size,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 *start_offset = arena->offset;
|
u8 *alloc_start = arena->offset;
|
||||||
u8 *alloc_start = arena->offset + sizeof(ArenaAllocHDR);
|
|
||||||
|
|
||||||
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
u8 *output = wapp_mem_util_align_forward((void *)alloc_start, alignment);
|
||||||
if (output + size >= arena->buf + arena->capacity) {
|
if (output + size >= arena->buf + arena->capacity) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArenaAllocHDR *header = (ArenaAllocHDR *)start_offset;
|
|
||||||
*header = (ArenaAllocHDR){
|
|
||||||
.magic = HDR_MAGIC,
|
|
||||||
.alloc_size = size,
|
|
||||||
.alignment = alignment,
|
|
||||||
.alloc_start = output,
|
|
||||||
};
|
|
||||||
|
|
||||||
arena->offset = output + size;
|
arena->offset = output + size;
|
||||||
|
|
||||||
return (void *)output;
|
return (void *)output;
|
||||||
@@ -315,7 +218,7 @@ internal void base_arena_clear(BaseArena *arena) {
|
|||||||
arena->offset = arena->buf;
|
arena->offset = arena->buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void base_arena_free(BaseArena *arena) {
|
internal void base_arena_destroy(BaseArena *arena) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -328,33 +231,3 @@ internal void base_arena_free(BaseArena *arena) {
|
|||||||
arena->capacity = 0;
|
arena->capacity = 0;
|
||||||
arena->prev = arena->next = NULL;
|
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;
|
|
||||||
}
|
|
||||||
@@ -13,11 +13,8 @@ typedef struct growing_arena Arena;
|
|||||||
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity);
|
bool wapp_mem_arena_init(Arena **arena, u64 base_capacity);
|
||||||
void *wapp_mem_arena_alloc(Arena *arena, u64 size);
|
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_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_clear(Arena *arena);
|
||||||
void wapp_mem_arena_free(Arena **arena);
|
void wapp_mem_arena_destroy(Arena **arena);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
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
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "dstr.h"
|
#include "dstr.h"
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_arena.h"
|
||||||
#include "mem_libc.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -9,45 +8,42 @@
|
|||||||
// Use this scalar to allocate extra memory in order to avoid having to
|
// Use this scalar to allocate extra memory in order to avoid having to
|
||||||
// constantly reallocate
|
// constantly reallocate
|
||||||
#define CAPACITY_SCALAR 8
|
#define CAPACITY_SCALAR 8
|
||||||
|
#define MINIMUM_DSTR_CAPACITY 1024
|
||||||
|
|
||||||
struct dstr {
|
struct dstr {
|
||||||
Allocator allocator;
|
|
||||||
u64 capacity;
|
u64 capacity;
|
||||||
u64 size;
|
u64 size;
|
||||||
char buf[];
|
char buf[];
|
||||||
};
|
};
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator) {
|
String *wapp_dstr_with_capacity(u64 capacity, Arena *arena) {
|
||||||
Allocator alloc;
|
if (!arena) {
|
||||||
if (allocator) {
|
return NULL;
|
||||||
alloc = *allocator;
|
|
||||||
} else {
|
|
||||||
alloc = wapp_mem_libc_allocator();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String *out =
|
String *out =
|
||||||
(String *)wapp_mem_allocator_alloc(&alloc, sizeof(String) + capacity + 1);
|
(String *)wapp_mem_arena_alloc(arena, 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;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
|
String *wapp_dstr_from_string(const char *str, Arena *arena) {
|
||||||
if (!str) {
|
if (!str || !arena) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 length = strlen(str);
|
u64 length = strlen(str);
|
||||||
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
u64 capacity = length * CAPACITY_SCALAR;
|
||||||
|
capacity =
|
||||||
|
capacity >= MINIMUM_DSTR_CAPACITY ? capacity : MINIMUM_DSTR_CAPACITY;
|
||||||
|
|
||||||
String *out = wapp_dstr_with_capacity(capacity, allocator);
|
String *out = wapp_dstr_with_capacity(capacity, arena);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -58,55 +54,45 @@ String *wapp_dstr_from_string(const char *str, const Allocator *allocator) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_dstr_update(String **dst, const char *src) {
|
StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena) {
|
||||||
if (!dst || !(*dst)) {
|
if (!dst || !(*dst)) {
|
||||||
return;
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 length = strlen(src);
|
u64 length = strlen(src);
|
||||||
|
|
||||||
String *str = *dst;
|
String *str = *dst;
|
||||||
|
|
||||||
if (length < str->capacity) {
|
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);
|
memset(str->buf, 0, str->capacity);
|
||||||
|
|
||||||
str->size = length;
|
str->size = length;
|
||||||
|
|
||||||
strncpy(str->buf, src, length + 1);
|
strncpy(str->buf, src, length + 1);
|
||||||
} else {
|
|
||||||
u64 capacity = length * CAPACITY_SCALAR;
|
|
||||||
|
|
||||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
return (StringUpdate){.updated = true, .str = *dst};
|
||||||
&(str->allocator), *dst, sizeof(String) + capacity + 1);
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena) {
|
||||||
tmp->size = length;
|
|
||||||
strncpy(tmp->buf, src, length + 1);
|
|
||||||
|
|
||||||
*dst = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_free(String **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String *str_ptr = *str;
|
|
||||||
wapp_mem_allocator_free(&(str_ptr->allocator), (void **)str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_concat(String **dst, const char *src) {
|
|
||||||
if (!dst || !(*dst)) {
|
if (!dst || !(*dst)) {
|
||||||
return;
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 src_length = strlen(src);
|
u64 src_length = strlen(src);
|
||||||
if (src_length == 0) {
|
if (src_length == 0) {
|
||||||
return;
|
return (StringUpdate){.updated = false, .str = *dst};
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 new_length = (*dst)->size + src_length;
|
u64 new_length = (*dst)->size + src_length;
|
||||||
@@ -117,42 +103,7 @@ void wapp_dstr_concat(String **dst, const char *src) {
|
|||||||
strncpy(str, (*dst)->buf, (*dst)->size);
|
strncpy(str, (*dst)->buf, (*dst)->size);
|
||||||
strncat(str, src, new_length + 1 - (*dst)->size);
|
strncat(str, src, new_length + 1 - (*dst)->size);
|
||||||
|
|
||||||
wapp_dstr_update(dst, str);
|
return wapp_dstr_update(dst, str, arena);
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_append(String **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;
|
|
||||||
|
|
||||||
wapp_dstr_update(dst, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wapp_dstr_resize(String **str) {
|
|
||||||
if (!str || !(*str)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String *str_ptr = *str;
|
|
||||||
u64 capacity = (*str)->size;
|
|
||||||
|
|
||||||
String *tmp = (String *)wapp_mem_allocator_realloc(
|
|
||||||
&(str_ptr->allocator), *str, sizeof(String) + capacity + 1);
|
|
||||||
if (!tmp) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp->capacity = capacity;
|
|
||||||
|
|
||||||
*str = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wapp_dstr_clear(String *str) {
|
void wapp_dstr_clear(String *str) {
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#define DSTR_H
|
#define DSTR_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "mem_allocator.h"
|
#include "mem_arena.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -10,13 +11,16 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct dstr String;
|
typedef struct dstr String;
|
||||||
|
|
||||||
String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator);
|
typedef struct string_update StringUpdate;
|
||||||
String *wapp_dstr_from_string(const char *str, const Allocator *allocator);
|
struct string_update {
|
||||||
void wapp_dstr_update(String **dst, const char *src);
|
bool updated;
|
||||||
void wapp_dstr_free(String **str);
|
String *str;
|
||||||
void wapp_dstr_concat(String **dst, const char *src);
|
};
|
||||||
void wapp_dstr_append(String **dst, char c);
|
|
||||||
void wapp_dstr_resize(String **str);
|
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_clear(String *str);
|
||||||
void wapp_dstr_print(const String *str);
|
void wapp_dstr_print(const String *str);
|
||||||
i64 wapp_dstr_find(const String *str, const char *substr);
|
i64 wapp_dstr_find(const String *str, const char *substr);
|
||||||
Reference in New Issue
Block a user