From a7ce0f95e6098872883d45bbc6148b1b08d13776 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 24 Feb 2023 00:29:24 +0100 Subject: [PATCH] Refactor allocator locations. --- lib/std/core/allocators/arena_allocator.c3 | 27 ++++++ lib/std/core/allocators/dynamic_arena.c3 | 45 ++++++++++ lib/std/core/mem_allocator.c3 | 100 --------------------- lib/std/core/os/wasm_memory.c3 | 32 +++++++ 4 files changed, 104 insertions(+), 100 deletions(-) create mode 100644 lib/std/core/os/wasm_memory.c3 diff --git a/lib/std/core/allocators/arena_allocator.c3 b/lib/std/core/allocators/arena_allocator.c3 index c49ec1219..86fbc7557 100644 --- a/lib/std/core/allocators/arena_allocator.c3 +++ b/lib/std/core/allocators/arena_allocator.c3 @@ -1,5 +1,32 @@ module std::core::mem::allocator; +struct ArenaAllocator +{ + inline Allocator allocator; + char[] data; + usz used; +} + +/** + * Initialize a memory arena for use using the provided bytes. + * + * @require this != null + **/ +fn void ArenaAllocator.init(ArenaAllocator* this, char[] data) +{ + this.function = &arena_allocator_function; + this.data = data; + this.used = 0; +} + +/** + * @require this != null + **/ +fn void ArenaAllocator.reset(ArenaAllocator* this) +{ + this.used = 0; +} + struct ArenaAllocatorHeader { usz size; diff --git a/lib/std/core/allocators/dynamic_arena.c3 b/lib/std/core/allocators/dynamic_arena.c3 index 4b5ac6004..faa1153ff 100644 --- a/lib/std/core/allocators/dynamic_arena.c3 +++ b/lib/std/core/allocators/dynamic_arena.c3 @@ -1,5 +1,50 @@ module std::core::mem::allocator; +struct DynamicArenaAllocator +{ + inline Allocator allocator; + Allocator* backing_allocator; + DynamicArenaPage* page; + DynamicArenaPage* unused_page; + usz page_size; +} + +/** + * @require page_size >= 128 + * @require this != null + **/ +fn void DynamicArenaAllocator.init(DynamicArenaAllocator* this, usz page_size, Allocator* backing_allocator = mem::current_allocator()) +{ + this.function = &dynamic_arena_allocator_function; + this.page = null; + this.unused_page = null; + this.page_size = page_size; + this.backing_allocator = backing_allocator; +} + +/** + * @require this != null + **/ +fn void DynamicArenaAllocator.destroy(DynamicArenaAllocator* this) +{ + DynamicArenaPage* page = this.page; + while (page) + { + DynamicArenaPage* next_page = page.prev_arena; + this.backing_allocator.free(page)!!; + page = next_page; + } + page = this.unused_page; + while (page) + { + DynamicArenaPage* next_page = page.prev_arena; + this.backing_allocator.free(page)!!; + page = next_page; + } + this.page = null; + this.unused_page = null; +} + struct DynamicArenaPage { void* memory; diff --git a/lib/std/core/mem_allocator.c3 b/lib/std/core/mem_allocator.c3 index c7e10361f..c8a0d3a31 100644 --- a/lib/std/core/mem_allocator.c3 +++ b/lib/std/core/mem_allocator.c3 @@ -107,105 +107,5 @@ fn usz alignment_for_allocation(usz alignment) @inline @private return alignment; } -struct DynamicArenaAllocator -{ - inline Allocator allocator; - Allocator* backing_allocator; - DynamicArenaPage* page; - DynamicArenaPage* unused_page; - usz page_size; -} - -/** - * @require page_size >= 128 - * @require this != null - **/ -fn void DynamicArenaAllocator.init(DynamicArenaAllocator* this, usz page_size, Allocator* backing_allocator = mem::current_allocator()) -{ - this.function = &dynamic_arena_allocator_function; - this.page = null; - this.unused_page = null; - this.page_size = page_size; - this.backing_allocator = backing_allocator; -} - -/** - * @require this != null - **/ -fn void DynamicArenaAllocator.destroy(DynamicArenaAllocator* this) -{ - DynamicArenaPage* page = this.page; - while (page) - { - DynamicArenaPage* next_page = page.prev_arena; - this.backing_allocator.free(page)!!; - page = next_page; - } - page = this.unused_page; - while (page) - { - DynamicArenaPage* next_page = page.prev_arena; - this.backing_allocator.free(page)!!; - page = next_page; - } - this.page = null; - this.unused_page = null; -} -struct ArenaAllocator -{ - inline Allocator allocator; - char[] data; - usz used; -} - -/** - * Initialize a memory arena for use using the provided bytes. - * - * @require this != null - **/ -fn void ArenaAllocator.init(ArenaAllocator* this, char[] data) -{ - this.function = &arena_allocator_function; - this.data = data; - this.used = 0; -} - -/** - * @require this != null - **/ -fn void ArenaAllocator.reset(ArenaAllocator* this) -{ - this.used = 0; -} - -const usz WASM_BLOCK_SIZE = 65536; - -WasmMemory wasm_memory; - -struct WasmMemory -{ - usz allocation; - uptr use; -} - -fn char[]! WasmMemory.allocate_block(WasmMemory* this, usz bytes) -{ - if (!this.allocation) - { - this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE; - } - isz bytes_required = bytes + this.use - this.allocation; - if (bytes_required <= 0) - { - defer this.use += bytes; - return ((char*)this.use)[:bytes]; - } - - usz blocks_required = (bytes_required + WASM_BLOCK_SIZE + 1) / WASM_BLOCK_SIZE; - if ($$wasm_memory_grow(0, blocks_required) == -1) return AllocationFailure.OUT_OF_MEMORY!; - this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE; - defer this.use += bytes; - return ((char*)this.use)[:bytes]; -} \ No newline at end of file diff --git a/lib/std/core/os/wasm_memory.c3 b/lib/std/core/os/wasm_memory.c3 new file mode 100644 index 000000000..e0be13900 --- /dev/null +++ b/lib/std/core/os/wasm_memory.c3 @@ -0,0 +1,32 @@ +module std::core::mem::allocator; + + +const usz WASM_BLOCK_SIZE = 65536; + +WasmMemory wasm_memory; + +struct WasmMemory +{ + usz allocation; + uptr use; +} + +fn char[]! WasmMemory.allocate_block(WasmMemory* this, usz bytes) +{ + if (!this.allocation) + { + this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE; + } + isz bytes_required = bytes + this.use - this.allocation; + if (bytes_required <= 0) + { + defer this.use += bytes; + return ((char*)this.use)[:bytes]; + } + + usz blocks_required = (bytes_required + WASM_BLOCK_SIZE + 1) / WASM_BLOCK_SIZE; + if ($$wasm_memory_grow(0, blocks_required) == -1) return AllocationFailure.OUT_OF_MEMORY!; + this.allocation = $$wasm_memory_size(0) * WASM_BLOCK_SIZE; + defer this.use += bytes; + return ((char*)this.use)[:bytes]; +} \ No newline at end of file