github.com/moontrade/nogc@v0.1.7/alloc_std.go (about)

     1  //go:build !tinygo && !wasm && !wasi && !tinygo.wasm
     2  // +build !tinygo,!wasm,!wasi,!tinygo.wasm
     3  
     4  package nogc
     5  
     6  import (
     7  	"github.com/moontrade/nogc/alloc/rpmalloc"
     8  )
     9  
    10  func Init() {}
    11  
    12  func Scope(fn func(a AutoFree)) {
    13  	scope(fn)
    14  }
    15  
    16  func scope(fn func(a AutoFree)) {
    17  	a := NewAuto(32)
    18  	defer a.Free()
    19  	fn(a)
    20  	a.Print()
    21  }
    22  
    23  // Alloc calls Alloc on the system allocator
    24  //export alloc
    25  func Alloc(size uintptr) Pointer {
    26  	return Pointer(rpmalloc.Malloc(size))
    27  }
    28  
    29  //export allocCap
    30  func AllocCap(size uintptr) (Pointer, uintptr) {
    31  	p, c := rpmalloc.MallocCap(size)
    32  	return Pointer(p), c
    33  }
    34  
    35  //export allocZeroed
    36  func AllocZeroed(size uintptr) Pointer {
    37  	return Pointer(rpmalloc.MallocZeroed(size))
    38  }
    39  
    40  //export allocZeroedCap
    41  func AllocZeroedCap(size uintptr) (Pointer, uintptr) {
    42  	p, c := rpmalloc.MallocZeroedCap(size)
    43  	return Pointer(p), c
    44  }
    45  
    46  // Alloc calls Alloc on the system allocator
    47  //export calloc
    48  func Calloc(num, size uintptr) Pointer {
    49  	return Pointer(rpmalloc.Calloc(num, size))
    50  }
    51  
    52  //export callocCap
    53  func CallocCap(num, size uintptr) (Pointer, uintptr) {
    54  	p, c := rpmalloc.CallocCap(num, size)
    55  	return Pointer(p), c
    56  }
    57  
    58  // Realloc calls Realloc on the system allocator
    59  //export realloc
    60  func Realloc(p Pointer, size uintptr) Pointer {
    61  	return Pointer(rpmalloc.Realloc(uintptr(p), size))
    62  }
    63  
    64  //export reallocCap
    65  func ReallocCap(p Pointer, size uintptr) (Pointer, uintptr) {
    66  	newPtr, c := rpmalloc.ReallocCap(uintptr(p), size)
    67  	return Pointer(newPtr), c
    68  }
    69  
    70  // Free calls Free on the system allocator
    71  //export free
    72  func Free(p Pointer) {
    73  	rpmalloc.Free(uintptr(p))
    74  }
    75  
    76  //export sizeof
    77  func Sizeof(ptr Pointer) uintptr {
    78  	return rpmalloc.UsableSize(uintptr(ptr))
    79  }