diff --git a/lib/std/core/allocators/temp_allocator.c3 b/lib/std/core/allocators/temp_allocator.c3 index 8f60cf4ea..a7216a97b 100644 --- a/lib/std/core/allocators/temp_allocator.c3 +++ b/lib/std/core/allocators/temp_allocator.c3 @@ -45,6 +45,13 @@ fn TempAllocator*! new_temp_allocator(usz size, Allocator allocator) return temp; } +fn void TempAllocator.destroy(&self) +{ + self.reset(0); + if (self.last_page) (void)self._free_page(self.last_page); + allocator::free(self.backing_allocator, self); +} + fn usz TempAllocator.mark(&self) @dynamic => self.used; fn void TempAllocator.release(&self, void* old_pointer, bool) @dynamic diff --git a/lib/std/core/mem_allocator.c3 b/lib/std/core/mem_allocator.c3 index ec0499186..32504e906 100644 --- a/lib/std/core/mem_allocator.c3 +++ b/lib/std/core/mem_allocator.c3 @@ -395,6 +395,23 @@ fn void init_default_temp_allocators() @private thread_temp_allocator = temp_allocator_pair[0]; } +fn void destroy_temp_allocators_after_exit() @finalizer(65535) @local +{ + destroy_temp_allocators(); +} + +/** + * Call this to destroy any memory used by the temp allocators. This will invalidate all temp memory. + **/ +fn void destroy_temp_allocators() +{ + if (!thread_temp_allocator) return; + temp_allocator_pair[0].destroy(); + temp_allocator_pair[1].destroy(); + temp_allocator_pair[..] = null; + thread_temp_allocator = null; +} + fn TempAllocator* temp_allocator_next() @private { if (!thread_temp_allocator) diff --git a/releasenotes.md b/releasenotes.md index 6b1688519..d412ce24d 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -54,6 +54,7 @@ ### Stdlib changes - `send` and `recv` added to `libc` for Posix / Win32. +- Add support to destroy temp allocators. ## 0.6.1 Change list