ledisdb/store/leveldb/slice.go

44 lines
603 B
Go
Raw Normal View History

2014-10-29 05:29:35 +03:00
// +build leveldb
2014-10-29 05:29:35 +03:00
package leveldb
// #cgo LDFLAGS: -lleveldb
// #include "leveldb/c.h"
import "C"
import (
"reflect"
"unsafe"
)
type CSlice struct {
data unsafe.Pointer
size int
}
func NewCSlice(p unsafe.Pointer, n int) *CSlice {
return &CSlice{p, n}
}
func (s *CSlice) Data() []byte {
var value []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap = int(s.size)
sH.Len = int(s.size)
sH.Data = uintptr(s.data)
return value
}
func (s *CSlice) Size() int {
return int(s.size)
}
func (s *CSlice) Free() {
if s.data != nil {
2014-10-29 05:29:35 +03:00
C.leveldb_free(s.data)
s.data = nil
}
}