internal/graphicsdriver/opengl/gl: Bug fix: Remove unsafe reflect usage

Closes #1495
This commit is contained in:
Hajime Hoshi 2021-02-14 21:27:27 +09:00
parent 115c11232e
commit 464d4efcb1

View File

@ -75,26 +75,15 @@ func Strs(strs ...string) (cstrs **uint8, free func()) {
panic("Strs: expected at least 1 string") panic("Strs: expected at least 1 string")
} }
// Allocate a contiguous array large enough to hold all the strings' contents. css := make([]*uint8, 0, len(strs))
n := 0 for _, str := range strs {
for i := range strs { cs := C.CString(str)
n += len(strs[i]) css = append(css, (*uint8)(unsafe.Pointer(cs)))
}
data := C.malloc(C.size_t(n))
// Copy all the strings into data.
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(data),
Len: n,
Cap: n,
}))
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
offset := 0
for i := range strs {
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
offset += len(strs[i])
} }
return (**uint8)(&css[0]), func() { C.free(data) } return (**uint8)(&css[0]), func() {
for _, cs := range css {
C.free(unsafe.Pointer(cs))
}
}
} }