github.com/moontrade/unsafe@v0.9.1/memory/alloc_std.go (about) 1 //go:build !tinygo && !wasm && !wasi && !tinygo.wasm && cgo 2 3 package memory 4 5 import ( 6 "github.com/moontrade/unsafe/memory/rpmalloc" 7 ) 8 9 func Init() {} 10 11 // Alloc calls Alloc on the system allocator 12 // 13 //export alloc 14 func Alloc(size uintptr) Pointer { 15 return Pointer(rpmalloc.Malloc(size)) 16 } 17 18 //export allocCap 19 func AllocCap(size uintptr) (Pointer, uintptr) { 20 p, c := rpmalloc.MallocCap(size) 21 return Pointer(p), c 22 } 23 24 func AllocZeroed(size uintptr) Pointer { 25 return Pointer(rpmalloc.MallocZeroed(size)) 26 } 27 28 func AllocZeroedCap(size uintptr) (Pointer, uintptr) { 29 p, c := rpmalloc.MallocZeroedCap(size) 30 return Pointer(p), c 31 } 32 33 // Alloc calls Alloc on the system allocator 34 func Calloc(num, size uintptr) Pointer { 35 return Pointer(rpmalloc.Calloc(num, size)) 36 } 37 38 func CallocCap(num, size uintptr) (Pointer, uintptr) { 39 p, c := rpmalloc.CallocCap(num, size) 40 return Pointer(p), c 41 } 42 43 // Realloc calls Realloc on the system allocator 44 func Realloc(p Pointer, size uintptr) Pointer { 45 return Pointer(rpmalloc.Realloc(uintptr(p), size)) 46 } 47 48 //export reallocCap 49 func ReallocCap(p Pointer, size uintptr) (Pointer, uintptr) { 50 newPtr, c := rpmalloc.ReallocCap(uintptr(p), size) 51 return Pointer(newPtr), c 52 } 53 54 // Free calls Free on the system allocator 55 func Free(p Pointer) { 56 rpmalloc.Free(uintptr(p)) 57 } 58 59 func Sizeof(ptr Pointer) uintptr { 60 return rpmalloc.UsableSize(uintptr(ptr)) 61 }