@@ -100,42 +100,13 @@ _free_virtual_memory :: proc "contextless" (ptr: rawptr, size: int) {
100
100
}
101
101
102
102
_resize_virtual_memory :: proc " contextless" (ptr: rawptr , old_size: int , new_size: int , alignment: int ) -> rawptr {
103
- old_size_pages := old_size / PAGE_SIZE
104
- new_size_pages := new_size / PAGE_SIZE
105
- if old_size % PAGE_SIZE != 0 {
106
- old_size_pages += 1
107
- }
108
- if new_size % PAGE_SIZE != 0 {
109
- new_size_pages += 1
110
- }
111
-
112
- if new_size_pages == old_size_pages {
113
- return ptr
114
- } else if new_size_pages > old_size_pages {
115
- new_address: u64
116
- result_alloc := mach_vm_allocate (mach_task_self_, &new_address, u64 (new_size), VM_FLAGS_ANYWHERE)
117
- if result_alloc != 0 {
118
- return nil
119
- }
120
-
121
- alignment_mask: u64
122
- if alignment != 0 {
123
- alignment_mask = u64 (alignment) - 1
124
- }
125
-
126
- mach_vm_protect (mach_task_self_, new_address, u64 (new_size), true , VM_PROT_READ|VM_PROT_WRITE)
127
- mach_vm_protect (mach_task_self_, new_address, u64 (new_size), false , VM_PROT_READ|VM_PROT_WRITE)
128
- cur_protection, max_protection: i32
129
- result_remap := mach_vm_remap (mach_task_self_, &new_address, u64 (old_size), alignment_mask, VM_FLAGS_ANYWHERE, mach_task_self_, u64 (uintptr (ptr)), true , &cur_protection, &max_protection, VM_INHERIT_COPY)
130
- if result_remap != 0 {
131
- return nil
132
- }
133
- mach_vm_deallocate (mach_task_self_, u64 (uintptr (ptr)), u64 (old_size))
134
- return rawptr (uintptr (new_address))
103
+ result: rawptr = ---
104
+ if alignment == 0 {
105
+ result = _allocate_virtual_memory (new_size)
135
106
} else {
136
- new_size_boundary := new_size_pages * PAGE_SIZE
137
- shrink_by := u64 (old_size - new_size)
138
- mach_vm_deallocate (mach_task_self_, u64 (uintptr (ptr) + uintptr (new_size_boundary)), shrink_by)
139
- return rawptr (uintptr (ptr))
107
+ result = _allocate_virtual_memory_aligned (new_size, alignment)
140
108
}
109
+ intrinsics.mem_copy_non_overlapping (result, ptr, min (new_size, old_size))
110
+ mach_vm_deallocate (mach_task_self_, u64 (uintptr (ptr)), u64 (old_size))
111
+ return result
141
112
}
0 commit comments