From f0005d4d30923ce6c583c6af2dd990134fb3d826 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Thu, 30 Apr 2020 21:08:15 +0000 Subject: [PATCH] Comment the byte slice conversion --- unsafe.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unsafe.go b/unsafe.go index d6c0c04..3a5fb26 100644 --- a/unsafe.go +++ b/unsafe.go @@ -11,5 +11,15 @@ func unsafeIndex(base unsafe.Pointer, offset uintptr, elemsz uintptr, n int) uns } func unsafeByteSlice(base unsafe.Pointer, offset uintptr, i, j int) []byte { + // See: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices + // + // This memory is not allocated from C, but it is unmanaged by Go's + // garbage collector and should behave similarly, and the compiler + // should produce similar code. Note that this conversion allows a + // subslice to begin after the base address, with an optional offset, + // while the URL above does not cover this case and only slices from + // index 0. However, the wiki never says that the address must be to + // the beginning of a C allocation (or even that malloc was used at + // all), so this is believed to be correct. return (*[maxAllocSize]byte)(unsafeAdd(base, offset))[i:j:j] }