github.com/moontrade/nogc@v0.1.7/alloc/rpmalloc/tinygo/cgo.go (about)

     1  //go:build tinygo
     2  // +build tinygo
     3  
     4  package rpmalloc
     5  
     6  /*
     7  #include "rpmalloc.h"
     8  #include <stdlib.h>
     9  */
    10  import "C"
    11  import (
    12  	"unsafe"
    13  )
    14  
    15  func init() {
    16  	C.rpmalloc_initialize()
    17  }
    18  func Init() {}
    19  
    20  // ReadThreadStats get thread statistics
    21  func ReadThreadStats(stats *ThreadStats) {
    22  	C.rpmalloc_thread_statistics((*C.rpmalloc_thread_statistics_t)(unsafe.Pointer(stats)))
    23  }
    24  
    25  // ReadGlobalStats get global statistics
    26  //go:nosplit
    27  //go:noescape
    28  func ReadGlobalStats(stats *GlobalStats) {
    29  	C.rpmalloc_global_statistics((*C.rpmalloc_global_statistics_t)(unsafe.Pointer(stats)))
    30  }
    31  
    32  // Malloc allocate a memory block of at least the given size
    33  //go:nosplit
    34  //go:noescape
    35  func Malloc(size uintptr) uintptr {
    36  	return uintptr(unsafe.Pointer(C.rpmalloc((C.size_t)(size))))
    37  }
    38  
    39  // MallocCap allocate a memory block of at least the given size
    40  //go:nosplit
    41  //go:noescape
    42  func MallocCap(size uintptr) (uintptr, uintptr) {
    43  	ptr := uintptr(unsafe.Pointer(C.rpmalloc((C.size_t)(size))))
    44  	return ptr, uintptr(C.rpmalloc_usable_size(unsafe.Pointer(ptr)))
    45  }
    46  
    47  // MallocZero allocate a memory block of at least the given size
    48  //go:nosplit
    49  //go:noescape
    50  func MallocZeroed(size uintptr) uintptr {
    51  	return uintptr(unsafe.Pointer(C.rpcalloc((C.size_t)(1), (C.size_t)(size))))
    52  }
    53  
    54  // MallocZeroCap allocate a memory block of at least the given size
    55  //go:nosplit
    56  //go:noescape
    57  func MallocZeroedCap(size uintptr) (uintptr, uintptr) {
    58  	ptr := uintptr(unsafe.Pointer(C.rpcalloc((C.size_t)(1), (C.size_t)(size))))
    59  	return ptr, uintptr(C.rpmalloc_usable_size(unsafe.Pointer(ptr)))
    60  }
    61  
    62  // Calloc Allocates a memory block of at least the given size and zero initialize it
    63  //go:nosplit
    64  //go:noescape
    65  func Calloc(num, size uintptr) uintptr {
    66  	return uintptr(C.rpcalloc((C.size_t)(num), (C.size_t)(size)))
    67  }
    68  
    69  // CallocCap Allocates a memory block of at least the given size and zero initialize it
    70  //go:nosplit
    71  //go:noescape
    72  func CallocCap(num, size uintptr) (uintptr, uintptr) {
    73  	ptr := uintptr(C.rpcalloc((C.size_t)(num), (C.size_t)(size)))
    74  	return ptr, uintptr(C.rpmalloc_usable_size(unsafe.Pointer(ptr)))
    75  }
    76  
    77  // Realloc the given block to at least the given size
    78  //go:nosplit
    79  //go:noescape
    80  func Realloc(ptr, size uintptr) uintptr {
    81  	return uintptr(C.rprealloc(unsafe.Pointer(ptr), (C.size_t)(size)))
    82  }
    83  
    84  // ReallocCap the given block to at least the given size
    85  func ReallocCap(ptr, size uintptr) (uintptr, uintptr) {
    86  	newptr := uintptr(C.rprealloc(unsafe.Pointer(ptr), (C.size_t)(size)))
    87  	return newptr, uintptr(C.rpmalloc_usable_size(unsafe.Pointer(newptr)))
    88  }
    89  
    90  // UsableSize Query the usable size of the given memory block (from given pointer to the end of block)
    91  func UsableSize(ptr uintptr) uintptr {
    92  	return uintptr(C.rpmalloc_usable_size(unsafe.Pointer(ptr)))
    93  }
    94  
    95  // Free the given memory block
    96  func Free(ptr uintptr) {
    97  	C.rpfree(unsafe.Pointer(ptr))
    98  }