-
Notifications
You must be signed in to change notification settings - Fork 168
/
cgoMemory.go
56 lines (47 loc) · 935 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 (c *CgoMemoryManage) Retain() {
atomic.AddInt32(&c.retainCount, 1)
debugLogf("Retain", c)
}
func (c *CgoMemoryManage) RetainCount() int32 {
return c.retainCount + 1
}
func (c *CgoMemoryManage) Release() {
atomic.AddInt32(&c.retainCount, -1)
debugLogf("Release", c)
}
func (c *CgoMemoryManage) Free() {
debugLogf("Free", c)
}