From 99988a9001cf0340c6f953009773a566f106b843 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 29 Apr 2023 16:21:28 -0500 Subject: [PATCH] changed: alloc.fixed, added methods to Allocator --- core/alloc/fixed.onyx | 39 ++++++++++++++++++++------------------- core/builtin.onyx | 6 ++++++ core/memory/memory.onyx | 8 ++++++++ core/net/tcp.onyx | 3 --- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/core/alloc/fixed.onyx b/core/alloc/fixed.onyx index 261ce258..ac37b4e8 100644 --- a/core/alloc/fixed.onyx +++ b/core/alloc/fixed.onyx @@ -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; } diff --git a/core/builtin.onyx b/core/builtin.onyx index 49c6e242..c04f2d75 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -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); diff --git a/core/memory/memory.onyx b/core/memory/memory.onyx index 7e7f5b64..fbf3d3a6 100644 --- a/core/memory/memory.onyx +++ b/core/memory/memory.onyx @@ -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; +} diff --git a/core/net/tcp.onyx b/core/net/tcp.onyx index 0408c81c..feb22025 100644 --- a/core/net/tcp.onyx +++ b/core/net/tcp.onyx @@ -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 { -- 2.25.1