summaryrefslogtreecommitdiff
path: root/src/common.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.cc')
-rw-r--r--src/common.cc30
1 files changed, 12 insertions, 18 deletions
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 <cstdint>
+#include <cstdio>
/* 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<u8> 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<typename T>
+bool slice_write(const Slice<T>* 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<const u8> String;
#endif