diff options
| author | talha <talha@talhaamir.xyz> | 2024-02-15 09:58:42 +0500 | 
|---|---|---|
| committer | talha <talha@talhaamir.xyz> | 2024-02-15 09:58:42 +0500 | 
| commit | ab3edaa58eed4ff73410954ca094531d49eb5844 (patch) | |
| tree | 4dd8dc896baa5737153ed0d48f27881171fd3287 /memory/memory.h | |
added personal libraries to git tracking
Diffstat (limited to 'memory/memory.h')
| -rw-r--r-- | memory/memory.h | 94 | 
1 files changed, 94 insertions, 0 deletions
| diff --git a/memory/memory.h b/memory/memory.h new file mode 100644 index 0000000..14504b4 --- /dev/null +++ b/memory/memory.h @@ -0,0 +1,94 @@ +#ifndef AMR_MEMORY_H +#define AMR_MEMORY_H + +#include <stddef.h> +#include <stdint.h> +#include <assert.h> +#include <string.h> + +#ifndef AMR_TYPES_H + +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; + +#endif // AMR_TYPES_H + + +#ifndef DEFAULT_ALIGNMENT +#define DEFAULT_ALIGNMENT (2*sizeof(void *)) +#endif + +// @todo: build a logging mechanism for handling errors +// maybe read about that + +enum MemStatus { MEM_OK=0, MEM_OUT_OF_BOUNDS, MEM_FULL }; + +struct res_void { +  enum MemStatus status; +  size_t bytes_count; +  void* memory; +}; + +bool is_power_of_two(uintptr_t x); + +//=========================================================================================== +// ---------------------------------- ARENA ------------------------------------------------- +//=========================================================================================== + +struct arena { +  unsigned char* buffer; +  size_t prev_offset; +  size_t curr_offset; +  size_t capacity; +}; + +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, +                                    size_t new_size, size_t align);  +void* arena_resize(struct arena* a, void* old_mem, size_t old_size, +                            size_t new_size); +void arena_clear(struct arena *a); + +//=========================================================================================== +// ---------------------------------- STACK ------------------------------------------------- +//=========================================================================================== + +/*  +* @todo: stack needs to be updated, it's really just a work in progress right now. +* The main thing is minimizing the use of compound types, since that is pretty annoying to deal with. +* I would rather write code that makes sure to collapse all possible cases and lets me just not worry about code. +* Would rather stick to worrying about data being data +*/ + +struct stack { +  unsigned char* buffer; +  size_t prev_offset; +  size_t curr_offset; +  size_t capacity; +}; + +struct stack_hdr { +  size_t prev_offset; +  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);  +enum MemStatus stack_free(struct stack* s); +struct res_void 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); + +#endif | 
