So I found out how to create a slice from an unsafe pointer in Go:
func cArray2ByteSlice(array unsafe.Pointer, len int) (list []byte) {
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&list)))
sliceHeader.Cap = len
sliceHeader.Len = len
sliceHeader.Data = uintptr(array)
return list
}
I have a window of opportunity to access the heap memory at array unsafe.Pointer
between calling the external C API lock
and unlock
.
The GC will eventually attempt to collect the slice list []byte
.
The thing is: the slice header is in golang allocated heap, but the sliceHeader.Data
is not.
Is the GC smart enough to ignore none-golang heap allocation in that situation, as in most others?
After calling unlock
, I want to be sure that Go GC doesn't attempt to release the heap memeory at sliceHeader.Data
.
Also if I want to copy the data for use outside of the lock
/unlock
period, I assume I could return this var instead:
var safeList []byte = list[:]