changed: alloc.fixed, added methods to Allocator
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 Apr 2023 21:21:28 +0000 (16:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 Apr 2023 21:21:28 +0000 (16:21 -0500)
core/alloc/fixed.onyx
core/builtin.onyx
core/memory/memory.onyx
core/net/tcp.onyx

index 261ce258c81a27206e86ef9f29384421c926ecdb..ac37b4e83b7f6c07b36a7d0b53eaf0b72d4d71be 100644 (file)
@@ -2,8 +2,9 @@ package core.alloc.fixed
 
 use core
 
-// This allocator is very simple and always returns the same pointer,
-// unless too much memory is asked for, in which case it returns null.
+// This allocator is very simple. It is simply a bump allocator from
+// a fixed size buffer. It cannot free or resize, and will return null
+// when it has used all memory in the buffer given to it.
 //
 // This kind of allocator is useful for temporary string building or
 // similar circumstances, where you know that the needed memory size
@@ -12,32 +13,32 @@ use core
 // can continue to use the same code that does allocations like normal,
 // but can get the speed increase of a simple allocation strategy.
 
-FixedAllocatorData :: struct {
-    ptr  : rawptr;
-    size : u32;
+FixedAllocator :: struct {
+    buffer: [] u8;
+    end: u32;
 }
 
 #local
 fixed_allocator_proc :: (data: rawptr, aa: AllocationAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {
-    fa_data := cast(&FixedAllocatorData) data;  
+    fa_data := cast(&FixedAllocator) data;  
 
     if aa != .Alloc do return null;
-    if size > fa_data.size do return null;
+    if size > fa_data.buffer.count - fa_data.end do return null;
+
+    aligned_end := cast(u32) core.memory.align(fa_data.end, align);
+    defer fa_data.end = aligned_end + size;
     
-    return fa_data.ptr;
+    return fa_data.buffer.data + aligned_end;
 }
 
-make :: (ptr: rawptr, size: u32) -> FixedAllocatorData {
-    return FixedAllocatorData.{
-        ptr  = ptr,
-        size = size,
-    };
-}
+make :: (buffer: [] u8) => FixedAllocator.{ buffer, 0 }
 
 #match core.alloc.as_allocator make_allocator
-make_allocator :: (fa_data: &FixedAllocatorData) -> Allocator {
-    return Allocator.{
-        func = fixed_allocator_proc,
-        data = fa_data,
-    };
+make_allocator :: (fa_data: &FixedAllocator) => Allocator.{
+    func = fixed_allocator_proc,
+    data = fa_data,
+}
+
+reset :: (data: &FixedAllocator) {
+    data.end = 0;
 }
index 49c6e24275874682aa79642066c43c1f2189a577..c04f2d75ce5a8abdf6847543b2c33f630f6a9811 100644 (file)
@@ -263,6 +263,12 @@ raw_free :: (use a: Allocator, ptr: rawptr) {
     func(data, AllocationAction.Free, 0, 0, ptr);
 }
 
+#inject Allocator {
+    alloc :: raw_alloc
+    resize :: raw_resize
+    free :: raw_free
+}
+
 //
 // Helper function to allocate/free using allocator in the context structure.
 calloc  :: (size: u32)              => raw_alloc(context.allocator, size);
index 7e7f5b645e674ae056fc3cbe2eaa27ec5cfd6745..fbf3d3a6847d6a0f8b86002571787df52108c9db 100644 (file)
@@ -50,3 +50,11 @@ align :: (size: u64, align: u64) -> u64 {
     }
     return size;
 }
+
+#overload
+align :: (size: u32, align: u32) -> u32 {
+    if size % align != 0 {
+        size += align - (size % align);
+    }
+    return size;
+}
index 0408c81cf37b5d76da9122c7e2d2dea7256d6aa4..feb22025ed28996a03eaca1c78ebbb55b62d375e 100644 (file)
@@ -107,8 +107,6 @@ TCP_Server :: struct {
     clients: [] &Client;
     client_count: u32; // max clients is stored as clients.count.
 
-    listener_thread: thread.Thread;
-
     alive         := true;
     pulse_time_ms := 500;
 
@@ -184,7 +182,6 @@ tcp_server_stop :: (use server: &TCP_Server) {
     }
 
     server.socket->close();
-    thread.kill(&listener_thread);
 }
 
 tcp_server_pulse :: (use server: &TCP_Server) -> bool {