diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 21:56:42 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 21:56:42 +0100 |
| commit | 176b2b462371cd860ac9c6b5d2c1db92ae707e88 (patch) | |
| tree | 983d1ddbae380eca94dfebfaf15fdf3245ba284c | |
| parent | cb95ef0b3690e7817db34e3a7c0d7c021a7a8ccf (diff) | |
dont need array
| -rw-r--r-- | src/array.cc | 31 | ||||
| -rw-r--r-- | src/common.cc | 4 | ||||
| -rw-r--r-- | src/memory.cc | 5 | ||||
| -rw-r--r-- | src/source.cc | 55 | ||||
| -rw-r--r-- | src/voidc.cc | 1 |
5 files changed, 65 insertions, 31 deletions
diff --git a/src/array.cc b/src/array.cc index f522bfc..35b8272 100644 --- a/src/array.cc +++ b/src/array.cc @@ -16,25 +16,26 @@ struct Array { #define array_init(T, allocator) \ Array<T> { 0, 0, nullptr, allocator } -template<typename T> +template <typename T> static inline usize array_nbytes(usize size) { assert_neq(size, 0); return size * sizeof(T); } -template<typename T> +template <typename T> static inline usize array_next_capacity(const Array<T>* array) { assert_neq(array, nullptr); return array->capacity == 0 ? ARRAY_INIT : array->capacity * ARRAY_GROWTH; } -template<typename T> +template <typename T> bool array_resize(Array<T>* array, usize new_capacity) { assert_neq(array, nullptr); assert_st(array->size, new_capacity); - T* new_data = (T*)allocate(array->allocator, array_nbytes<T>(new_capacity), alignof(T)); - if(new_data == nullptr) return false; + T* new_data = + (T*)allocate(array->allocator, array_nbytes<T>(new_capacity), alignof(T)); + if (new_data == nullptr) return false; memcpy(new_data, array->data, array_nbytes<T>(new_capacity)); deallocate(array->allocator, (u8*)array->data); @@ -44,18 +45,18 @@ bool array_resize(Array<T>* array, usize new_capacity) { return true; } -template<typename T> +template <typename T> static inline bool array_grow_if_needed(Array<T>* array) { assert_neq(array, nullptr); assert_ste(array->size, array->capacity); - - if(array->size < array->capacity) return true; + + if (array->size < array->capacity) return true; usize new_capacity = array_next_capacity(array); return array_resize(array, new_capacity); } -template<typename T> +template <typename T> static inline T* array_at(const Array<T>* array, usize index) { assert_neq(array, nullptr); assert_st(index, array->capacity); @@ -63,21 +64,21 @@ static inline T* array_at(const Array<T>* array, usize index) { return array->data + index; } -template<typename T> +template <typename T> inline T* array_last(const Array<T>* array) { assert_neq(array, nullptr); - if(array->size == 0) return nullptr; + if (array->size == 0) return nullptr; return array_at(array, array->size - 1); } -template<typename T> +template <typename T> bool array_push(Array<T>* array, T* item) { assert_neq(array, nullptr); assert_neq(item, nullptr); bool ret = array_grow_if_needed(array); - if(!ret) return false; + if (!ret) return false; T* loc = array_at(array, array->size); memcpy(loc, item, sizeof(T)); @@ -89,11 +90,11 @@ bool array_push(Array<T>* array, T* item) { template <typename T> void array_deinit(Array<T>* array) { assert_neq(array, nullptr); - + if (unlikely(array->data == nullptr)) return; deallocate(array->allocator, (u8*)array->data); - memset(array, 0, sizeof(*array)); + memset(array, 0, sizeof(*array)); } #endif diff --git a/src/common.cc b/src/common.cc index fd2a843..64c462b 100644 --- a/src/common.cc +++ b/src/common.cc @@ -25,6 +25,8 @@ typedef intptr_t isize; /* intrinsics */ #define likely(cond) __builtin_expect(!!(cond), 1) #define unlikely(cond) __builtin_expect(!!(cond), 0) +#define containerof(type, member, ptr) \ + ((type *)((char *)(ptr) - offsetof(type, member))) /* error handling sort of */ [[noreturn]] void panic_impl(const char* file, i32 line, const char* fmt, ...) { @@ -81,7 +83,7 @@ template <typename T> bool slice_write(const Slice<T>* slice, FILE* stream) { assert_neq(slice, nullptr); assert_neq(stream, nullptr); - + usize rc = fwrite(slice->ptr, sizeof(T), slice->length, stream); if (rc == 0 || slice->size() > rc) return false; return true; diff --git a/src/memory.cc b/src/memory.cc index cf8f88c..bb0ecb1 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -16,6 +16,11 @@ struct Allocator { Allocator_Deallocate deallocate; }; +template<typename T> +T* allocate(const Allocator* allocator, usize n) { + return allocator->allocate(allocator->self, sizeof(T) * n, alignof(T)); +} + u8* allocate(const Allocator* allocator, usize size, usize align) { return allocator->allocate(allocator->self, size, align); } diff --git a/src/source.cc b/src/source.cc index 69f1941..02dd42e 100644 --- a/src/source.cc +++ b/src/source.cc @@ -21,20 +21,27 @@ struct Buffer { const Allocator* allocator; }; -bool buffer_init(Buffer* buffer, const Allocator* allocator, - const String* content_in, const String* file_in) { - assert_neq(buffer, nullptr); +bool buffer_init(const Allocator* allocator, + const String* content_in, const String* file_in, Buffer** out) { assert_neq(allocator, nullptr); assert_neq(content_in, nullptr); assert_neq(file_in, nullptr); + assert_neq(out, nullptr); + + Buffer* buffer = allocate<Buffer>(allocator, 1); + if(buffer == nullptr) return false; String content, file; bool ret = slice_copy(allocator, content_in, &content); - if (unlikely(!ret)) return false; + if (unlikely(!ret)) { + deallocate(allocator, (u8*)buffer); + return false; + } ret = slice_copy(allocator, file_in, &file); if (unlikely(!ret)) { slice_deallocate(allocator, &content); + deallocate(allocator, (u8*)buffer); return false; } @@ -42,40 +49,60 @@ bool buffer_init(Buffer* buffer, const Allocator* allocator, buffer->content = content; buffer->allocator = allocator; buffer->link = {}; + + *out = buffer; return true; } void buffer_deinit(Buffer* buffer) { assert_neq(buffer, nullptr); + const Allocator* allocator = buffer->allocator; + if(allocator == nullptr) return; + slice_deallocate(buffer->allocator, &buffer->file); slice_deallocate(buffer->allocator, &buffer->content); - memset(buffer, 0, sizeof(*buffer)); + + deallocate(buffer->allocator, (u8*)buffer); } struct Buffer_Manager { Link* stack; - Array<Buffer> buffers; + Array<Buffer*> buffers; }; #define buffer_manager_init(allocator) \ - Buffer_Manager { nullptr, array_init(Buffer, allocator) } + Buffer_Manager { nullptr, array_init(Buffer*, allocator) } bool buffer_manager_push(Buffer_Manager* manager, Buffer* b) { assert_neq(manager, nullptr); assert_neq(b, nullptr); bool ret = array_push(&manager->buffers, b); - if(!ret) return false; + if (!ret) return false; - b = array_last(&manager->buffers); - if(manager->stack == nullptr) { - manager->stack = &b->link; - return true; - } + b = *array_last(&manager->buffers); + if (manager->stack != nullptr) link_after(manager->stack, &b->link); + + manager->stack = &b->link; + return true; +} + +bool buffer_manager_pop(Buffer_Manager* manager, Buffer** b) { + assert_neq(manager, nullptr); + assert_neq(b, nullptr); + + Link* link = manager->stack; + if (link == nullptr) return false; + + Link* next = link->prev; + link_remove(link); + + manager->stack = next; - link_after(manager->stack, &b->link); + Buffer* buffer = containerof(Buffer, link, link); + *b = buffer; return true; } diff --git a/src/voidc.cc b/src/voidc.cc index be97c41..8cf9a63 100644 --- a/src/voidc.cc +++ b/src/voidc.cc @@ -28,6 +28,5 @@ int main() { buffer_manager_push(&manager, &buffer); - buffer_manager_deinit(&manager); } |
