35 lines
915 B
Go
35 lines
915 B
Go
package mem
|
|
|
|
// #include <stdlib.h>
|
|
import "C"
|
|
|
|
import "unsafe"
|
|
|
|
func DataToManagedPtr(data []byte) (ptr uint32, size uint32) {
|
|
uptr := unsafe.Pointer(unsafe.SliceData(data))
|
|
return uint32(uintptr(uptr)), uint32(len(data))
|
|
}
|
|
|
|
func DataToUnmanagedPtr(data []byte) (uint32, uint32) {
|
|
size := C.ulong(len(data))
|
|
ptr := unsafe.Pointer(C.malloc(size))
|
|
copy(unsafe.Slice((*byte)(ptr), len(data)), data)
|
|
return uint32(uintptr(ptr)), uint32(size)
|
|
}
|
|
|
|
func DataFromPtr(ptr, size uint32) []byte {
|
|
p := unsafe.Pointer(uintptr(ptr))
|
|
return unsafe.Slice((*byte)(p), int(size))
|
|
}
|
|
|
|
func PtrToData(ptr uint32, size uint32) []byte {
|
|
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(ptr))), size)
|
|
}
|
|
|
|
func UnifyPtrSize(ptr, size uint32) uint64 {
|
|
return (uint64(ptr) << uint64(32)) | uint64(size)
|
|
}
|
|
|
|
func UnifiedPtrToSizePtr(uint64ptr uint64) (ptr uint32, size uint32) {
|
|
return uint32(uint64ptr >> 32), uint32(uint64ptr)
|
|
}
|