diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 12:43:25 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 12:43:25 +0100 |
| commit | 01fe9f2eeab8b54487a4673e74851160adb1718d (patch) | |
| tree | d95bcb5c81a76660c01fa8434bf59613b6ac7586 | |
| parent | ef816e31ba15bbda487298339c5ca88456c31d33 (diff) | |
working on array implementation
| -rw-r--r-- | src/array.cc | 22 | ||||
| -rw-r--r-- | src/common.cc | 12 | ||||
| -rw-r--r-- | src/memory.cc | 3 | ||||
| -rw-r--r-- | src/source.cc | 25 | ||||
| -rw-r--r-- | src/voidc.cc | 10 |
5 files changed, 51 insertions, 21 deletions
diff --git a/src/array.cc b/src/array.cc index 0238c05..7c8cc19 100644 --- a/src/array.cc +++ b/src/array.cc @@ -1,9 +1,27 @@ #ifndef ARRAY_CC #define ARRAY_CC -template<typename T> +#include "memory.cc" + +#define ARRAY_INIT 4 +#define ARRAY_GROWTH 2 + +template <typename T> struct Array { - + Slice<T> buffer; + const Allocator* allocator; }; +#define array_init(T, allocator) Array<T>{Slice<T>{}, allocator} + +template<typename T> +void array_deinit(Array<T>* array) { + assert(array != nullptr); + + if(unlikely(array->allocator == nullptr)) return; + + slice_deallocate(array->allocator, &array->buffer); + array->allocator = nullptr; +} + #endif diff --git a/src/common.cc b/src/common.cc index df8b9aa..36ddbeb 100644 --- a/src/common.cc +++ b/src/common.cc @@ -44,8 +44,8 @@ typedef intptr_t isize; #define spanic(msg) panic("%s", msg) #undef assert -#define assert(cond) \ - do { \ +#define assert(cond) \ + do { \ if (unlikely(!(cond))) panic("assertion failed: %s", #cond); \ } while (0) @@ -102,7 +102,7 @@ static inline void link_after(Link* prev, Link* nlink) { nlink->next = next; prev->next = nlink; - if(next == nullptr) return; + if (next == nullptr) return; next->prev = nlink; } @@ -112,11 +112,9 @@ static inline void link_remove(Link* item) { Link* prev = item->prev; Link* next = item->next; - if(prev != nullptr) - prev->next = next; + if (prev != nullptr) prev->next = next; - if(next != nullptr) - next->prev = prev; + if (next != nullptr) next->prev = prev; item->prev = item->next = nullptr; } diff --git a/src/memory.cc b/src/memory.cc index 1cb6cb9..cf8f88c 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -41,7 +41,8 @@ const Allocator* heap_allocator() { } template <typename T> -bool slice_copy(const Allocator* allocator, const Slice<T>* source, Slice<T>* out) { +bool slice_copy(const Allocator* allocator, const Slice<T>* source, + Slice<T>* out) { T* new_ptr = allocate(allocator, source->size(), alignof(T)); if (new_ptr == nullptr) return false; diff --git a/src/source.cc b/src/source.cc index 63fbdea..8129df8 100644 --- a/src/source.cc +++ b/src/source.cc @@ -3,6 +3,7 @@ #include "common.cc" #include "memory.cc" +#include "array.cc" typedef u32 Source_Id; @@ -10,7 +11,7 @@ struct Span { Source_Id id; usize start, end; - Span(Source_Id id, usize start, usize end) : id(id), start(start), end(end) {} + Span(Source_Id id, usize start, usize end) : id(id), start(start), end(end) {} }; struct Buffer { @@ -20,13 +21,14 @@ struct Buffer { const Allocator* allocator; }; -bool buffer_init(Buffer* buffer, const Allocator* allocator, const String* content_in, const String* file_in) { +bool buffer_init(Buffer* buffer, const Allocator* allocator, + const String* content_in, const String* file_in) { String content, file; bool ret = slice_copy(allocator, content_in, &content); - if(unlikely(ret == false)) return false; - + if (unlikely(ret == false)) return false; + ret = slice_copy(allocator, file_in, &file); - if(unlikely(ret == false)) { + if (unlikely(ret == false)) { slice_deallocate(allocator, &content); return false; } @@ -44,9 +46,16 @@ void buffer_deinit(Buffer* buffer) { buffer->allocator = nullptr; } -struct Buffer_Stack { - Link* buffers; - usize nbuffers; +struct Buffer_Manager { + Link* stack; + Array<Buffer> buffers; }; +#define buffer_manager_init(allocator) Buffer_Manager{nullptr, buffer_init(allocator)} + +void buffer_manager_deinit(Buffer_Manager* stack) { + assert(stack != nullptr); + array_deinit(&stack->buffers); +} + #endif diff --git a/src/voidc.cc b/src/voidc.cc index 69dc4ed..d536acb 100644 --- a/src/voidc.cc +++ b/src/voidc.cc @@ -1,9 +1,9 @@ #include <cstdlib> #include <cstring> -#include "source.cc" #include "common.cc" #include "memory.cc" +#include "source.cc" static const char* SOURCE = R"( #include <stdlib.h> @@ -19,10 +19,14 @@ static const char* SOURCE_FILE = "source.c"; int main() { String source(SOURCE); String file(SOURCE_FILE); - + + Buffer_Manager manager = buffer_manager_init(heap_allocator()); + Buffer buffer{}; bool ret = buffer_init(&buffer, heap_allocator(), &source, &file); - if(!ret) return EXIT_FAILURE; + if (!ret) return EXIT_FAILURE; buffer_deinit(&buffer); + + buffer_manager_deinit(&manager); } |
