From f082cac762939d9b43f7f7301f3733583ee8f1aa Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 14 Jul 2025 03:44:52 +0200 Subject: [PATCH] Updates to API --- lib/std/core/allocators/vmem.c3 | 6 +++--- lib/std/core/os/virtual_mem.c3 | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/std/core/allocators/vmem.c3 b/lib/std/core/allocators/vmem.c3 index b56e1e3b5..f0e507be8 100644 --- a/lib/std/core/allocators/vmem.c3 +++ b/lib/std/core/allocators/vmem.c3 @@ -206,7 +206,7 @@ macro void? protect(Vmem* mem, usz after) @local mem.memory.commit(page_start, page_len)!; if (mem.options.protect_unused_pages || over_high_water) { - mem.memory.set_access(page_start, page_len, READWRITE)!; + mem.memory.protect(page_start, page_len, READWRITE)!; } mem.last_page = page_after; } @@ -234,8 +234,8 @@ macro void unprotect(Vmem* mem, usz after) @local { usz start = page_after << shift; usz len = (last_page - page_after) << shift; - if (mem.options.shrink_on_reset) (void)mem.memory.release(start, len); - if (mem.options.protect_unused_pages) (void)mem.memory.set_access(start, len, PROTECTED); + if (mem.options.protect_unused_pages) (void)mem.memory.protect(start, len, PROTECTED); + if (mem.options.shrink_on_reset) (void)mem.memory.decommit(start, len); } mem.allocated = after; } diff --git a/lib/std/core/os/virtual_mem.c3 b/lib/std/core/os/virtual_mem.c3 index f6c1257e7..c09f4f8d4 100644 --- a/lib/std/core/os/virtual_mem.c3 +++ b/lib/std/core/os/virtual_mem.c3 @@ -39,7 +39,19 @@ fn VirtualMemory? virtual_alloc(usz size, VirtualMemoryAccess access) } } -macro void? VirtualMemory.commit(self, usz offset, usz len) {} +<* + @param offset : "Starting from what offset to commit" + @param len : "To what len to commit" + @require offset < self.size : "Offset out of range" + @require offset + len < self.size : "Length out of range" + @require offset == 0 || math::is_power_of_2(offset) : "Offset should be a power of 2" + @require math::is_power_of_2(len) : "Length should be a multiple of the page size" + @return? VMEM_UPDATE_FAILED, VMEM_ACCESS_NOT_ALLOWED, VMEM_PAGE_NOT_ALIGNED, VMEM_OVERFLOW, VMEM_UNKNOWN_ERROR +*> +macro void? VirtualMemory.commit(self, usz offset, usz len) +{ + return self.protect(offset, len, READWRITE); +} <* @param offset : "Starting from what offset to update" @@ -51,7 +63,7 @@ macro void? VirtualMemory.commit(self, usz offset, usz len) {} @require math::is_power_of_2(len) : "Length should be a multiple of the page size" @return? VMEM_ACCESS_NOT_ALLOWED, VMEM_PAGE_NOT_ALIGNED, VMEM_OVERFLOW, VMEM_UNKNOWN_ERROR *> -fn void? VirtualMemory.set_access(self, usz offset, usz len, VirtualMemoryAccess access) +fn void? VirtualMemory.protect(self, usz offset, usz len, VirtualMemoryAccess access) { if (posix::mprotect(self.ptr + offset, len, access.posix_val)) { @@ -75,7 +87,7 @@ fn void? VirtualMemory.set_access(self, usz offset, usz len, VirtualMemoryAccess @require math::is_power_of_2(len) : "Length should be a multiple of the page size" @return? VMEM_UPDATE_FAILED *> -fn void? VirtualMemory.release(self, usz offset, usz len) +fn void? VirtualMemory.decommit(self, usz offset, usz len) { if (posix::madvise(self.ptr + offset, len, posix::MADV_DONTNEED)) return VMEM_UPDATE_FAILED?; } @@ -142,7 +154,7 @@ macro void? VirtualMemory.commit(self, usz offset, usz len) @require math::is_power_of_2(len) : "Length should be a multiple of the page size" @return? VMEM_UPDATE_FAILED *> -macro void? VirtualMemory.set_access(self, usz offset, usz len, VirtualMemoryAccess access) +macro void? VirtualMemory.protect(self, usz offset, usz len, VirtualMemoryAccess access) { Win32_Protect old; if (!win32::virtualProtect(self.ptr + offset, len, access.win32_val, &old)) @@ -152,15 +164,15 @@ macro void? VirtualMemory.set_access(self, usz offset, usz len, VirtualMemoryAcc } <* - @param offset : "Starting from what offset to release" - @param len : "To what len to release" + @param offset : "Starting from what offset to decommit" + @param len : "To what len to decommit" @require offset < self.size : "Offset out of range" @require offset + len < self.size : "Length out of range" @require offset == 0 || math::is_power_of_2(offset) : "Offset should be a power of 2" @require math::is_power_of_2(len) : "Length should be a multiple of the page size" @return? VMEM_UPDATE_FAILED *> -fn void? VirtualMemory.release(self, usz offset, usz len) +fn void? VirtualMemory.decommit(self, usz offset, usz len) { if (!win32::virtualFree(self.ptr + offset, len, MEM_DECOMMIT)) return VMEM_UPDATE_FAILED?; }