forked from cztomczak/cef2go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcef_base.go
More file actions
166 lines (144 loc) · 3.18 KB
/
cef_base.go
File metadata and controls
166 lines (144 loc) · 3.18 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2014 The cef2go authors. All rights reserved.
// License: BSD 3-clause.
// Website: https://github.com/CzarekTomczak/cef2go
// Website: https://github.com/fromkeith/cef2go
package chrome
/*
#include <stdlib.h>
#include "string.h"
#include "include/capi/cef_app_capi.h"
#include "cef_base.h"
extern int releaseVoid(void * self);
*/
import "C"
import (
"errors"
log "github.com/cihub/seelog"
"sync"
"unsafe"
)
// a niave memory management.
// allows us to keep track of allocated resources, and their counts
// furthermore, the deconstructor lets us free any go resources once
// their C versions have been released
// General info about the ref count in CEF:
// http://code.google.com/p/chromiumembedded/wiki/UsingTheCAPI
type MemoryManagedBridge struct {
Count int
Deconstructor func(it unsafe.Pointer)
Name string
}
var (
memoryBridge = make(map[unsafe.Pointer]MemoryManagedBridge)
refCountLock sync.Mutex
)
//export go_AddRef
func go_AddRef(it unsafe.Pointer) {
if it == nil {
return
}
refCountLock.Lock()
defer refCountLock.Unlock()
if m, ok := memoryBridge[it]; ok {
m.Count++
memoryBridge[it] = m
// log.Debug("Adding new reference: ", it)
return
}
}
//export go_Release
func go_Release(it unsafe.Pointer) int {
if it == nil {
return 0
}
refCountLock.Lock()
defer refCountLock.Unlock()
if m, ok := memoryBridge[it]; ok {
m.Count--
if m.Count == 0 {
if m.Deconstructor != nil {
m.Deconstructor(it)
}
C.free(it)
delete(memoryBridge, it)
return 1
} else {
//log.Debug("Reduce reference count for: ", it)
memoryBridge[it] = m
}
return 0
}
return 0
}
//export go_HasOneReferenceCount
func go_HasOneReferenceCount(it unsafe.Pointer) int {
refCountLock.Lock()
defer refCountLock.Unlock()
if m, ok := memoryBridge[it]; ok {
if m.Count == 1 {
return 1
}
return 0
}
return 0
}
func Release(w unsafe.Pointer) int {
return int(C.releaseVoid(w))
}
func AddRef(it unsafe.Pointer) {
C.add_refVoid(it)
}
//export go_CreateRef
func go_CreateRef(it unsafe.Pointer, name *C.char) {
goname := ""
if name != nil {
goname = C.GoString(name)
}
log.Info("Create Reference for ", goname)
CreateRef(it, goname)
}
func CreateRef(it unsafe.Pointer, name string) {
refCountLock.Lock()
defer refCountLock.Unlock()
if _, ok := memoryBridge[it]; !ok {
var m MemoryManagedBridge
m.Deconstructor = nil
m.Name = name
memoryBridge[it] = m
return
}
}
func RegisterDestructor(it unsafe.Pointer, decon func(it unsafe.Pointer)) bool {
refCountLock.Lock()
defer refCountLock.Unlock()
if m, ok := memoryBridge[it]; ok {
m.Deconstructor = decon
memoryBridge[it] = m
return true
}
return false
}
func DumpRefs() {
log.Info("Dumping reference : ")
refCountLock.Lock()
defer refCountLock.Unlock()
for k, v := range memoryBridge {
log.Info("%X : %#v", k, v)
}
}
func clearAllRefs() {
refCountLock.Lock()
defer refCountLock.Unlock()
for k, v := range memoryBridge {
v.Count = 1
go_Release(k)
}
}
func getRef(it unsafe.Pointer) (error, MemoryManagedBridge) {
if m, ok := memoryBridge[it]; ok {
return nil, m
} else {
mem := MemoryManagedBridge{}
return errors.New("No reference found"), mem
}
}