github.com/moontrade/unsafe@v0.9.1/memory/arrow_offheap.go (about)

     1  package memory
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  var ArrowAllocator arrowAllocator
     9  
    10  type arrowAllocator struct{}
    11  
    12  func (arrowAllocator) Allocate(size int) []byte {
    13  	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    14  		Data: uintptr(Alloc(uintptr(size))),
    15  		Len:  size,
    16  		Cap:  size,
    17  	}))
    18  }
    19  
    20  func (arrowAllocator) Reallocate(size int, b []byte) []byte {
    21  	if len(b) < 1 {
    22  		if size < 1 {
    23  			return nil
    24  		}
    25  		return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    26  			Data: uintptr(Alloc(uintptr(size))),
    27  			Len:  size,
    28  			Cap:  size,
    29  		}))
    30  	}
    31  	newAlloc := Realloc(Pointer(unsafe.Pointer(&b[0])), uintptr(size))
    32  	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    33  		Data: uintptr(newAlloc),
    34  		Len:  size,
    35  		Cap:  size,
    36  	}))
    37  }
    38  
    39  func (arrowAllocator) Free(b []byte) {
    40  	if cap(b) == 0 {
    41  		return
    42  	}
    43  	Free(Pointer(unsafe.Pointer(&b[0])))
    44  }