Skip to content

Commit

Permalink
change XMM test code
Browse files Browse the repository at this point in the history
  • Loading branch information
heiyeluren authored Feb 17, 2022
1 parent ce78ce8 commit 2d49e12
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 25 deletions.
15 changes: 14 additions & 1 deletion example/xmm-test00.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/*
XMM Example 00
Goal: How to use XMM quickly and easily
Description: An example of how to use the XMM memory library quickly and easily.
XMM 示例00
目标:如何简单快速使用XMM
Expand All @@ -9,39 +14,46 @@ package main

import (
//xmm "xmm/src"
xmm "github.com/heiyeluren/xmm/src"
xmm "github.com/heiyeluren/xmm"
"fmt"
"unsafe"
)

func main() {

//Initialising XMM objects
//初始化XMM对象
f := &xmm.Factory{}

// Request a memory block from the operating system
//If memory usage reaches 60%, asynchronous automatic expansion is performed, each time 256MB of memory is expanded asynchronously (fixed value), the value of 0.6 can be configured independently
//从操作系统申请一个内存块
//如果内存使用达到60%,就进行异步自动扩容,每次异步扩容256MB内存(固定值),0.6这个数值可以自主配置
mm, err := f.CreateMemory(0.6)
if err != nil {
panic("CreateMemory fail ")
}

// manipulate int types, request memory and assign a value
//操作int类型,申请内存后赋值
var tmpNum int = 9527
p, err := mm.Alloc(unsafe.Sizeof(tmpNum))
if err != nil {
panic("Alloc fail ")
}
Id := (*int)(p)
// Write the set number assignment to the XMM memory
//把设定好的数字赋值写入到XMM内存中
*Id = tmpNum

// To manipulate string types, XMM provides the From() interface to get a pointer directly and the string will be stored in XMM
//操作字符串类型,XMM提供了From()接口,可以直接获得一个指针,字符串会存储在XMM中
Name, err := mm.From("heiyeluren")
if err != nil {
panic("Alloc fail ")
}

// Output variables and memory addresses from XMM memory
//从XMM内存中输出变量和内存地址
fmt.Println("\n===== XMM X(eXtensible) Memory Manager example 00 ======\n")
fmt.Println("\n-- Memory data status --\n")
Expand All @@ -50,6 +62,7 @@ func main() {

fmt.Println("\n===== Example test success ======\n")

//Free the Id,Name memory block
//释放Id,Name内存块
mm.Free(uintptr(p))
mm.FreeString(Name)
Expand Down
23 changes: 21 additions & 2 deletions example/xmm-test01.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/*
XMM 简单示例 - 01
XMM Simple Example - 01
目标:如何在结构体中使用XMM
说明:示例如何结构体类场景如何使用XMM内存库
Objective: How to use XMM in structures
说明:示例如何结构体类场景如何使用XMM内存库
Description: Example of how to use the XMM memory library in a structure class scenario
*/
package main

import (
//xmm "xmm/src"
xmm "github.com/heiyeluren/xmm/src"
xmm "github.com/heiyeluren/xmm"
"fmt"
"unsafe"
)

