-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
104 lines (91 loc) · 2.84 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"reflect"
"unsafe"
)
// FreeCppStr release the memory of a cpp string object
func FreeCppStr(str unsafe.Pointer) {
C.nemo_delCppStr(str)
}
// FreeCppSSVector release the memory of a cpp ss vector object
func FreeCppSSVector(vp unsafe.Pointer) {
C.nemo_delSSVector(vp)
}
func goByte2char(b []byte) *C.char {
if len(b) > 0 {
return (*C.char)(unsafe.Pointer(&b[0]))
}
return nil
}
func goBytedup2char(b []byte) *C.char {
var c *C.char
if len(b) > 0 {
cData := C.malloc(C.size_t(len(b)))
copy((*[1 << 30]byte)(cData)[0:len(b)], b)
c = (*C.char)(cData)
}
return c
}
func cstr2GoMultiByte(n int, cdatalist **C.char, clenlist *C.size_t) [][]byte {
mb := make([][]byte, n)
datalist := cStr2Slice(cdatalist, n)
lenlist := sizet2Slice(clenlist, n)
for i := range mb {
mb[i] = C.GoBytes(unsafe.Pointer(datalist[i]), C.int(lenlist[i]))
C.free(unsafe.Pointer(datalist[i]))
}
//fix origin memory leak
C.free(unsafe.Pointer(cdatalist))
C.free(unsafe.Pointer(clenlist))
return mb
// if there is no slice, we can must use very complicate type cast int go to manipulate c array such as below:
// (* C.size_t)(unsafe.Pointer((uintptr(unsafe.Pointer(lenlist)) + i * unsafe.Sizeof(*lenlist))))
}
func sizet2Slice(data *C.size_t, length int) []C.size_t {
var res []C.size_t
sH := (*reflect.SliceHeader)(unsafe.Pointer(&res))
sH.Cap, sH.Len, sH.Data = length, length, uintptr(unsafe.Pointer(data))
return res
}
func cStr2Slice(data **C.char, length int) []*C.char {
var res []*C.char
sH := (*reflect.SliceHeader)(unsafe.Pointer(&res))
sH.Cap, sH.Len, sH.Data = length, length, uintptr(unsafe.Pointer(data))
return res
}
func cArray2goMSliceObj(n int, datalist []*C.char, lenlist []C.size_t) [][]byte {
mb := make([][]byte, n)
for i := range mb {
mb[i] = C.GoBytes(unsafe.Pointer(datalist[i]), C.int(lenlist[i]))
}
return mb
}
func cArray2goMSlice(n int, datalist []*C.char, lenlist []C.size_t) [][]byte {
mb := make([][]byte, n)
for i := range mb {
sH := (*reflect.SliceHeader)(unsafe.Pointer(&mb[i]))
sH.Cap, sH.Len, sH.Data = int(lenlist[i]), int(lenlist[i]), uintptr(unsafe.Pointer(datalist[i]))
}
return mb
}
func cInt64s2Slice(data *C.int64_t, length int) []C.int64_t {
var res []C.int64_t
sH := (*reflect.SliceHeader)(unsafe.Pointer(&res))
sH.Cap, sH.Len, sH.Data = length, length, uintptr(unsafe.Pointer(data))
return res
}
func cDoubles2Slice(data *C.double, length int) []C.double {
var res []C.double
sH := (*reflect.SliceHeader)(unsafe.Pointer(&res))
sH.Cap, sH.Len, sH.Data = length, length, uintptr(unsafe.Pointer(data))
return res
}
func cUll2Slice(data *C.ulonglong, length int) []C.ulonglong {
var res []C.ulonglong
sH := (*reflect.SliceHeader)(unsafe.Pointer(&res))
sH.Cap, sH.Len, sH.Data = length, length, uintptr(unsafe.Pointer(data))
return res
}