github.com/lemon-mint/libuseful@v1.3.1-0.20220724073654-ee73785d5aa0/sys.go (about)

     1  package libuseful
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // SysMemStat represents a global system statistic that is managed atomically.
     8  //
     9  // This type must structurally be a uint64 so that mstats aligns with MemStats.
    10  type SysMemStat uint64
    11  
    12  // SysAlloc allocates unmanaged memory
    13  func SysAlloc(n uintptr, sysStat *SysMemStat) unsafe.Pointer
    14  
    15  // SysFree frees memory allocated by SysAlloc
    16  func SysFree(v unsafe.Pointer, n uintptr, sysStat *SysMemStat)
    17  
    18  //go:linkname SysAlloc runtime.sysAlloc
    19  //go:linkname SysFree runtime.sysFree
    20  
    21  var libUsefulMemStat SysMemStat
    22  
    23  // Alloc allocates unmanaged memory (Calls SysAlloc)
    24  func Alloc(size uintptr) unsafe.Pointer {
    25  	return SysAlloc(size, &libUsefulMemStat)
    26  }
    27  
    28  // Free frees memory allocated by Alloc
    29  func Free(ptr unsafe.Pointer, size uintptr) {
    30  	SysFree(ptr, size, &libUsefulMemStat)
    31  }