Add static, runtime and debug assert utilities
This commit is contained in:
		| @@ -3,6 +3,7 @@ | ||||
|  */ | ||||
|  | ||||
| #include "./dbl_list.h" | ||||
| #include "../../common/assert/assert.h" | ||||
| #include "../../common/aliases/aliases.h" | ||||
| #include "../../common/platform/platform.h" | ||||
| #include <stddef.h> | ||||
|   | ||||
| @@ -2,27 +2,27 @@ | ||||
|  | ||||
| #include "mem_allocator.h" | ||||
| #include "../../common/aliases/aliases.h" | ||||
| #include "../../common/assert/assert.h" | ||||
| #include <stdlib.h> | ||||
| #include <assert.h> | ||||
|  | ||||
| void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size) { | ||||
|   assert(allocator != NULL && (allocator->alloc) != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && (allocator->alloc) != NULL, "`allocator` and `allocator->alloc` should not be NULL"); | ||||
|   return allocator->alloc(size, allocator->obj); | ||||
| } | ||||
|  | ||||
| void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, u64 alignment) { | ||||
|   assert(allocator != NULL && (allocator->alloc_aligned) != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && (allocator->alloc_aligned) != NULL, "`allocator` and `allocator->alloc_aligned` should not be NULL"); | ||||
|   return allocator->alloc_aligned(size, alignment, allocator->obj); | ||||
| } | ||||
|  | ||||
| void *wapp_mem_allocator_realloc(const Allocator *allocator, void *ptr, u64 old_size, u64 new_size) { | ||||
|   assert(allocator != NULL && (allocator->realloc) != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && (allocator->realloc) != NULL, "`allocator` and `allocator->realloc` should not be NULL"); | ||||
|   return allocator->realloc(ptr, old_size, new_size, allocator->obj); | ||||
| } | ||||
|  | ||||
| void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, u64 old_size, | ||||
|                                          u64 new_size, u64 alignment) { | ||||
|   assert(allocator != NULL && (allocator->realloc_aligned) != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && (allocator->realloc_aligned) != NULL, "`allocator` and `allocator->realloc_aligned` should not be NULL"); | ||||
|   return allocator->realloc_aligned(ptr, old_size, new_size, alignment, allocator->obj); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -2,18 +2,18 @@ | ||||
|  | ||||
| #include "str8.h" | ||||
| #include "../../../common/aliases/aliases.h" | ||||
| #include "../../../common/assert/assert.h" | ||||
| #include "../../mem_allocator/mem_allocator.h" | ||||
| #include <stdarg.h> | ||||
| #include <stddef.h> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <stdbool.h> | ||||
| #include <assert.h> | ||||
|  | ||||
| #define STR8_BUF_ALLOC_SIZE(CAPACITY) (sizeof(Str8) + sizeof(c8) * CAPACITY) | ||||
|  | ||||
| Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity) { | ||||
|   assert(allocator != NULL); | ||||
|   wapp_debug_assert(allocator != NULL, "`allocator` should not be NULL"); | ||||
|  | ||||
|   Str8 *str = wapp_mem_allocator_alloc(allocator, STR8_BUF_ALLOC_SIZE(capacity)); | ||||
|   if (!str) { | ||||
| @@ -29,7 +29,7 @@ RETURN_STR8: | ||||
| } | ||||
|  | ||||
| Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) { | ||||
|   assert(allocator != NULL && str != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL, "`allocator` and `str` should not be NULL"); | ||||
|  | ||||
|   u64 length = strlen(str); | ||||
|   Str8 *output     = wapp_str8_alloc_buf(allocator, length * 2); | ||||
| @@ -45,7 +45,7 @@ RETURN_ALLOC_CSTR: | ||||
| } | ||||
|  | ||||
| Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str) { | ||||
|   assert(allocator != NULL && str != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL, "`allocator` and `str` should not be NULL"); | ||||
|  | ||||
|   Str8 *output = wapp_str8_alloc_buf(allocator, str->capacity); | ||||
|   if (!output) { | ||||
| @@ -60,7 +60,7 @@ RETURN_ALLOC_STR8: | ||||
| } | ||||
|  | ||||
| Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end) { | ||||
|   assert(allocator != NULL && str != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL, "`allocator` and `str` should not be NULL"); | ||||
|  | ||||
|   Str8 *output = NULL; | ||||
|  | ||||
| @@ -85,7 +85,7 @@ RETURN_ALLOC_SUBSTR: | ||||
| } | ||||
|  | ||||
| void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str) { | ||||
|   assert(allocator != NULL && str != NULL && (*str) != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL && (*str) != NULL, "Either `allocator` is NULL or `str` is an invalid double pointer"); | ||||
|   wapp_mem_allocator_free(allocator, (void **)str, STR8_BUF_ALLOC_SIZE((*str)->capacity)); | ||||
| } | ||||
|  | ||||
| @@ -148,7 +148,7 @@ Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) { | ||||
| } | ||||
|  | ||||
| Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) { | ||||
|   assert(allocator != NULL && dst != NULL && src != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL"); | ||||
|  | ||||
|   Str8 *output  = NULL; | ||||
|   u64 remaining = dst->capacity - dst->size; | ||||
| @@ -174,7 +174,7 @@ RETURN_STR8_CONCAT: | ||||
| } | ||||
|  | ||||
| void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) { | ||||
|   assert(dst != NULL && src != NULL); | ||||
|   wapp_debug_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); | ||||
|  | ||||
|   u64 remaining = dst->capacity - dst->size; | ||||
|   u64 to_copy   = remaining < src->size ? remaining : src->size; | ||||
| @@ -184,7 +184,7 @@ void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) { | ||||
| } | ||||
|  | ||||
| void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) { | ||||
|   assert(dst != NULL && src != NULL); | ||||
|   wapp_debug_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); | ||||
|  | ||||
|   u64 length  = strlen(src); | ||||
|   u64 to_copy = length <= dst->capacity ? length : dst->capacity; | ||||
| @@ -195,7 +195,7 @@ void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) { | ||||
| } | ||||
|  | ||||
| void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) { | ||||
|   assert(dst != NULL && src != NULL); | ||||
|   wapp_debug_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); | ||||
|  | ||||
|   u64 to_copy = src->size <= dst->capacity ? src->size : dst->capacity; | ||||
|  | ||||
| @@ -205,7 +205,7 @@ void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) { | ||||
| } | ||||
|  | ||||
| void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) { | ||||
|   assert(dst != NULL && src != NULL); | ||||
|   wapp_debug_assert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL"); | ||||
|  | ||||
|   u64 to_copy = src->size < dst_capacity ? src->size : dst_capacity - 1; | ||||
|  | ||||
| @@ -214,7 +214,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) { | ||||
| } | ||||
|  | ||||
| void wapp_str8_format(Str8 *dst, const char *format, ...) { | ||||
|   assert(dst != NULL && format != NULL); | ||||
|   wapp_debug_assert(dst != NULL && format != NULL, "`dst` and `format` should not be NULL"); | ||||
|  | ||||
|   va_list args1; | ||||
|   va_list args2; | ||||
| @@ -276,7 +276,7 @@ i64 wapp_str8_rfind(Str8RO *str, Str8RO substr) { | ||||
| } | ||||
|  | ||||
| Str8List *wapp_str8_split_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { | ||||
|   assert(allocator != NULL && str != NULL && delimiter != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL && delimiter != NULL, "`allocator`, `str` and `delimiter` should not be NULL"); | ||||
|  | ||||
|   Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List)); | ||||
|  | ||||
| @@ -329,7 +329,7 @@ RETURN_STR8_SPLIT: | ||||
| } | ||||
|  | ||||
| Str8List *wapp_str8_rsplit_with_max(const Allocator *allocator, Str8RO *str, Str8RO *delimiter, i64 max_splits) { | ||||
|   assert(allocator != NULL && str != NULL && delimiter != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && str != NULL && delimiter != NULL, "`allocator`, `str` and `delimiter` should not be NULL"); | ||||
|  | ||||
|   Str8List *output = wapp_mem_allocator_alloc(allocator, sizeof(Str8List)); | ||||
|  | ||||
| @@ -379,7 +379,7 @@ RETURN_STR8_SPLIT: | ||||
| } | ||||
|  | ||||
| Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *delimiter) { | ||||
|   assert(allocator != NULL && list != NULL && delimiter != NULL); | ||||
|   wapp_debug_assert(allocator != NULL && list != NULL && delimiter != NULL, "`allocator`, `list` and `delimiter` should not be NULL"); | ||||
|  | ||||
|   u64 capacity = wapp_str8_list_total_size(list) + (delimiter->size * (list->node_count - 1)); | ||||
|   Str8 *output = wapp_str8_alloc_buf(allocator, capacity * 2); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user