github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/qdb/os_membinds/membind_linux.go (about)

     1  // +build linux
     2  
     3  /*
     4  If this file does not build and you don't know what to do, simply delete it and rebuild.
     5  */
     6  
     7  package qdb
     8  
     9  /*
    10  #include <stdlib.h>
    11  #include <string.h>
    12  
    13  static void *alloc_ptr(void *c, unsigned long l) {
    14  	void *ptr = malloc(l);
    15  	memcpy(ptr, c, l);
    16  	return ptr;
    17  }
    18  
    19  static void *my_alloc(unsigned long l) {
    20  	return malloc(l);
    21  }
    22  
    23  */
    24  import "C"
    25  
    26  import (
    27  	"unsafe"
    28  )
    29  
    30  
    31  func gcc_HeapAlloc(le uint32) data_ptr_t {
    32  	return data_ptr_t(C.my_alloc(C.ulong(le)))
    33  }
    34  
    35  func gcc_HeapFree(ptr data_ptr_t) {
    36  	C.free(unsafe.Pointer(ptr))
    37  }
    38  
    39  func gcc_AllocPtr(v []byte) data_ptr_t {
    40  	ptr := unsafe.Pointer(&v[0]) // see https://github.com/golang/go/issues/15172
    41  	return data_ptr_t(C.alloc_ptr(ptr, C.ulong(len(v))))
    42  }
    43  
    44  func init() {
    45  	if membind_use_wrapper {
    46  		panic("Another wrapper already initialized")
    47  	}
    48  	println("Using malloc() qdb memory bindings")
    49  	_heap_alloc = gcc_HeapAlloc
    50  	_heap_free = gcc_HeapFree
    51  	_heap_store = gcc_AllocPtr
    52  	membind_use_wrapper = true
    53  }