- 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.
This commit is contained in:
Christoffer Lerno
2025-03-18 18:34:52 +01:00
parent cfc87a9d66
commit 7e100472e7
15 changed files with 45 additions and 44 deletions

View File

@@ -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]);

View File

@@ -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;

View File

@@ -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};

View File

@@ -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);

View File

@@ -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(),
};

View File

@@ -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());

View File

@@ -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:

View File

@@ -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?;
}

View File

@@ -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]);
}

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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 {};

View File

@@ -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++;