Skip to content

Commit

Permalink
Merge pull request #137 from vansante/revert-buffer
Browse files Browse the repository at this point in the history
Revert "Avoid allocating a new go byte slice buffer and instead reuse the C buffer"
  • Loading branch information
tonimelisma authored Dec 13, 2020
2 parents fe7ae84 + 82918d4 commit b69d46a
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions vips/foreign.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"image/png"
"math"
"reflect"
"runtime"
"unsafe"

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

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

return buf
}

0 comments on commit b69d46a

Please sign in to comment.