forked from pradeep-pyro/triangle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.go
71 lines (62 loc) · 1.91 KB
/
windows.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
//go:build windows
package triangle
import "unsafe"
import "C"
// The allocations here are using C.malloc and C.free over trimalloc and
// trifree, as C.malloc works the same but gives a better error report in cases
// if we go out of memory.
func trimalloc(size C.ulonglong) unsafe.Pointer {
return C.malloc(size)
}
func intSliceToCArr(slice []int32) (*C.int, C.int) {
sz := len(slice)
ptr := (*C.int)(trimalloc(C.sizeof_int * (C.ulonglong)(sz)))
cArr := (*[1 << 30]C.int)(unsafe.Pointer(ptr))[:sz:sz]
for i := range slice {
cArr[i] = C.int(slice[i])
}
return ptr, C.int(len(slice))
}
func intSlice2DToCArr(slice [][2]int32) (*C.int, C.int) {
sz := 2 * len(slice)
ptr := (*C.int)(trimalloc(C.sizeof_int * (C.ulonglong)(sz)))
cArr := (*[1 << 30]C.int)(unsafe.Pointer(ptr))[:sz:sz]
for i := range slice {
j := 2 * i
cArr[j] = C.int(slice[i][0])
cArr[j+1] = C.int(slice[i][1])
}
return ptr, C.int(len(slice))
}
func intSlice3DToCArr(slice [][3]int32) (*C.int, C.int) {
sz := 3 * len(slice)
ptr := (*C.int)(trimalloc(C.sizeof_int * (C.ulonglong)(sz)))
cArr := (*[1 << 30]C.int)(unsafe.Pointer(ptr))[:sz:sz]
for i := range slice {
j := 3 * i
cArr[j] = C.int(slice[i][0])
cArr[j+1] = C.int(slice[i][1])
cArr[j+2] = C.int(slice[i][2])
}
return ptr, C.int(len(slice))
}
func flt64SliceToCArr(slice []float64) (*C.double, C.int) {
sz := len(slice)
ptr := (*C.double)(trimalloc(C.sizeof_double * (C.ulonglong)(sz)))
cArr := (*[1 << 30]C.double)(unsafe.Pointer(ptr))[:sz:sz]
for i := range slice {
cArr[i] = C.double(slice[i])
}
return ptr, C.int(len(slice))
}
func flt64Slice2DToCArr(slice [][2]float64) (*C.double, C.int) {
sz := 2 * len(slice)
ptr := (*C.double)(trimalloc(C.sizeof_double * (C.ulonglong)(sz)))
cArr := (*[1 << 30]C.double)(unsafe.Pointer(ptr))[:sz:sz]
for i := range slice {
j := i * 2
cArr[j] = C.double(slice[i][0])
cArr[j+1] = C.double(slice[i][1])
}
return ptr, C.int(len(slice))
}