diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 12:29:14 +0100 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-03-02 12:29:14 +0100 |
| commit | 74933654160064f9303551a6c012be6b88d5b626 (patch) | |
| tree | 95191a4335f1a59a407cde9cba010a6c59ebab41 /src | |
| parent | df736f5a97b329377e45adb8d734fd58ea9b14ac (diff) | |
creating and removing buffer
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.cc | 25 | ||||
| -rw-r--r-- | src/memory.cc | 10 | ||||
| -rw-r--r-- | src/source.cc | 39 | ||||
| -rw-r--r-- | src/voidc.cc | 17 |
4 files changed, 73 insertions, 18 deletions
diff --git a/src/common.cc b/src/common.cc index 141b079..df8b9aa 100644 --- a/src/common.cc +++ b/src/common.cc @@ -5,6 +5,7 @@ #include <cstdint> #include <cstdio> #include <cstdlib> +#include <cstring> /* typedefs for common types */ @@ -55,8 +56,9 @@ struct Slice { T* ptr; usize length; - Slice() : ptr(nullptr), length(0) {} + Slice() : Slice(nullptr, 0) {} Slice(T* ptr, usize length) : ptr(ptr), length(length) {} + Slice(const char* str) : ptr((u8*)str), length(strlen(str)) {} T* operator[](usize index) { assert(index < this->length); @@ -86,14 +88,10 @@ struct Link { Link* next; Link* prev; + Link() : Link(nullptr, nullptr) {} Link(Link* next, Link* prev) : next(next), prev(prev) {} }; -struct List { - Link head; - Link tail; -}; - static inline void link_after(Link* prev, Link* nlink) { assert(prev != nullptr); assert(nlink != nullptr); @@ -108,4 +106,19 @@ static inline void link_after(Link* prev, Link* nlink) { next->prev = nlink; } +static inline void link_remove(Link* item) { + assert(item != nullptr); + + Link* prev = item->prev; + Link* next = item->next; + + if(prev != nullptr) + prev->next = next; + + if(next != nullptr) + next->prev = prev; + + item->prev = item->next = nullptr; +} + #endif diff --git a/src/memory.cc b/src/memory.cc index 2d83413..1cb6cb9 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -16,11 +16,11 @@ struct Allocator { Allocator_Deallocate deallocate; }; -u8* allocate(Allocator* allocator, usize size, usize align) { +u8* allocate(const Allocator* allocator, usize size, usize align) { return allocator->allocate(allocator->self, size, align); } -void deallocate(Allocator* allocator, u8* ptr) { +void deallocate(const Allocator* allocator, u8* ptr) { allocator->deallocate(allocator->self, ptr); } @@ -34,14 +34,14 @@ static inline void heap_deallocate(u8* self, u8* ptr) { mi_free(ptr); } -Allocator* heap_allocator() { +const Allocator* heap_allocator() { thread_local Allocator HEAP_ALLOCATOR = {nullptr, heap_allocate, heap_deallocate}; return &HEAP_ALLOCATOR; } template <typename T> -bool slice_copy(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; @@ -53,7 +53,7 @@ bool slice_copy(Allocator* allocator, const Slice<T>* source, Slice<T>* out) { } template <typename T> -void slice_deallocate(Allocator* allocator, Slice<T>* slice) { +void slice_deallocate(const Allocator* allocator, Slice<T>* slice) { if (slice->ptr == nullptr) return; deallocate(allocator, (u8*)slice->ptr); diff --git a/src/source.cc b/src/source.cc index 9234ea9..d714f29 100644 --- a/src/source.cc +++ b/src/source.cc @@ -4,9 +4,46 @@ #include "common.cc" #include "memory.cc" +typedef u32 Source_Id; + +struct Span { + Source_Id id; + usize start, end; + + Span(Source_Id id, usize start, usize end) : id(id), start(start), end(end) {} +}; + struct Buffer { - String content; String file; + String content; + Link link; +}; + +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; + + ret = slice_copy(allocator, file_in, &file); + if(unlikely(ret == false)) { + slice_deallocate(allocator, &content); + return false; + } + + buffer->file = file; + buffer->content = content; + buffer->link = {}; + return true; +} + +void buffer_deinit(Buffer* buffer, const Allocator* allocator) { + slice_deallocate(allocator, &buffer->file); + slice_deallocate(allocator, &buffer->content); +} + +struct Buffer_Stack { + Link* buffers; + usize nbuffers; }; #endif diff --git a/src/voidc.cc b/src/voidc.cc index 8110184..5bbd7f4 100644 --- a/src/voidc.cc +++ b/src/voidc.cc @@ -1,5 +1,7 @@ +#include <cstdlib> #include <cstring> +#include "source.cc" #include "common.cc" #include "memory.cc" @@ -11,13 +13,16 @@ int main() { } )"; -static const String SOURCE_STRING = {(u8*)SOURCE, strlen(SOURCE)}; -int main() { - String source; - slice_copy(heap_allocator(), &SOURCE_STRING, &source); +static const char* SOURCE_FILE = "source.c"; - slice_write(&source, stdout); +int main() { + String source(SOURCE); + String file(SOURCE_FILE); + + Buffer buffer{}; + bool ret = buffer_init(&buffer, heap_allocator(), &source, &file); + if(!ret) return EXIT_FAILURE; - slice_deallocate(heap_allocator(), &source); + } |
