forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgoMemory.go
56 lines (47 loc) · 965 Bytes
/
cgoMemory.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
package gmf
import (
"log"
"sync/atomic"
)
type CgoMemoryManage struct {
retainCount int32
}
type CgoMemoryManager interface {
Retain()
RetainCount() int32
Release()
Free()
}
func Retain(i CgoMemoryManager) CgoMemoryManager {
//func Retain(i CgoMemoryManager) interface {} {
i.Retain()
return i
}
func Release(i CgoMemoryManager) {
if nil == i {
return
}
i.Release()
if 0 >= i.RetainCount() {
i.Free()
}
}
func debugLogf(functionname string, c *CgoMemoryManage) {
if false {
log.Printf("CgoMemoeryMangaer "+functionname+"(%p) retainCount=%d", c, c.RetainCount())
}
}
func (this *CgoMemoryManage) Retain() {
atomic.AddInt32(&this.retainCount, 1)
debugLogf("Retain", this)
}
func (this *CgoMemoryManage) RetainCount() int32 {
return this.retainCount + 1
}
func (this *CgoMemoryManage) Release() {
atomic.AddInt32(&this.retainCount, -1)
debugLogf("Release", this)
}
func (this *CgoMemoryManage) Free() {
debugLogf("Free", this)
}