Update dstr to use Arena
This commit is contained in:
		| @@ -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) { | String *wapp_dstr_update(String **dst, const char *src, Arena *arena) { | ||||||
|   if (!dst || !(*dst)) { |   if (!dst || !(*dst)) { | ||||||
|     return; |     return *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 *dst; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     String *new_str = wapp_dstr_from_string(src, arena); | ||||||
|  |     if (!new_str) { | ||||||
|  |       return *dst; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return 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 *dst; | ||||||
|         &(str->allocator), *dst, sizeof(String) + capacity + 1); |  | ||||||
|     if (!tmp) { |  | ||||||
|       return; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|     tmp->capacity = capacity; | String *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 *dst; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   u64 src_length = strlen(src); |   u64 src_length = strlen(src); | ||||||
|   if (src_length == 0) { |   if (src_length == 0) { | ||||||
|     return; |     return *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,7 @@ | |||||||
| #define DSTR_H | #define DSTR_H | ||||||
| 
 | 
 | ||||||
| #include "aliases.h" | #include "aliases.h" | ||||||
| #include "mem_allocator.h" | #include "mem_arena.h" | ||||||
| 
 | 
 | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
| extern "C" { | extern "C" { | ||||||
| @@ -10,13 +10,10 @@ extern "C" { | |||||||
| 
 | 
 | ||||||
| typedef struct dstr String; | typedef struct dstr String; | ||||||
| 
 | 
 | ||||||
| String *wapp_dstr_with_capacity(u64 capacity, const Allocator *allocator); | String *wapp_dstr_with_capacity(u64 capacity, Arena *arena); | ||||||
| String *wapp_dstr_from_string(const char *str, const Allocator *allocator); | String *wapp_dstr_from_string(const char *str, Arena *arena); | ||||||
| void wapp_dstr_update(String **dst, const char *src); | String *wapp_dstr_update(String **dst, const char *src, Arena *arena); | ||||||
| void wapp_dstr_free(String **str); | String *wapp_dstr_concat(String **dst, const char *src, Arena *arena); | ||||||
| void wapp_dstr_concat(String **dst, const char *src); |  | ||||||
| void wapp_dstr_append(String **dst, char c); |  | ||||||
| void wapp_dstr_resize(String **str); |  | ||||||
| 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