github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/qdb/os_membinds/membind_windows.go (about) 1 // +build windows 2 3 package qdb 4 5 import ( 6 "unsafe" 7 "reflect" 8 "syscall" 9 ) 10 11 var ( 12 funcGlobalAlloc *syscall.Proc 13 funcGlobalFree *syscall.Proc 14 ) 15 16 func win_HeapAlloc(le uint32) data_ptr_t { 17 ptr, _, _ := funcGlobalAlloc.Call(0, uintptr(le)) 18 return data_ptr_t(ptr) 19 } 20 21 func win_HeapFree(ptr data_ptr_t) { 22 funcGlobalFree.Call(uintptr(ptr)) 23 } 24 25 func win_AllocPtr(v []byte) data_ptr_t { 26 ptr := win_HeapAlloc(uint32(len(v))) 27 sl := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data:uintptr(ptr), Len:int(len(v)), Cap:int(len(v))})) 28 copy(sl, v) 29 return ptr 30 } 31 32 33 func init() { 34 if membind_use_wrapper { 35 return 36 } 37 dll, er := syscall.LoadDLL("kernel32.dll") 38 if er!=nil { 39 return 40 } 41 funcGlobalAlloc, _ = dll.FindProc("GlobalAlloc") 42 funcGlobalFree, _ = dll.FindProc("GlobalFree") 43 if funcGlobalAlloc==nil || funcGlobalFree==nil { 44 return 45 } 46 println("Using kernel32.dll for qdb memory bindings") 47 _heap_alloc = win_HeapAlloc 48 _heap_free = win_HeapFree 49 _heap_store = win_AllocPtr 50 membind_use_wrapper = true 51 }