-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemsafe_test.go
115 lines (101 loc) · 1.84 KB
/
memsafe_test.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
105
106
107
108
109
110
111
112
113
114
115
package lion
import (
"testing"
"unsafe"
)
type MemSafeGet struct {
Obj [1280]int64
}
var (
msgobjf *Field
)
func init() {
mptr := Ptr[MemSafeGet]()
msgobjf = FieldOf[MemSafeGet](&mptr.Obj)
}
// this is ok
func getobjfptr1(idx int64) *[1280]int64 {
var obj = new(MemSafeGet)
obj.Obj[8] = idx
return (msgobjf.PtrOf(unsafe.Pointer(obj)).(*[1280]int64))
}
// this is not ok
func getobjfptr2(idx int64) *[1280]int64 {
var obj = new(MemSafeGet)
obj.Obj[8] = idx
ptrnum := uintptr(unsafe.Pointer(obj))
return (msgobjf.PtrOf(unsafe.Pointer(ptrnum)).(*[1280]int64))
}
func TestMemSafeForGetObjFptr1(t *testing.T) {
fptr := getobjfptr1(-1)
if fptr[8] != -1 {
t.Fail()
}
for i := 0; i < 1000000; i++ {
getobjfptr1(int64(i))
if fptr[8] != -1 {
t.Fail()
break
}
}
}
func TestMemSafeForGetObjFptr2(t *testing.T) {
fptr := getobjfptr2(-1)
if fptr[8] != -1 {
t.Fail()
}
for i := 0; i < 1000000; i++ {
getobjfptr2(int64(i))
if fptr[8] != -1 {
t.Fail()
break
}
}
}
func TestMemCopy(t *testing.T) {
var numa = int64(0)
var numb = int64(455)
memcopy(unsafe.Pointer(&numa), unsafe.Pointer(&numb), 8)
if numa != numb {
t.Error("1")
}
numa = 777
var ptra = &numa
var ptrb = &numb
memcopy(unsafe.Pointer(&ptra), unsafe.Pointer(&ptrb), 8)
if ptra != ptrb {
t.Error("2")
}
if *ptra != numb {
t.Error("3")
}
}
type MemSafeSet struct {
Obj *[1280]int64
}
var (
mssobjf *Field
)
func init() {
mptr := Ptr[MemSafeSet]()
mssobjf = FieldOf[MemSafeSet](&mptr.Obj)
}
func setobjfptr1(v int64) *MemSafeSet {
ele := MemSafeSet{}
objptr := &[1280]int64{}
objptr[8] = v
mssobjf.AssignTo(unsafe.Pointer(&ele), objptr)
return &ele
}
func TestMemSafeForSet(t *testing.T) {
obj := setobjfptr1(-1)
if obj.Obj[8] != -1 {
t.Fail()
}
for i := 0; i < 100000; i++ {
setobjfptr1(int64(i))
if obj.Obj[8] != -1 {
t.Fail()
}
}
}