forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice.go
40 lines (33 loc) · 768 Bytes
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gorocksdb
// #include <stdlib.h>
import "C"
import "unsafe"
// Slice is used as a wrapper for non-copy values
type Slice struct {
data *C.char
size C.size_t
freed bool
}
// NewSlice returns a slice with the given data.
func NewSlice(data *C.char, size C.size_t) *Slice {
return &Slice{data, size, false}
}
// Data returns the data of the slice.
func (s *Slice) Data() []byte {
return charToByte(s.data, s.size)
}
// the data will be copied
func (s *Slice) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(s.data), C.int(s.size))
}
// Size returns the size of the data.
func (s *Slice) Size() int {
return int(s.size)
}
// Free frees the slice data.
func (s *Slice) Free() {
if !s.freed {
C.free(unsafe.Pointer(s.data))
s.freed = true
}
}