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
|
// 23 february 2015
package ui
// Go pointers cannot be passed to C safely.
// The Go runtime will move them around, tripping C up.
// However, C pointers don't have this problem.
// Solution: use the C pointer to get the Go object needed!
import (
"fmt"
"sync"
)
var (
ids map[uintptr]interface{}
idsLock sync.Mutex
)
func getGoObject(cptr uintptr) interface{} {
idsLock.Lock()
defer idsLock.Unlock()
if i, ok := ids[cptr]; ok {
return i
}
panic(fmt.Errorf("[BUG in package ui; report to andlabs immediately] C pointer %p not associated with Go object in package ui", cptr))
}
func associateGoObject(cptr uintptr, goobj interface{}) {
idsLock.Lock()
defer idsLock.Unlock()
if i, ok := ids[cptr]; ok {
panic(fmt.Errorf("[BUG in package ui; report to andlabs immediately] C pointer %p already associated with Go object of type %T but we want to associate it with Go object of type %T", cptr, i, goobj))
}
ids[cptr] = goobj
}
func disassociateGoObject(cptr uintptr) {
idsLock.Lock()
defer idsLock.Unlock()
if _, ok := ids[cptr]; !ok {
panic(fmt.Errorf("[BUG in package ui; report to andlabs immediately] C pointer %p not associated with any Go object but we want to disassociate it", cptr))
}
delete(ids, cptr)
}
func idsHandler(req *request) bool {
switch req.id {
case reqObjectDestroyed:
disassociateGoObject(req.ptr)
return true
}
return false
}
func init() {
interopHandlers = append(interopHandlers, idsHandler)
}
|