Skip to content

Commit

Permalink
replace direct use of reflect.SliceHeader with cast; avoid zero-byte …
Browse files Browse the repository at this point in the history
…allocation (#117)
  • Loading branch information
dertseha authored Apr 28, 2021
1 parent 1c8b6c6 commit 640349a
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions tmpl/conversions.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,13 @@ func Strs(strs ...string) (cstrs **uint8, free func()) {
for i := range strs {
n += len(strs[i])
}
if n == 0 {
n = 1 // avoid allocating zero bytes in case all strings are empty.
}
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,
}))
dataSlice := (*[1 << 30]byte)(data)[:n]
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
offset := 0
for i := range strs {
Expand Down

0 comments on commit 640349a

Please sign in to comment.