-
Notifications
You must be signed in to change notification settings - Fork 66
/
util.go
84 lines (70 loc) · 1.72 KB
/
util.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package grocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import (
"errors"
"unsafe"
)
// boolToChar converts a bool value to C.uchar.
func boolToChar(b bool) C.uchar {
if b {
return 1
}
return 0
}
// charToBool converts C.uchar to bool value
func charToBool(c C.uchar) bool {
return c != 0
}
// refCBytes referencing *C.char.
func refCBytes(data *C.char, len C.size_t) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(data)), int(len))
}
// refGoBytes returns *C.char from byte slice.
func refGoBytes(b []byte) *C.char {
var c *C.char
if len(b) > 0 {
c = (*C.char)(unsafe.Pointer(&b[0]))
}
return c
}
// Go []byte to C string
// The C string is allocated in the C heap using malloc.
func cByteSlice(b []byte) *C.char {
var c *C.char
if len(b) > 0 {
c = (*C.char)(C.CBytes(b))
}
return c
}
// charSlice converts a C array of *char to a []*C.char.
func charSlice(data **C.char, len C.int) []*C.char {
return unsafe.Slice(data, int(len))
}
// charSliceIntoStringSlice converts a C array of *char to a []string.
func charSliceIntoStringSlice(data **C.char, len C.int) []string {
s := charSlice(data, len)
result := make([]string, int(len))
for i := range s {
result[i] = C.GoString(s[i])
}
return result
}
// sizeSlice converts a C array of size_t to a []C.size_t.
func sizeSlice(data *C.size_t, len C.int) []C.size_t {
return unsafe.Slice(data, int(len))
}
// fromCError returns go error and free c_err if need.
func fromCError(cErr *C.char) (err error) {
if cErr != nil {
err = errors.New(C.GoString(cErr))
C.rocksdb_free(unsafe.Pointer(cErr))
}
return
}
func toString(cVal *C.char, len C.int) string {
s := C.GoStringN(cVal, C.int(len))
C.rocksdb_free(unsafe.Pointer(cVal))
return s
}