forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
54 lines (45 loc) · 1.37 KB
/
array.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gorocksdb
// #include "stdlib.h"
// #include "rocksdb/c.h"
import "C"
import (
"reflect"
"unsafe"
)
type charsSlice []*C.char
type sizeTSlice []C.size_t
type columnFamilySlice []*C.rocksdb_column_family_handle_t
func (s charsSlice) c() **C.char {
sH := (*reflect.SliceHeader)(unsafe.Pointer(&s))
return (**C.char)(unsafe.Pointer(sH.Data))
}
func (s sizeTSlice) c() *C.size_t {
sH := (*reflect.SliceHeader)(unsafe.Pointer(&s))
return (*C.size_t)(unsafe.Pointer(sH.Data))
}
func (s columnFamilySlice) c() **C.rocksdb_column_family_handle_t {
sH := (*reflect.SliceHeader)(unsafe.Pointer(&s))
return (**C.rocksdb_column_family_handle_t)(unsafe.Pointer(sH.Data))
}
// bytesSliceToCSlices converts a slice of byte slices to two slices with C
// datatypes. One containing pointers to copies of the byte slices and one
// containing their sizes.
// IMPORTANT: All the contents of the charsSlice array are malloced and
// should be freed using the Destroy method of charsSlice.
func byteSlicesToCSlices(vals [][]byte) (charsSlice, sizeTSlice) {
if len(vals) == 0 {
return nil, nil
}
chars := make(charsSlice, len(vals))
sizes := make(sizeTSlice, len(vals))
for i, val := range vals {
chars[i] = (*C.char)(C.CBytes(val))
sizes[i] = C.size_t(len(val))
}
return chars, sizes
}
func (s charsSlice) Destroy() {
for _, chars := range s {
C.free(unsafe.Pointer(chars))
}
}