mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Add page_alloc and page_allocator
This commit is contained in:
Vendored
+46
@@ -0,0 +1,46 @@
|
|||||||
|
//+build js wasm32
|
||||||
|
package wasm_js_interface
|
||||||
|
|
||||||
|
import "core:mem"
|
||||||
|
import "core:intrinsics"
|
||||||
|
|
||||||
|
PAGE_SIZE :: 64 * 1024
|
||||||
|
page_alloc :: proc(page_count: int) -> (data: []byte, err: mem.Allocator_Error) {
|
||||||
|
prev_page_count := intrinsics.wasm_memory_grow(0, uintptr(page_count))
|
||||||
|
if prev_page_count < 0 {
|
||||||
|
return nil, .Out_Of_Memory
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr := ([^]u8)(uintptr(prev_page_count) * PAGE_SIZE)
|
||||||
|
return ptr[:page_count * PAGE_SIZE], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
page_allocator :: proc() -> mem.Allocator {
|
||||||
|
procedure :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
||||||
|
size, alignment: int,
|
||||||
|
old_memory: rawptr, old_size: int,
|
||||||
|
location := #caller_location) -> ([]byte, mem.Allocator_Error) {
|
||||||
|
switch mode {
|
||||||
|
case .Alloc:
|
||||||
|
assert(size % PAGE_SIZE == 0)
|
||||||
|
return page_alloc(size/PAGE_SIZE)
|
||||||
|
case .Resize, .Free, .Free_All:
|
||||||
|
return nil, .Mode_Not_Implemented
|
||||||
|
case .Query_Features:
|
||||||
|
set := (^mem.Allocator_Mode_Set)(old_memory)
|
||||||
|
if set != nil {
|
||||||
|
set^ = {.Alloc}
|
||||||
|
}
|
||||||
|
case .Query_Info:
|
||||||
|
return nil, .Mode_Not_Implemented
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
procedure = procedure,
|
||||||
|
data = nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user