From 66fd6aeb6dd9acbf949e9972c674151ada5fe8ca Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Wed, 31 Jan 2018 18:23:34 +0100 Subject: [PATCH] Test using cgocheck=2 and fix compaction_filter test for that --- .travis.yml | 2 +- compaction_filter_test.go | 5 ++++- util.go | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 982b7715..4bbe75c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ install: - go get -t ./... script: - - go test -v ./ + - GODEBUG=cgocheck=2 go test -v ./ notifications: email: diff --git a/compaction_filter_test.go b/compaction_filter_test.go index 1dfcd63e..2735cf16 100644 --- a/compaction_filter_test.go +++ b/compaction_filter_test.go @@ -11,9 +11,12 @@ func TestCompactionFilter(t *testing.T) { var ( changeKey = []byte("change") changeValOld = []byte("old") - changeValNew = []byte("new") + changeValNew = cBackedBytes([]byte("new")) deleteKey = []byte("delete") ) + + defer freeCBackedBytes(changeValNew) + db := newTestDB(t, "TestCompactionFilter", func(opts *Options) { opts.SetCompactionFilter(&mockCompactionFilter{ filter: func(level int, key, val []byte) (remove bool, newVal []byte) { diff --git a/util.go b/util.go index 236f3fea..3ac17d95 100644 --- a/util.go +++ b/util.go @@ -1,5 +1,6 @@ package gorocksdb +// #include import "C" import ( "reflect" @@ -30,6 +31,19 @@ func charToByte(data *C.char, len C.size_t) []byte { return value } +// cBackedBytes returs a copy of the same byte slice which is backed by +// malloced memory. This should be freed using freeCBackedBytes. +func cBackedBytes(data []byte) []byte { + return charToByte(cByteSlice(data), C.size_t(len(data))) +} + +// freeCBackedBytes frees a byte slice created by cBackedBytes +func freeCBackedBytes(data []byte) { + sH := (*reflect.SliceHeader)(unsafe.Pointer(&data)) + C.free(unsafe.Pointer(sH.Data)) + +} + // byteToChar returns *C.char from byte slice. func byteToChar(b []byte) *C.char { var c *C.char