-
Notifications
You must be signed in to change notification settings - Fork 123
/
xmm-test02.go
153 lines (130 loc) · 3.23 KB
/
xmm-test02.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
XMM 简单示例 - 02
XMM Simple Example - 02
目标:使用XMM构建一个单链表程序
Objective: To build a single linked table program using XMM
说明:示例复杂场景中使用XMM内存库
Description: Example complex scenario using the XMM memory library
*/
package main
import (
"fmt"
"unsafe"
"github.com/heiyeluren/xmm"
)
// XListNode Define the node structure of a chain table
// 定义一个链表的节点结构
type XListNode struct {
Val int
Next *XListNode
}
// XList Single linked table main structure
// 单链表主结构
type XList struct {
Head *XListNode
// Tail *XListNode
}
// Init Initialize the chain table
// 初始化链表
// Initialize the chain table
func (l *XList) Init(mm xmm.XMemory) {
Node, err := mm.Alloc(unsafe.Sizeof(XListNode{}))
if err != nil {
panic("Alloc fail")
}
// head := &XListNode{Val:list[0]}
head := (*XListNode)(Node)
l.Head = head
fmt.Println("Init Xlist done")
}
// Append Link table adds nodes
// 链表增加节点
func (l *XList) Append(i int, mm xmm.XMemory) {
h := l.Head
for h.Next != nil {
h = h.Next
}
p, err := mm.Alloc(unsafe.Sizeof(XListNode{}))
if err != nil {
panic("Alloc fail")
}
Node := (*XListNode)(p)
Node.Val = i
Node.Next = nil
h.Next = Node
fmt.Println("Append item:", Node.Val)
}
// Show iterate through all the chain table nodes and print
// 遍历所有链表节点并打印
func (l *XList) Show() {
h := l.Head
// fmt.Println(h.Val)
for h.Next != nil {
h = h.Next
fmt.Println("Show item:", h.Val)
}
}
// Destroy Releasing the entire linked table structure
// 释放整个链表结构
func (l *XList) Destroy(mm xmm.XMemory) {
cnt := 0
h := l.Head
// fmt.Println(h.Val)
// 统计需要释放总数
// Count the total number of releases required
for h.Next != nil {
h = h.Next
cnt++
// fmt.Println(h.Val)
}
// fmt.Println("item count:", cnt)
// 循环释放所有内存
// Loop to free all memory
for i := 0; i <= cnt; i++ {
h := l.Head
pre := l.Head
for h.Next != nil {
pre = h
h = h.Next
}
fmt.Println("Free item:", h.Val)
mm.Free(uintptr(unsafe.Pointer(h)))
pre.Next = nil
}
}
// 主函数
// Main functions
func main() {
// 初始化XMM对象
// Initialise the XMM object
f := &xmm.Factory{}
// 从操作系统申请一个内存块,如果内存使用达到60%,就进行异步自动扩容,每次异步扩容256MB内存(固定值)
// Request a block of memory from the OS and perform asynchronous auto-expansion if memory usage reaches 60%, 256MB of memory per asynchronous expansion (fixed value)
mm, err := f.CreateMemory(0.6)
if err != nil {
panic("CreateMemory fail ")
}
// 要生成的数字列表
// List of numbers to be generated
list := []int{2, 4, 3}
fmt.Println("\n===== XMM X(eXtensible) Memory Manager example 02 - LinkedList ======\n")
// 初始化链表
// Initialize LinkedList
l := &XList{}
l.Init(mm)
fmt.Println("")
// 把元素压入链表
// Pressing elements into a LinkedList
for i := 0; i < len(list); i++ {
l.Append(list[i], mm)
}
fmt.Println("")
// Iterate through all the linked table data
l.Show()
fmt.Println("")
// Free all LinkedList memory
l.Destroy(mm)
// 结束
// End
fmt.Println("\n===== Example test success ======\n")
}