func main() {

//定义一个类型(结构体)
// Define a type (structure)
type User struct {
Id uint
Name string
Expand All @@ -25,46 +30,60 @@ func main() {
}

//初始化XMM对象
// Initialise the XMM object
f := &xmm.Factory{}

//从操作系统申请一个内存块
//如果内存使用达到60%,就进行异步自动扩容,每次异步扩容256MB内存(固定值),0.6这个数值可以自主配置
// Request a memory block from the operating system
//If memory usage reaches 60%, asynchronous automatic expansion is performed, each time 256MB of memory is expanded asynchronously (fixed value), the value of 0.6 can be configured independently

mm, err := f.CreateMemory(0.6)
if err != nil {
panic("CreateMemory fail ")
}

//自己从内存块中申请一小段自己想用的内存
// Request a small section of memory from the memory block yourself that you want to use
size := unsafe.Sizeof(User{})
p, err := mm.Alloc(size)
if err != nil {
panic("Alloc fail ")
}

//使用该内存块,进行结构体元素赋值
// Use this memory block for structure element assignment
user := (*User)(p)
user.Id = 1
user.Age = 18
user.Name = "heiyeluren"
user.Email = "[email protected]"

//输出变量,打印整个结构体等
// Output variables, print entire structures, etc.
fmt.Println("\n===== XMM X(eXtensible) Memory Manager example 01 ======\n")

fmt.Println("\n-- Memory data status --\n")
fmt.Println("User ptr addr: \t", p)
fmt.Println("User data: \t", user)

//释放内存块(实际是做mark标记操作)
//Free memory blocks (actually doing a mark mark operation)
mm.Free(uintptr(p))

//Free()后再看看变量值,只是针对这个内存块进行mark标记动作,并未彻底从内存中释放(XMM设计机制,降低实际gc回收空闲时间)
//XMM内部会有触发gc的机制,主要是内存容量,参数TotalGCFactor=0.0004,目前如果要配置,需要自己修改这个常量,一般不用管它,Free()操作中有万分之4的概率会命中触发gc~
//GC触发策略:待释放内存 > 总内存 * 万分之4 会触发gc动作
//After Free() and then look at the variable value, only for this memory block to mark mark action, not completely released from memory (XMM design mechanism to reduce the actual gc recovery idle time)
//XMM will have an internal mechanism to trigger gc, mainly memory capacity, parameter TotalGCFactor=0.0004, currently if you want to configure, you need to modify this constant yourself, generally do not bother with it, Free() operation has a 4 in 10,000 probability of hitting the trigger gc ~
//GC trigger policy: memory to be freed > total memory * 4 in 10,000 will trigger gc action

fmt.Println("\n-- Memory data status after XMM.Free() --\n")
fmt.Println("memory ptr addr:\t", p)
fmt.Println("User data:\t\t", user)

//结束
// End
fmt.Println("\n===== Example test success ======\n")
}

26 changes: 23 additions & 3 deletions example/xmm-test02.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
/*
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 (
//xmm "xmm/src"
xmm "github.com/heiyeluren/xmm/src"
xmm "github.com/heiyeluren/xmm"
"fmt"
"unsafe"
)

//定义一个链表的节点结构
// Define the node structure of a chain table
type XListNode struct {
Val int
Next *XListNode
}

//单链表主结构
/ Single linked table main structure
type XList struct {
Head *XListNode
//Tail *XListNode
}


//初始化链表
//Initialize the chain table
func (l *XList) Init( mm xmm.XMemory ) {
Node,err := mm.Alloc(unsafe.Sizeof(XListNode{}))
if err != nil {
Expand All @@ -39,6 +46,7 @@ func (l *XList) Init( mm xmm.XMemory ) {
}

//链表增加节点
//Link table adds nodes
func (l *XList) Append(i int, mm xmm.XMemory) {
h := l.Head
for h.Next != nil {
Expand All @@ -57,6 +65,7 @@ func (l *XList) Append(i int, mm xmm.XMemory) {
}

//遍历所有链表节点并打印
// iterate through all the chain table nodes and print
func (l *XList) Show() {
h := l.Head
//fmt.Println(h.Val)
Expand All @@ -67,12 +76,14 @@ func (l *XList) Show() {
}

//释放整个链表结构
//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++
Expand All @@ -81,6 +92,7 @@ func (l *XList) Destroy(mm xmm.XMemory) {
//fmt.Println("item count:", cnt)

//循环释放所有内存
// Loop to free all memory
for i := 0; i <= cnt; i++ {
h := l.Head
pre := l.Head
Expand All @@ -95,41 +107,49 @@ func (l *XList) Destroy(mm xmm.XMemory) {
}

//主函数
// 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")
}

Expand Down
Loading

0 comments on commit 2d49e12

Please sign in to comment.