/
memory.go
74 lines (64 loc) · 1.64 KB
/
memory.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 pkg
/*
#cgo LDFLAGS: -L${SRCDIR}/../../target/release/ -lpravega_client_c -Wl,-rpath,${SRCDIR}/../../target/release/
#include "pravega_client.h"
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
type (
cu8ptr = *C.uint8_t
usize = C.uintptr_t
cusize = C.size_t
cint = C.int
ci32 = C.int32_t
cu32 = C.uint32_t
ci64 = C.int64_t
cu64 = C.uint64_t
cbool = C.bool
)
func copyAndDestroyBuffer(b C.Buffer) []byte {
if emptyBuffer(b) {
return nil
}
res := C.GoBytes(unsafe.Pointer(b.ptr), cint(b.len))
C.free_buffer(b)
return res
}
func emptyBuffer(b C.Buffer) bool {
return b.ptr == cu8ptr(nil) || b.len == usize(0) || b.cap == usize(0)
}
func errorWithMessage(err error, b C.Buffer) error {
msg := copyAndDestroyBuffer(b)
if msg == nil {
return err
}
return fmt.Errorf("%s", string(msg))
}
func freeCString(str *C.char) {
C.free(unsafe.Pointer(str))
}
// https://chai2010.cn/advanced-go-programming-book/ch2-cgo/ch2-07-memory.html
// https://github.com/CosmWasm/wasmvm/blob/main/api/memory.go
// makeView creates a view into the given byte slice which allows Rust code to read it.
// The byte slice is managed by Go and will be garbage collected. Use runtime.KeepAlive
// to ensure the byte slice lives long enough.
func makeViewFromString(s string) C.Buffer {
p := (*reflect.StringHeader)(unsafe.Pointer(&s))
return C.Buffer{
ptr: cu8ptr(unsafe.Pointer(p.Data)),
len: cusize(p.Len),
cap: cusize(p.Len),
}
}
func makeViewFromSlice(s []byte) (C.Buffer, int) {
p := (*reflect.SliceHeader)(unsafe.Pointer(&s))
return C.Buffer{
ptr: cu8ptr(unsafe.Pointer(p.Data)),
len: cusize(p.Len),
cap: cusize(p.Cap),
}, p.Len
}