From ffa09db5138dea853c910a0307fe5063511abb9f Mon Sep 17 00:00:00 2001 From: Fabrice Date: Sun, 1 Mar 2026 22:03:55 +0100 Subject: fixing type errors working on strings --- src/common.cc | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'src/common.cc') diff --git a/src/common.cc b/src/common.cc index aa734b3..ef790b2 100644 --- a/src/common.cc +++ b/src/common.cc @@ -2,6 +2,7 @@ #define COMMON_CC #include +#include /* typedefs for common types */ @@ -25,32 +26,25 @@ struct Slice { T* ptr; usize length; + Slice() : ptr(nullptr), length(0) {} Slice(T* ptr, usize length) : ptr(ptr), length(length) {} T* operator[](usize index) { return ptr + index; } -}; - -typedef Slice String; - -/* allocator handling */ -typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align); -typedef void (*Allocator_Deallocate)(u8* self, u8* ptr); - -struct Allocator { - u8* self; - Allocator_Allocate allocate; - Allocator_Deallocate deallocate; + inline usize size() const { + return this->length * sizeof(T); + } }; -u8* allocate(Allocator* allocator, usize size, usize align) { - return allocator->allocate(allocator->self, size, align); -} +template +bool slice_write(const Slice* slice, FILE* stream) { + usize rc = fwrite(slice->ptr, sizeof(T), slice->length, stream); + if(rc == 0 || slice->size() > rc) return false; + return true; +} -void deallocate(Allocator* allocator, u8* ptr) { - allocator->deallocate(allocator->self, ptr); -} +typedef Slice String; #endif -- cgit v1.2.3