From 7e100472e7cf1baf79921c1893f8d5c53be40b5c Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 18 Mar 2025 18:34:52 +0100 Subject: [PATCH] - `AnyList` now also defaults to the temp allocator. - `os::getcwd` and `os::get_home_dir` requires an explicit allocator. - `file::load_new` and `file::load_path_new` removed. --- lib/std/collections/anylist.c3 | 4 +-- lib/std/core/allocators/temp_allocator.c3 | 3 ++ lib/std/core/array.c3 | 2 +- lib/std/core/mem.c3 | 36 +++++++++---------- lib/std/core/runtime_test.c3 | 2 +- lib/std/io/file.c3 | 11 ++---- lib/std/io/os/getcwd.c3 | 2 +- lib/std/io/os/temp_directory.c3 | 2 +- lib/std/math/random/math.seeder.c3 | 2 +- lib/std/os/env.c3 | 4 +-- lib/std/os/macos/darwin.c3 | 4 +-- lib/std/os/macos/objc.c3 | 2 +- lib/std/threads/fixed_pool.c3 | 2 +- releasenotes.md | 3 ++ resources/testfragments/allocators_testing.c3 | 10 +++--- 15 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/std/collections/anylist.c3 b/lib/std/collections/anylist.c3 index 6ec2af3ad..ae263be0f 100644 --- a/lib/std/collections/anylist.c3 +++ b/lib/std/collections/anylist.c3 @@ -74,7 +74,7 @@ fn usz? AnyList.to_format(&self, Formatter* formatter) @dynamic *> macro void AnyList.push(&self, element) { - if (!self.allocator) self.allocator = allocator::heap(); + if (!self.allocator) self.allocator = tmem(); self.append_internal(allocator::clone(self.allocator, element)); } @@ -166,7 +166,7 @@ fn any? AnyList.pop_first_retained(&self) <* Same as copy_pop() but pops the first value instead. *> -fn any? AnyList.copy_pop_first(&self, Allocator allocator = allocator::heap()) +fn any? AnyList.copy_pop_first(&self, Allocator allocator) { if (!self.size) return NO_MORE_ELEMENT?; defer self.free_element(self.entries[self.size]); diff --git a/lib/std/core/allocators/temp_allocator.c3 b/lib/std/core/allocators/temp_allocator.c3 index ecddc22b0..980a048d1 100644 --- a/lib/std/core/allocators/temp_allocator.c3 +++ b/lib/std/core/allocators/temp_allocator.c3 @@ -66,6 +66,9 @@ fn TempAllocator*? TempAllocator.derive_allocator(&self, usz min_size, usz buffe usz start = mem::aligned_offset(self.used + buffer, mem::DEFAULT_MEM_ALIGNMENT); void* ptr = &self.data[start]; TempAllocator* temp = (TempAllocator*)ptr; + $if env::ADDRESS_SANITIZER: + asan::unpoison_memory_region(ptr, TempAllocator.sizeof); + $endif temp.last_page = null; temp.backing_allocator = self.backing_allocator; temp.used = 0; diff --git a/lib/std/core/array.c3 b/lib/std/core/array.c3 index 3c440266a..f22c957c6 100644 --- a/lib/std/core/array.c3 +++ b/lib/std/core/array.c3 @@ -81,7 +81,7 @@ macro concat(Allocator allocator, arr1, arr2) @nodiscard @require @typeis(arr1[0], $typeof(arr2[0])) : "Arrays must have the same type" @ensure return.len == arr1.len + arr2.len *> -macro tconcat(arr1, arr2) @nodiscard => concat(allocator::temp(), arr1, arr2); +macro tconcat(arr1, arr2) @nodiscard => concat(tmem(), arr1, arr2); module std::core::array::slice{Type}; diff --git a/lib/std/core/mem.c3 b/lib/std/core/mem.c3 index e9c2063f4..94b84fd1f 100644 --- a/lib/std/core/mem.c3 +++ b/lib/std/core/mem.c3 @@ -538,7 +538,7 @@ macro void @stack_mem(usz $size; @body(Allocator mem)) @builtin { char[$size] buffer; OnStackAllocator allocator; - allocator.init(&buffer, allocator::heap()); + allocator.init(&buffer, mem); defer allocator.free(); @body(&allocator); } @@ -547,7 +547,7 @@ macro void @stack_pool(usz $size; @body) @builtin { char[$size] buffer; OnStackAllocator allocator; - allocator.init(&buffer, allocator::heap()); + allocator.init(&buffer, mem); defer allocator.free(); mem::@scoped(&allocator) { @@ -611,7 +611,7 @@ macro TrackingEnv* get_tracking_env() macro @clone(value) @builtin @nodiscard { - return allocator::clone(allocator::heap(), value); + return allocator::clone(mem, value); } macro @tclone(value) @builtin @nodiscard @@ -621,7 +621,7 @@ macro @tclone(value) @builtin @nodiscard fn void* malloc(usz size) @builtin @inline @nodiscard { - return allocator::malloc(allocator::heap(), size); + return allocator::malloc(mem, size); } <* @@ -630,13 +630,13 @@ fn void* malloc(usz size) @builtin @inline @nodiscard *> fn void* malloc_aligned(usz size, usz alignment) @builtin @inline @nodiscard { - return allocator::malloc_aligned(allocator::heap(), size, alignment)!!; + return allocator::malloc_aligned(mem, size, alignment)!!; } fn void* tmalloc(usz size, usz alignment = 0) @builtin @inline @nodiscard { if (!size) return null; - return allocator::temp().acquire(size, NO_ZERO, alignment)!!; + return tmem().acquire(size, NO_ZERO, alignment)!!; } <* @@ -758,7 +758,7 @@ macro talloc_with_padding($Type, usz padding) @nodiscard *> macro new_array($Type, usz elements) @nodiscard { - return allocator::new_array(allocator::heap(), $Type, elements); + return allocator::new_array(mem, $Type, elements); } <* @@ -767,7 +767,7 @@ macro new_array($Type, usz elements) @nodiscard *> macro new_array_aligned($Type, usz elements) @nodiscard { - return allocator::new_array_aligned(allocator::heap(), $Type, elements); + return allocator::new_array_aligned(mem, $Type, elements); } <* @@ -775,7 +775,7 @@ macro new_array_aligned($Type, usz elements) @nodiscard *> macro alloc_array($Type, usz elements) @nodiscard { - return allocator::alloc_array(allocator::heap(), $Type, elements); + return allocator::alloc_array(mem, $Type, elements); } <* @@ -784,7 +784,7 @@ macro alloc_array($Type, usz elements) @nodiscard *> macro alloc_array_aligned($Type, usz elements) @nodiscard { - return allocator::alloc_array_aligned(allocator::heap(), $Type, elements); + return allocator::alloc_array_aligned(mem, $Type, elements); } macro talloc_array($Type, usz elements) @nodiscard @@ -799,7 +799,7 @@ macro temp_array($Type, usz elements) @nodiscard fn void* calloc(usz size) @builtin @inline @nodiscard { - return allocator::calloc(allocator::heap(), size); + return allocator::calloc(mem, size); } <* @@ -808,40 +808,40 @@ fn void* calloc(usz size) @builtin @inline @nodiscard *> fn void* calloc_aligned(usz size, usz alignment) @builtin @inline @nodiscard { - return allocator::calloc_aligned(allocator::heap(), size, alignment)!!; + return allocator::calloc_aligned(mem, size, alignment)!!; } fn void* tcalloc(usz size, usz alignment = 0) @builtin @inline @nodiscard { if (!size) return null; - return allocator::temp().acquire(size, ZERO, alignment)!!; + return tmem().acquire(size, ZERO, alignment)!!; } fn void* realloc(void *ptr, usz new_size) @builtin @inline @nodiscard { - return allocator::realloc(allocator::heap(), ptr, new_size); + return allocator::realloc(mem, ptr, new_size); } fn void* realloc_aligned(void *ptr, usz new_size, usz alignment) @builtin @inline @nodiscard { - return allocator::realloc_aligned(allocator::heap(), ptr, new_size, alignment)!!; + return allocator::realloc_aligned(mem, ptr, new_size, alignment)!!; } fn void free(void* ptr) @builtin @inline { - return allocator::free(allocator::heap(), ptr); + return allocator::free(mem, ptr); } fn void free_aligned(void* ptr) @builtin @inline { - return allocator::free_aligned(allocator::heap(), ptr); + return allocator::free_aligned(mem, ptr); } fn void* trealloc(void* ptr, usz size, usz alignment = mem::DEFAULT_MEM_ALIGNMENT) @builtin @inline @nodiscard { if (!size) return null; if (!ptr) return tmalloc(size, alignment); - return allocator::temp().resize(ptr, size, alignment)!!; + return tmem().resize(ptr, size, alignment)!!; } module std::core::mem @if(env::NO_LIBC); diff --git a/lib/std/core/runtime_test.c3 b/lib/std/core/runtime_test.c3 index 17b26fa46..6c41e1ec4 100644 --- a/lib/std/core/runtime_test.c3 +++ b/lib/std/core/runtime_test.c3 @@ -181,7 +181,7 @@ fn bool run_tests(String[] args, TestUnit[] tests) @private .breakpoint_on_assert = false, .test_filter = "", .has_ansi_codes = terminal_has_ansi_codes(), - .stored.allocator = allocator::heap(), + .stored.allocator = mem, .stored.stderr = *io::stderr(), .stored.stdout = *io::stdout(), }; diff --git a/lib/std/io/file.c3 b/lib/std/io/file.c3 index 8ada06d3d..52b75b645 100644 --- a/lib/std/io/file.c3 +++ b/lib/std/io/file.c3 @@ -183,9 +183,7 @@ fn char[]? load_buffer(String filename, char[] buffer) } -fn char[]? load(Allocator allocator, String filename) => load_new(filename, allocator); - -fn char[]? load_new(String filename, Allocator allocator = allocator::heap()) +fn char[]? load(Allocator allocator, String filename) { File file = open(filename, "rb")!; defer (void)file.close(); @@ -201,12 +199,9 @@ fn char[]? load_new(String filename, Allocator allocator = allocator::heap()) return data[:len]; } -fn char[]? load_path_new(Path path, Allocator allocator = allocator::heap()) => load_new(path.str_view(), allocator); +fn char[]? load_path(Allocator allocator, Path path) => load(allocator, path.str_view()); -fn char[]? load_temp(String filename) -{ - return load_new(filename, allocator::temp()); -} +fn char[]? load_temp(String filename) => load(tmem(), filename); fn char[]? load_path_temp(Path path) => load_temp(path.str_view()); diff --git a/lib/std/io/os/getcwd.c3 b/lib/std/io/os/getcwd.c3 index c4ab658d2..3c04b801d 100644 --- a/lib/std/io/os/getcwd.c3 +++ b/lib/std/io/os/getcwd.c3 @@ -1,7 +1,7 @@ module std::io::os; import libc, std::os; -macro String? getcwd(Allocator allocator = allocator::heap()) +macro String? getcwd(Allocator allocator) { $switch: $case env::WIN32: diff --git a/lib/std/io/os/temp_directory.c3 b/lib/std/io/os/temp_directory.c3 index 9e81e25e9..c8e20f782 100644 --- a/lib/std/io/os/temp_directory.c3 +++ b/lib/std/io/os/temp_directory.c3 @@ -23,7 +23,7 @@ fn Path? native_temp_directory(Allocator allocator) @if(env::WIN32) => @pool() module std::io::os @if(env::NO_LIBC); import std::io::path; -macro Path? native_temp_directory(Allocator allocator = allocator::heap()) +macro Path? native_temp_directory(Allocator allocator) { return io::UNSUPPORTED_OPERATION?; } diff --git a/lib/std/math/random/math.seeder.c3 b/lib/std/math/random/math.seeder.c3 index 7e44dd4c7..6011b6601 100644 --- a/lib/std/math/random/math.seeder.c3 +++ b/lib/std/math/random/math.seeder.c3 @@ -87,7 +87,7 @@ fn char[8 * 4] entropy() @if(!env::WASM_NOLIBC) random_int, hash(clock::now()), hash(&DString.init), - hash(allocator::heap()) + hash(mem) }; return bitcast(entropy_data, char[8 * 4]); } diff --git a/lib/std/os/env.c3 b/lib/std/os/env.c3 index 1304aa09e..4a40d6b49 100644 --- a/lib/std/os/env.c3 +++ b/lib/std/os/env.c3 @@ -66,7 +66,7 @@ fn bool set_var(String name, String value, bool overwrite = true) => @pool() <* Returns the current user's home directory. *> -fn String? get_home_dir(Allocator using = allocator::heap()) +fn String? get_home_dir(Allocator allocator) { String home; $if !env::WIN32: @@ -74,7 +74,7 @@ fn String? get_home_dir(Allocator using = allocator::heap()) $else home = "USERPROFILE"; $endif - return get_var(using, home); + return get_var(allocator, home); } diff --git a/lib/std/os/macos/darwin.c3 b/lib/std/os/macos/darwin.c3 index d666e1418..59e081843 100644 --- a/lib/std/os/macos/darwin.c3 +++ b/lib/std/os/macos/darwin.c3 @@ -93,7 +93,7 @@ fn uptr? load_address() @local } -fn Backtrace? backtrace_load_element(String execpath, void* buffer, void* load_address, Allocator allocator = allocator::heap()) @local +fn Backtrace? backtrace_load_element(Allocator allocator, String execpath, void* buffer, void* load_address) @local { @pool() { @@ -150,7 +150,7 @@ fn BacktraceList? symbolize_backtrace(Allocator allocator, void*[] backtrace) String execpath = executable_path(tmem())!; foreach (addr : backtrace) { - list.push(backtrace_load_element(execpath, addr, load_addr, allocator) ?? backtrace::BACKTRACE_UNKNOWN); + list.push(backtrace_load_element(allocator, execpath, addr, load_addr) ?? backtrace::BACKTRACE_UNKNOWN); } }; return list; diff --git a/lib/std/os/macos/objc.c3 b/lib/std/os/macos/objc.c3 index 2bc96bd62..355b0743a 100644 --- a/lib/std/os/macos/objc.c3 +++ b/lib/std/os/macos/objc.c3 @@ -26,7 +26,7 @@ macro ObjcClass? class_by_name(ZString c) return cls ?: CLASS_NOT_FOUND?; } -macro ObjcClass[] class_get_list(Allocator allocator = allocator::heap()) +macro ObjcClass[] class_get_list(Allocator allocator) { int num_classes = objc::getClassList(null, 0); if (!num_classes) return {}; diff --git a/lib/std/threads/fixed_pool.c3 b/lib/std/threads/fixed_pool.c3 index d75796900..8eada681d 100644 --- a/lib/std/threads/fixed_pool.c3 +++ b/lib/std/threads/fixed_pool.c3 @@ -116,7 +116,7 @@ fn void? FixedThreadPool.push(&self, ThreadPoolFn func, args...) if (args.len) { data = mem::alloc_array(any, args.len); - foreach (i, arg : args) data[i] = allocator::clone_any(allocator::heap(), arg); + foreach (i, arg : args) data[i] = allocator::clone_any(mem, arg); } self.queue[self.qindex] = { .func = func, .args = data }; self.qindex++; diff --git a/releasenotes.md b/releasenotes.md index 080f4ca46..3a4ff6241 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -72,6 +72,9 @@ - `Allocator` interface removes `mark` and `reset`. - DynamicArenaAllocator has changed init function. - Added `BackedArenaAllocator` which is allocated to a fixed size, then allocates on the backing allocator and supports mark/reset. +- `AnyList` now also defaults to the temp allocator. +- `os::getcwd` and `os::get_home_dir` requires an explicit allocator. +- `file::load_new` and `file::load_path_new` removed. ## 0.6.8 Change list diff --git a/resources/testfragments/allocators_testing.c3 b/resources/testfragments/allocators_testing.c3 index 7f55e708f..bb33fd161 100644 --- a/resources/testfragments/allocators_testing.c3 +++ b/resources/testfragments/allocators_testing.c3 @@ -9,7 +9,7 @@ enum Foo fn void print_pages() { - allocator::temp().print_pages(io::stdout())!!; + tmem().print_pages(io::stdout())!!; } fn void setstring(char* dst, String str) @@ -63,7 +63,7 @@ fn void main() io::printf("First big: %p\n", first_big); print_pages(); }; - mem::@scoped(allocator::temp()) + mem::@scoped(tmem()) { io::printf("Malloc: %p\n", (void*)malloc(23)); io::printf("Malloc: %p\n", (void*)malloc(23)); @@ -73,14 +73,14 @@ fn void main() { io::printf("Talloc: %p\n", (void*)tmalloc(22)); }; - testAllocator(allocator::temp(), 126); - testAllocator(allocator::temp(), 12600); + testAllocator(tmem(), 126); + testAllocator(tmem(), 12600); ArenaAllocator aa; aa.init(&&char[1024] {}); testAllocator(&aa, 126); io::printn("Test dynamic arena"); DynamicArenaAllocator dynamic_arena; - dynamic_arena.init(1024, allocator::heap()); + dynamic_arena.init(mem, 1024); testAllocator(&dynamic_arena, 112); testAllocator(&dynamic_arena, 712); first_big[3] = 123;