github.com/moontrade/nogc@v0.1.7/alloc_tinygounix.go (about) 1 //go:build tinygo && !tiny.wasm && (darwin || (linux && !baremetal && !wasi) || (freebsd && !baremetal)) && !nintendoswitch 2 // +build tinygo 3 // +build !tiny.wasm 4 // +build darwin linux,!baremetal,!wasi freebsd,!baremetal 5 // +build !nintendoswitch 6 7 package nogc 8 9 import ( 10 "github.com/moontrade/nogc/alloc/rpmalloc" 11 ) 12 13 //////////////////////////////////////////////////////////////////////////////////// 14 // Global allocator convenience 15 //////////////////////////////////////////////////////////////////////////////////// 16 17 func Init() {} 18 19 func Scope(fn func(a AutoFree)) { 20 scope(fn) 21 } 22 23 func scope(fn func(a AutoFree)) { 24 a := NewAuto(32) 25 defer a.Free() 26 fn(a) 27 a.Print() 28 } 29 30 // Alloc calls Alloc on the system allocator 31 func Alloc(size uintptr) Pointer { 32 return Pointer(rpmalloc.Malloc(size)) 33 } 34 35 func AllocCap(size uintptr) (Pointer, uintptr) { 36 p, c := rpmalloc.MallocCap(size) 37 return Pointer(p), c 38 } 39 40 func AllocZeroed(size uintptr) Pointer { 41 return Pointer(rpmalloc.MallocZeroed(size)) 42 } 43 44 func AllocZeroedCap(size uintptr) (Pointer, uintptr) { 45 p, c := rpmalloc.MallocZeroedCap(size) 46 return Pointer(p), c 47 } 48 49 // Alloc calls Alloc on the system allocator 50 func Calloc(num, size uintptr) Pointer { 51 return Pointer(rpmalloc.Calloc(num, size)) 52 } 53 54 func CallocCap(num, size uintptr) (Pointer, uintptr) { 55 p, c := rpmalloc.CallocCap(num, size) 56 return Pointer(p), c 57 } 58 59 // Realloc calls Realloc on the system allocator 60 func Realloc(p Pointer, size uintptr) Pointer { 61 return Pointer(rpmalloc.Realloc(uintptr(p), size)) 62 } 63 64 func ReallocCap(p Pointer, size uintptr) (Pointer, uintptr) { 65 newPtr, c := rpmalloc.ReallocCap(uintptr(p), size) 66 return Pointer(newPtr), c 67 } 68 69 // Free calls Free on the system allocator 70 func Free(p Pointer) { 71 rpmalloc.Free(uintptr(p)) 72 } 73 74 func Sizeof(ptr Pointer) uintptr { 75 return rpmalloc.UsableSize(uintptr(ptr)) 76 } 77 78 //// Scope creates an AutoFree free list that automatically reclaims memory 79 //// after callback finishes. 80 //func (a *Heap) Scope(fn func(a AutoFree)) { 81 // auto := NewAuto(a.AsAllocator(), 32) 82 // fn(auto) 83 // auto.Free() 84 //} 85 86 func initAllocator(heapStart, heapEnd uintptr) {}