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