forked from tinygo-org/cbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptrmap.go
41 lines (31 loc) · 630 Bytes
/
ptrmap.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
package cbgo
import (
"sync"
"unsafe"
)
// ptrMap is a thread-safe map with pointer keys and generic values.
type ptrMap struct {
m map[unsafe.Pointer]interface{}
mtx sync.RWMutex
}
func newPtrMap() *ptrMap {
return &ptrMap{
m: map[unsafe.Pointer]interface{}{},
}
}
func (pm *ptrMap) find(p unsafe.Pointer) interface{} {
pm.mtx.RLock()
defer pm.mtx.RUnlock()
return pm.m[p]
}
func (pm *ptrMap) add(p unsafe.Pointer, itf interface{}) error {
pm.mtx.Lock()
defer pm.mtx.Unlock()
pm.m[p] = itf
return nil
}
func (pm *ptrMap) del(p unsafe.Pointer) {
pm.mtx.Lock()
defer pm.mtx.Unlock()
delete(pm.m, p)
}