diff options
-rw-r--r-- | memory/memory.c (renamed from memory/memory.cpp) | 28 | ||||
-rw-r--r-- | memory/memory.h | 49 | ||||
-rw-r--r-- | memory/test.cpp | 230 | ||||
-rw-r--r-- | strings/strings.cpp | 31 | ||||
-rw-r--r-- | strings/strings.h | 42 | ||||
-rw-r--r-- | strings/test.cpp | 65 | ||||
-rw-r--r-- | types.h | 23 |
7 files changed, 169 insertions, 299 deletions
diff --git a/memory/memory.cpp b/memory/memory.c index 3e305c8..0c998c7 100644 --- a/memory/memory.cpp +++ b/memory/memory.c @@ -10,7 +10,7 @@ uintptr_t mem_clamp_top(uintptr_t mem, uintptr_t top) return mem > top ? top : mem; } -bool is_power_of_two(uintptr_t x) { +b8 is_power_of_two(uintptr_t x) { return (x & (x-1)) == 0; } @@ -31,7 +31,7 @@ uintptr_t align_forward(uintptr_t ptr, size_t alignment) { } //=========================================================================================== -// ---------------------------------- ARENA ------------------------------------------------- +// ---------------------------------- Arena ------------------------------------------------- //=========================================================================================== /* @@ -40,14 +40,14 @@ uintptr_t align_forward(uintptr_t ptr, size_t alignment) { This is because there is no check being made */ -void arena_init(struct arena* a, unsigned char* backing_store, size_t capacity) { +void arena_init(struct Arena* a, unsigned char* backing_store, size_t capacity) { a->buffer = backing_store; a->curr_offset = 0; a->prev_offset = 0; a->capacity = capacity; } -void* arena_alloc_aligned(struct arena* a, size_t size, size_t alignment) { +void* arena_alloc_aligned(struct Arena* a, size_t size, size_t alignment) { void* ptr = NULL; assert(is_power_of_two(alignment)); @@ -67,11 +67,11 @@ void* arena_alloc_aligned(struct arena* a, size_t size, size_t alignment) { return ptr; } -void* arena_alloc(struct arena* a, size_t size) { +void* arena_alloc(struct Arena* a, size_t size) { return arena_alloc_aligned(a, size, DEFAULT_ALIGNMENT); } -void* arena_resize_aligned(struct arena* a, void* old_memory, size_t old_size, +void* arena_resize_aligned(struct Arena* a, void* old_memory, size_t old_size, size_t new_size, size_t alignment) { unsigned char* old = (unsigned char*)old_memory; @@ -111,12 +111,12 @@ void* arena_resize_aligned(struct arena* a, void* old_memory, size_t old_size, return ptr; } -void* arena_resize(struct arena* a, void* old_mem, size_t old_size, +void* arena_resize(struct Arena* a, void* old_mem, size_t old_size, size_t new_size) { return arena_resize_aligned(a, old_mem, old_size, new_size, DEFAULT_ALIGNMENT); } -void arena_clear(struct arena *a) { +void arena_clear(struct Arena *a) { a->curr_offset = 0; a->prev_offset = 0; } @@ -171,7 +171,7 @@ size_t calc_padding_with_header(uintptr_t ptr, uintptr_t alignment, size_t hdr_s return (size_t)padding; } -struct res_void stack_alloc_aligned(struct stack* s, size_t size, size_t alignment) +struct ResVoid stack_alloc_aligned(struct stack* s, size_t size, size_t alignment) { uintptr_t curr_addr, next_addr; size_t padding; @@ -182,7 +182,7 @@ struct res_void stack_alloc_aligned(struct stack* s, size_t size, size_t alignme alignment = 128; } - struct res_void result = {}; + struct ResVoid result = {0}; curr_addr = (uintptr_t)s->buffer + (uintptr_t)s->curr_offset; padding = calc_padding_with_header(curr_addr, (uintptr_t)alignment, sizeof(struct stack_hdr)); @@ -207,7 +207,7 @@ struct res_void stack_alloc_aligned(struct stack* s, size_t size, size_t alignme return result; } -struct res_void stack_alloc(struct stack* s, size_t size) +struct ResVoid stack_alloc(struct stack* s, size_t size) { return stack_alloc_aligned(s, size, DEFAULT_ALIGNMENT); } @@ -224,10 +224,10 @@ enum MemStatus stack_free(struct stack* s) return MEM_OK; } -struct res_void stack_resize_aligned(struct stack* s, void* old_memory, size_t old_size, +struct ResVoid stack_resize_aligned(struct stack* s, void* old_memory, size_t old_size, size_t new_size, size_t alignment) { - struct res_void result = {}; + struct ResVoid result = {0}; if (old_memory < s->buffer || old_memory > s->buffer + s->capacity) { @@ -262,7 +262,7 @@ struct res_void stack_resize_aligned(struct stack* s, void* old_memory, size_t o return result; } -struct res_void stack_resize(struct stack* s, void* old_memory, size_t old_size, size_t new_size) +struct ResVoid stack_resize(struct stack* s, void* old_memory, size_t old_size, size_t new_size) { return stack_resize_aligned(s, old_memory, old_size, new_size, DEFAULT_ALIGNMENT); } diff --git a/memory/memory.h b/memory/memory.h index 14504b4..94b5022 100644 --- a/memory/memory.h +++ b/memory/memory.h @@ -7,16 +7,19 @@ #include <string.h> #ifndef AMR_TYPES_H +#define AMR_TYPES_H -typedef int8_t S8; -typedef int16_t S16; -typedef int32_t S32; -typedef int64_t S64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; -typedef uint8_t U8; -typedef uint16_t U16; -typedef uint32_t U32; -typedef uint64_t U64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef u8 b8; #endif // AMR_TYPES_H @@ -30,19 +33,19 @@ typedef uint64_t U64; enum MemStatus { MEM_OK=0, MEM_OUT_OF_BOUNDS, MEM_FULL }; -struct res_void { +struct ResVoid { enum MemStatus status; size_t bytes_count; void* memory; }; -bool is_power_of_two(uintptr_t x); +b8 is_power_of_two(uintptr_t x); //=========================================================================================== // ---------------------------------- ARENA ------------------------------------------------- //=========================================================================================== -struct arena { +struct Arena { unsigned char* buffer; size_t prev_offset; size_t curr_offset; @@ -50,14 +53,14 @@ struct arena { }; uintptr_t align_forward(uintptr_t ptr, size_t align); -void arena_init(struct arena *a, unsigned char *backing_store, size_t capacity); -void* arena_alloc_aligned(struct arena* a, size_t size, size_t align); -void* arena_alloc(struct arena* a, size_t size); -void* arena_resize_aligned(struct arena* a, void* old_memory, size_t old_size, +void arena_init(struct Arena *a, unsigned char *backing_store, size_t capacity); +void* arena_alloc_aligned(struct Arena* a, size_t size, size_t align); +void* arena_alloc(struct Arena* a, size_t size); +void* arena_resize_aligned(struct Arena* a, void* old_memory, size_t old_size, size_t new_size, size_t align); -void* arena_resize(struct arena* a, void* old_mem, size_t old_size, +void* arena_resize(struct Arena* a, void* old_mem, size_t old_size, size_t new_size); -void arena_clear(struct arena *a); +void arena_clear(struct Arena *a); //=========================================================================================== // ---------------------------------- STACK ------------------------------------------------- @@ -82,13 +85,13 @@ struct stack_hdr { size_t padding; }; -void stack_init(struct stack* s, void *backing_store, size_t capacity); -struct res_void stack_alloc_aligned(struct stack* s, size_t size, size_t alignment); -struct res_void stack_alloc(struct stack* s, size_t size); +void stack_init(struct stack* s, void *backing_store, size_t capacity); +struct ResVoid stack_alloc_aligned(struct stack* s, size_t size, size_t alignment); +struct ResVoid stack_alloc(struct stack* s, size_t size); enum MemStatus stack_free(struct stack* s); -struct res_void stack_resize_aligned(struct stack* s, void* old_memory, size_t old_size, +struct ResVoid stack_resize_aligned(struct stack* s, void* old_memory, size_t old_size, size_t new_size, size_t alignment); -struct res_void stack_resize(struct stack* s, void* old_memory, size_t old_size, size_t new_size); -void stack_clear(struct stack* s); +struct ResVoid stack_resize(struct stack* s, void* old_memory, size_t old_size, size_t new_size); +void stack_clear(struct stack* s); #endif diff --git a/memory/test.cpp b/memory/test.cpp index 4d17abd..97c0f25 100644 --- a/memory/test.cpp +++ b/memory/test.cpp @@ -1,29 +1,29 @@ #include <stdlib.h> #include <stdio.h> -#include "memory.cpp" +#include "memory.c" // ================ ARENA ================ // -void test_arena_init(unsigned char *buffer, U32 buffer_size) +void test_arena_init(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); - assert((a.buffer == buffer) && "arena does not start at the buffer"); - assert((a.curr_offset == 0) && (a.prev_offset == 0) && "arena offsets not initialised properly"); - assert((a.capacity == buffer_size) && "arena capacity does not match buffer size"); + assert((a.buffer == buffer) && "Arena does not start at the buffer"); + assert((a.curr_offset == 0) && (a.prev_offset == 0) && "Arena offsets not initialised properly"); + assert((a.capacity == buffer_size) && "Arena capacity does not match buffer size"); } -void test_arena_alloc(unsigned char *buffer, U32 buffer_size) +void test_arena_alloc(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); assert((qres != NULL) && "Arena Allocation Test Failed: " "Reason: Failed to allocate memory"); } -void test_arena_alloc_low_memory(unsigned char *buffer, U32 buffer_size) +void test_arena_alloc_low_memory(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, buffer_size + 20); assert((qres == NULL) && "Low Memory Arena Allocation Test Failed: " @@ -31,12 +31,12 @@ void test_arena_alloc_low_memory(unsigned char *buffer, U32 buffer_size) qres = arena_alloc(&a, 20); } -void test_arena_alloc_negative_size(unsigned char *buffer, U32 buffer_size) +void test_arena_alloc_negative_size(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 16); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; qres = arena_alloc(&a, -16); @@ -44,93 +44,93 @@ void test_arena_alloc_negative_size(unsigned char *buffer, U32 buffer_size) "Reason: Should have allocated the remaining buffer. \n"); } -void test_arena_resize(unsigned char *buffer, U32 buffer_size) +void test_arena_resize(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 8); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n1, 8, 16); assert((qres != NULL) && "Arena Resize Test Failed: " "Reason: Failed to resize previously allocated memory\n" "this should not have happened since we do not have anything causing availability issues\n"); - U8* n1_1 = (U8*)qres; + u8* n1_1 = (u8*)qres; assert((*n1 == *n1_1) && "Arena Resize Test Failed: " "Reason: Value of resized memory changed. This should not happen in any case!\n"); } -void test_arena_resize_tail(unsigned char *buffer, U32 buffer_size) +void test_arena_resize_tail(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 16); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 16); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n2, 16, 8); /*assert((qres.bytes_count == 8) && "Arena Resize Tail Test Failed: " "Reason: Failed to resize previously allocated memory at tail\n");*/ - U8* n2_1 = (U8*)qres; + u8* n2_1 = (u8*)qres; assert((*n2 == *n2_1) && "Arena Resize Test Failed: " "Reason: Value of resized memory changed. This should not happen in any case!\n"); } -void test_arena_resize_filled_completely(unsigned char *buffer, U32 buffer_size) +void test_arena_resize_filled_completely(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 8); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n1, 8, buffer_size - 32); assert((qres != NULL) && "Arena Resize Filled Completely Failed: \n" - "Reason: Failed to resize an element even though it perfectly fills the memory arena"); + "Reason: Failed to resize an element even though it perfectly fills the memory Arena"); } -void test_arena_resize_out_of_bounds(unsigned char *buffer, U32 buffer_size) +void test_arena_resize_out_of_bounds(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 8); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n1, 8, buffer_size - 8); assert(qres == NULL && "Arena Resize OUT OF BOUNDS test Failed: \n" - "Reason: Failed to fit a larger than capacity element within arena capacity.\n"); + "Reason: Failed to fit a larger than capacity element within Arena capacity.\n"); } -void test_arena_resize_negative_size(unsigned char *buffer, U32 buffer_size) +void test_arena_resize_negative_size(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 8); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n1, 8, -35); @@ -138,47 +138,47 @@ void test_arena_resize_negative_size(unsigned char *buffer, U32 buffer_size) "Reason: Failed to handle allocating a -ve size (VERY LARGE IN UINT) space.\n"); } -void test_arena_resize_tail_negative_size(unsigned char *buffer, U32 buffer_size) +void test_arena_resize_tail_negative_size(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 23; qres = arena_alloc(&a, 8); - U8* n2 = (U8*)qres; + u8* n2 = (u8*)qres; *n2 = 20; qres = arena_resize(&a, n2, 8, -35); assert(qres == NULL && "Arena Resize OUT OF BOUNDS test Failed: \n" - "Reason: Failed to fit a larger than capacity element within arena capacity.\n"); + "Reason: Failed to fit a larger than capacity element within Arena capacity.\n"); } -void test_arena_clear(unsigned char *buffer, U32 buffer_size) +void test_arena_clear(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); arena_clear(&a); assert((a.prev_offset == 0) && (a.curr_offset == 0) && "Arena Clear Test Failed: \n" - "Reason: Failed to clear offsets on an active arena entity after it was cleared.\n"); + "Reason: Failed to clear offsets on an active Arena entity after it was cleared.\n"); } -void test_arena_realloc(unsigned char *buffer, U32 buffer_size) +void test_arena_realloc(unsigned char *buffer, u32 buffer_size) { - struct arena a = {0}; + struct Arena a = {0}; arena_init(&a, buffer, buffer_size); void *qres = arena_alloc(&a, 8); - U8* n1 = (U8*)qres; + u8* n1 = (u8*)qres; *n1 = 12; - U8 n1_value = *n1; + u8 n1_value = *n1; - // free arena + // free Arena arena_clear(&a); qres = arena_alloc(&a, 16); - U8* n1_realloc = (U8*)qres; + u8* n1_realloc = (u8*)qres; assert((*n1_realloc != n1_value) && "Arena Realloc Test Failed: \n" "Reason: Failed to properly clear memory in a region where memory was already allocated.\n"); @@ -186,7 +186,7 @@ void test_arena_realloc(unsigned char *buffer, U32 buffer_size) // ================ STACK ================ // -void test_stack_init(unsigned char *buffer, U32 buffer_size) +void test_stack_init(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); @@ -195,16 +195,16 @@ void test_stack_init(unsigned char *buffer, U32 buffer_size) assert((s.capacity == buffer_size) && "stack capacity does not match buffer size"); } -void test_stack_alloc_n1(unsigned char *buffer, U32 buffer_size) +void test_stack_alloc_n1(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); assert((qres.status == MEM_OK) && "failed to allocate stack element"); - U8 *ele = (U8 *)qres.memory; + u8 *ele = (u8 *)qres.memory; struct stack_hdr *header = (struct stack_hdr*)((uintptr_t)ele - sizeof(struct stack_hdr)); assert((header->prev_offset == 0) && "incorrect prev_offset for first stack element"); @@ -213,20 +213,20 @@ void test_stack_alloc_n1(unsigned char *buffer, U32 buffer_size) assert((s.curr_offset == pad + ele_sz ) && "incorrect curr offset memory"); } -void test_stack_alloc_n2(unsigned char *buffer, U32 buffer_size) +void test_stack_alloc_n2(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; + u8 ele_sz = 16; stack_alloc(&s, ele_sz); size_t last_ele_head = s.prev_offset; size_t last_ele_tail = s.curr_offset; - struct res_void qres = stack_alloc(&s, ele_sz); + struct ResVoid qres = stack_alloc(&s, ele_sz); assert((qres.status == MEM_OK) && "failed to allocate stack element"); - U8 *ele = (U8 *)qres.memory; + u8 *ele = (u8 *)qres.memory; struct stack_hdr *header = (struct stack_hdr*)((uintptr_t)ele - sizeof(struct stack_hdr)); assert((header->prev_offset == last_ele_head) && "incorrect prev_offset for second stack element"); @@ -234,12 +234,12 @@ void test_stack_alloc_n2(unsigned char *buffer, U32 buffer_size) assert((s.curr_offset == last_ele_tail + pad + ele_sz ) && "incorrect curr offset memory"); } -void test_stack_free(unsigned char *buffer, U32 buffer_size) +void test_stack_free(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; + u8 ele_sz = 16; stack_alloc(&s, ele_sz); enum MemStatus status = stack_free(&s); @@ -249,72 +249,72 @@ void test_stack_free(unsigned char *buffer, U32 buffer_size) assert((s.prev_offset == 0) && (s.curr_offset == 0) && "failed to move offsets back correctly"); } -void test_stack_resize_n0(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_n0(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 static_mem[8] = {0,1,2,3,4,5,6,7}; - struct res_void qres = stack_resize(&s, static_mem, 8, 16); + u8 static_mem[8] = {0,1,2,3,4,5,6,7}; + struct ResVoid qres = stack_resize(&s, static_mem, 8, 16); assert((qres.status == MEM_OUT_OF_BOUNDS) && "Stack Resize n0 Failed: \ Reason: should not have resized an element not belonging to the stack\n"); } -void test_stack_resize_n1(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_n1(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 23; qres = stack_resize(&s, n1, ele_sz, ele_sz + 20); assert((qres.status == MEM_OK) && "Stack Resize n1 Failed: \ Reason: failed to resize stack element\n"); - n1 = (U8*)qres.memory; + n1 = (u8*)qres.memory; struct stack_hdr* header = (struct stack_hdr*)(n1 - sizeof(struct stack_hdr)); assert((s.curr_offset == 52) && "Stack Resize n1 Failed: \ Reason: incorrectly resized stack element\n"); } -void test_stack_resize_n1_low_memory(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_n1_low_memory(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 23; qres = stack_resize(&s, n1, ele_sz, ele_sz + 256); - n1 = (U8*)qres.memory; + n1 = (u8*)qres.memory; assert((qres.status == MEM_FULL) && "Stack Resize n1 low memory Failed: \ Reason: Should not be allowed to resize beyond the allocated stack capacity\n"); } -void test_stack_resize_n2(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_n2(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; qres = stack_alloc(&s, ele_sz); - U8* n2 = (U8*)qres.memory; + u8* n2 = (u8*)qres.memory; *n2 = 2; size_t last_ele_head = s.prev_offset; qres = stack_resize(&s, n1, ele_sz, 8); - U8* n3 = (U8*)qres.memory; + u8* n3 = (u8*)qres.memory; assert((qres.status == MEM_OK) && "Stack Resize n2 Failed: " "Reason: failed to resize first element in a two element array\n"); @@ -326,14 +326,14 @@ void test_stack_resize_n2(unsigned char *buffer, U32 buffer_size) "Reason: failed to set the previous offset of the header element properly after resize"); } -void test_stack_resize_tail(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_tail(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; qres = stack_resize(&s, n1, ele_sz, 8); @@ -341,20 +341,20 @@ void test_stack_resize_tail(unsigned char *buffer, U32 buffer_size) "Reason: failed to resize last element to be smaller\n"); } -void test_stack_alloc_free(unsigned char *buffer, U32 buffer_size) +void test_stack_alloc_free(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; size_t head_n1 = s.prev_offset; size_t tail_n1 = s.curr_offset; qres = stack_alloc(&s, ele_sz); - U8* n2 = (U8*)qres.memory; + u8* n2 = (u8*)qres.memory; *n2 = 2; size_t head_n2 = s.prev_offset; size_t tail_n2 = s.curr_offset; @@ -390,31 +390,31 @@ void test_stack_alloc_free(unsigned char *buffer, U32 buffer_size) */ qres = stack_alloc(&s, ele_sz); - U8* n2_1 = (U8*)qres.memory; + u8* n2_1 = (u8*)qres.memory; assert((*n2_1 == 0) && "Stack Alloc Free error" "Reason: failed to free up memory properly on allocating memory that was cleared"); } -void test_stack_alloc_free_resize(unsigned char *buffer, U32 buffer_size) +void test_stack_alloc_free_resize(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; size_t head_n1 = s.prev_offset; size_t tail_n1 = s.curr_offset; qres = stack_alloc(&s, ele_sz); - U8* n2 = (U8*)qres.memory; + u8* n2 = (u8*)qres.memory; *n2 = 2; size_t head_n2 = s.prev_offset; size_t tail_n2 = s.curr_offset; qres = stack_alloc(&s, ele_sz); - U8* n3 = (U8*)qres.memory; + u8* n3 = (u8*)qres.memory; *n3 = 3; size_t head_n3 = s.prev_offset; size_t tail_n3 = s.curr_offset; @@ -422,7 +422,7 @@ void test_stack_alloc_free_resize(unsigned char *buffer, U32 buffer_size) stack_free(&s); qres = stack_resize(&s, n1, ele_sz, 32); - U8* n1_1 = (U8*)qres.memory; + u8* n1_1 = (u8*)qres.memory; assert((*n1_1 != 3) && "Stack Alloc Free Resize Test Failed: " "Reason: the newly resized memory was not setup properly.\n" "It had the memory contents of a previously allocated statement\n"); @@ -431,20 +431,20 @@ void test_stack_alloc_free_resize(unsigned char *buffer, U32 buffer_size) "It should have the same memory address as memory allocation #3\n"); } -void test_stack_resize_n2_low_space(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_n2_low_space(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; size_t head_n1 = s.prev_offset; size_t tail_n1 = s.curr_offset; qres = stack_alloc(&s, s.capacity - 64); - U8* n2 = (U8*)qres.memory; + u8* n2 = (u8*)qres.memory; *n2 = 2; size_t head_n2 = s.prev_offset; size_t tail_n2 = s.curr_offset; @@ -455,7 +455,7 @@ void test_stack_resize_n2_low_space(unsigned char *buffer, U32 buffer_size) "Reason: Failed to catch resize with size larger than available space. \n"); } -void test_stack_alloc_negative_size(unsigned char *buffer, U32 buffer_size) +void test_stack_alloc_negative_size(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); @@ -464,19 +464,19 @@ void test_stack_alloc_negative_size(unsigned char *buffer, U32 buffer_size) // so we get a really large number, that actually ends up looping around and becomes less than the array. // so we have a bounds check for that. stack_alloc(&s, 16); - struct res_void qres = stack_alloc(&s, -16); + struct ResVoid qres = stack_alloc(&s, -16); assert((qres.status == MEM_FULL) && "Test Stack Alloc Negative Size Failed\n" "Reason: Failed to catch allocation with -ve size. \n" "That will translate to be a very large size since the size variable is a size_t (unsigned)\n"); } -void test_stack_resize_negative_size(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_negative_size(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - struct res_void qres = stack_alloc(&s, 16); - S8* n1 = (S8*)qres.memory; + struct ResVoid qres = stack_alloc(&s, 16); + s8* n1 = (s8*)qres.memory; *n1 = -20; qres = stack_resize(&s, n1, 16, -16); @@ -485,26 +485,26 @@ void test_stack_resize_negative_size(unsigned char *buffer, U32 buffer_size) "That will translate to be a very large size since the size variable is a size_t (unsigned)\n"); } -void test_stack_resize_tail_negative_size(unsigned char *buffer, U32 buffer_size) +void test_stack_resize_tail_negative_size(unsigned char *buffer, u32 buffer_size) { struct stack s = {0}; stack_init(&s, buffer, buffer_size); - U8 ele_sz = 16; - struct res_void qres = stack_alloc(&s, ele_sz); - U8* n1 = (U8*)qres.memory; + u8 ele_sz = 16; + struct ResVoid qres = stack_alloc(&s, ele_sz); + u8* n1 = (u8*)qres.memory; *n1 = 1; qres = stack_resize(&s, n1, ele_sz, -8); - U8* n2 = (U8*)qres.memory; + u8* n2 = (u8*)qres.memory; assert((qres.status == MEM_FULL) && "Stack Resize tail Negative Size Failed: " "Reason: Failed to catch resize with -ve size. \n" "That will translate to be a very large size since the size variable is a size_t (unsigned)\n"); } int main(int argc, char** argv) { - U32 sz = 256*sizeof(U8); - U8 *buffer = (U8 *)malloc(sz); + u32 sz = 256*sizeof(u8); + u8 *buffer = (u8 *)malloc(sz); // --------- ARENA --------- // printf("\n===== Testing Arena Allocator =====\n"); test_arena_init(buffer, sz); diff --git a/strings/strings.cpp b/strings/strings.cpp deleted file mode 100644 index 4c8486f..0000000 --- a/strings/strings.cpp +++ /dev/null @@ -1,31 +0,0 @@ -u64 CstrSize(const char* cstr) -{ - u8 *iter = (u8*)cstr; - u64 size = 0; - for(;*iter != 0; *iter++) { - size += 1; - } - - return size; -} - -struct str8 Str8(u8 *string, u64 size) -{ - struct str8 result = {string, size}; - return result; -} - -struct res_str8 Str8InitCstr(struct arena *a, const char* cstr) -{ - u64 cstr_size = CstrSize(cstr); - struct res_void cstr_res = arena_alloc(a, cstr_size); - u8 *str = (u8 *)cstr_res.memory; - u64 str_size = cstr_res.bytes_count; - - MemCopy((void*)str, (void*)cstr, str_size*sizeof(u8)); - - struct res_str8 result = {}; - result.string = Str8(str, str_size); - result.bytes_count = str_size; - return result; -} diff --git a/strings/strings.h b/strings/strings.h deleted file mode 100644 index dbaff7a..0000000 --- a/strings/strings.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef AMR_STR -#define AMR_STR - -#include <assert.h> -#include <string.h> - -#define MemCopy memcpy - -// @todo: the function names have been changed a bit for the interest -// of speed. Some structs and functions share the same names, so -// I need to fix this but also make sure the functions have good names - -enum StrStatus {STR_OK=0, STR_FULL}; - -struct str8 { - u8 *buffer; - u64 size; -}; - -struct str8_node { - str8_node *next; - str8 string; -}; - -struct str8_list { - str8_node *first; - str8_node *last; - u64 count; - u64 capacity; -}; - -struct res_str8 { - str8 string; - u64 bytes_count; - enum StrStatus status; -}; - -struct str8 Str8(u8 *string, u64 size); -u64 CstrSize(const char* cstr); -struct res_str8 Str8InitCstr(struct arena *a, const char* cstr, u64 size); - -#endif diff --git a/strings/test.cpp b/strings/test.cpp deleted file mode 100644 index 49f81c9..0000000 --- a/strings/test.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include <stdio.h> -#include "strings.h" -#include "strings.cpp" - -void TestCstrSize() -{ - u64 size = CstrSize("hello sailor\n"); - assert(size == 13 && "test_cstr_size() failed" - "the cstr size was wrong\n"); -} - -void TestEmptyCstrSize() -{ - u64 size = CstrSize(""); - assert(size == 0 && "test_empty_cstr_size() failed" - "the cstr size was wrong\n"); -} - -void TestStr8InitCstr(u8* buffer, u64 buffer_size) -{ - struct arena a = {}; - arena_init(&a, buffer, buffer_size); - struct res_str8 qres = Str8InitCstr(&a, "hello sailor"); - assert((qres.bytes_count == 12) && "Error TestStr8InitCstr:\n" - "Reason: Invalid size of allocated string\n" - "Hint: check what is happening to the bytes_count and how it is set first\n"); -} - -void TestStr8InitCstrEmpty(u8* buffer, u64 buffer_size) -{ - struct arena a = {}; - arena_init(&a, buffer, buffer_size); - struct res_str8 qres = Str8InitCstr(&a, ""); - assert((qres.string.size == 0) && "Error TestStr8InitCstrEmpty:\n" - "Reason: Invalid size of allocated string\n" - "Hint: check what is happening to the bytes_count and how it is set first\n"); -} - -void TestStr8InitCstrNoSpace(u8* buffer, u8 buffer_size) -{ - struct arena a = {}; - arena_init(&a, buffer, buffer_size); - struct res_str8 qres = Str8InitCstr(&a, "hello sailor, looks like you've got no space here on this boat!"); - assert(qres.string.size == 0 && "Error TestStr8InitStrNoSpace:\n" - "Reason: the bytes count is not 0, even though the string is larger than the supposedly available\n" - "arena space. This is an error and should not occur.\n" - "Hint: Look at what is going on in the allocation since that arena helpers handle allocations\n."); -} - -void test_strings() -{ - printf("\n======= Testing Strings Library =======\n"); - printf("===== Testing Cstring Functions =====\n"); - TestCstrSize(); - TestEmptyCstrSize(); - - u64 buffer_size = KB(64); - u8* buffer = (u8*)malloc(sizeof(u8)*buffer_size); - - TestStr8InitCstr(buffer, buffer_size); - TestStr8InitCstrEmpty(buffer, buffer_size); - TestStr8InitCstrNoSpace(buffer, 8); - printf("Cstring tests passed successfully\n"); - free(buffer); -} @@ -3,17 +3,22 @@ #include <stdint.h> -typedef int8_t S8; -typedef int16_t S16; -typedef int32_t S32; -typedef int64_t S64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; -typedef uint8_t U8; -typedef uint16_t U16; -typedef uint32_t U32; -typedef uint64_t U64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +#if FIGURE_IF_PLATFORM_SPECIFIC +// @todo: Before including this, I need to make sure this is platform independant. If this is platform dependant, +// I need to see if it is worth the hassle setting up something for each platform/compiler #pragma section(".roglob", read) #define read_only __declspec(allocate(".roglob")) +#endif -#endif // TYPES_H
\ No newline at end of file +#endif // TYPES_H |