forked from cosmos/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
74 lines (64 loc) · 1.65 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
package gorocksdb
// #include <stdlib.h>
import "C"
import (
"reflect"
"unsafe"
)
// btoi converts a bool value to int.
func btoi(b bool) int {
if b {
return 1
}
return 0
}
// boolToChar converts a bool value to C.uchar.
func boolToChar(b bool) C.uchar {
if b {
return 1
}
return 0
}
// charToByte converts a *C.char to a byte slice.
func charToByte(data *C.char, len C.size_t) []byte {
var value []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
}
// byteToChar returns *C.char from byte slice.
func byteToChar(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
}
// stringToChar returns *C.char from string.
func stringToChar(s string) *C.char {
ptrStr := (*reflect.StringHeader)(unsafe.Pointer(&s))
return (*C.char)(unsafe.Pointer(ptrStr.Data))
}
// charSlice converts a C array of *char to a []*C.char.
func charSlice(data **C.char, len C.int) []*C.char {
var value []*C.char
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
}
// 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 {
var value []C.size_t
sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
return value
}