diff options
author | Ayke van Laethem <aykevanlaethem@gmail.com> | 2017-10-02 20:27:54 +0200 |
---|---|---|
committer | Ayke van Laethem <aykevanlaethem@gmail.com> | 2017-10-02 20:29:19 +0200 |
commit | c00c43c28b3f2e269931aa7a9e6f7e622ac838da (patch) | |
tree | dc3279c3040cfc47712ace9988d6a68887a42906 /librsync/librsync_callback.go | |
parent | a1518c8e91c6923c4c48677fc543447b465dd16a (diff) | |
download | golibrsync-c00c43c28b3f2e269931aa7a9e6f7e622ac838da.tar.gz golibrsync-c00c43c28b3f2e269931aa7a9e6f7e622ac838da.tar.bz2 golibrsync-c00c43c28b3f2e269931aa7a9e6f7e622ac838da.zip |
Do not store Go pointers in C structs
Diffstat (limited to 'librsync/librsync_callback.go')
-rw-r--r-- | librsync/librsync_callback.go | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/librsync/librsync_callback.go b/librsync/librsync_callback.go index ace9779..66b16c7 100644 --- a/librsync/librsync_callback.go +++ b/librsync/librsync_callback.go @@ -3,6 +3,7 @@ package librsync /* #include <stdio.h> #include <librsync.h> +#include <stdlib.h> */ import "C" @@ -12,20 +13,25 @@ import ( ) //export patchCallbackGo -func patchCallbackGo(_patcher uintptr, pos C.rs_long_t, len *C.size_t, _buf *unsafe.Pointer) C.rs_result { +func patchCallbackGo(_patcher uintptr, pos C.rs_long_t, buflen *C.size_t, buf *unsafe.Pointer) C.rs_result { patcher := getPatcher(_patcher) - patcher.buf = make([]byte, int(*len)) - n, err := patcher.basis.ReadAt(patcher.buf, int64(pos)) - if n < int(*len) { + if patcher.buf != nil { + C.free(patcher.buf) + } + patcher.buf = C.malloc(*buflen) + // https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices + s := (*[1 << 30]byte)(patcher.buf)[:*buflen:*buflen] + n, err := patcher.basis.ReadAt(s, int64(pos)) + if n < int(*buflen) { if err != io.EOF { panic(jobInternalPanic{err}) } else { return C.RS_INPUT_ENDED } } - *len = C.size_t(n) - *_buf = unsafe.Pointer(&(patcher.buf[0])) + *buflen = C.size_t(n) + *buf = patcher.buf return C.RS_DONE } |