Skip to content

Commit b69d46a

Browse files
authored
Merge pull request #137 from vansante/revert-buffer
Revert "Avoid allocating a new go byte slice buffer and instead reuse the C buffer"
2 parents fe7ae84 + 82918d4 commit b69d46a

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

vips/foreign.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"image/png"
1111
"math"
12-
"reflect"
1312
"runtime"
1413
"unsafe"
1514

@@ -303,12 +302,8 @@ func vipsSaveJPEGToBuffer(in *C.VipsImage, quality int, stripMetadata, interlace
303302
}
304303

305304
func toBuff(ptr unsafe.Pointer, cLen C.size_t) []byte {
306-
// Create a go slice from the C memory without copying the data
307-
// https://stackoverflow.com/questions/51187973/how-to-create-an-array-or-a-slice-from-an-array-unsafe-pointer-in-golang
308-
sh := &reflect.SliceHeader{
309-
Data: uintptr(ptr),
310-
Len: int(cLen),
311-
Cap: int(cLen),
312-
}
313-
return *(*[]byte)(unsafe.Pointer(sh))
305+
buf := C.GoBytes(ptr, C.int(cLen))
306+
gFreePointer(ptr)
307+
308+
return buf
314309
}

0 commit comments

Comments
 (0)