-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalizer.go
52 lines (45 loc) · 1.16 KB
/
finalizer.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
package weakref
import (
fmt "fmt"
unsafe "unsafe"
reflect "reflect"
runtime "runtime"
sync "sync"
)
type Pointer = unsafe.Pointer
type IPointer = [2]uintptr
func ptrToIPointer(ptr interface{})(IPointer){
return *(*IPointer)((Pointer)(&ptr))
}
func iPointerToPtr(ptr IPointer)(interface{}){
return *(*interface{})((Pointer)(&ptr))
}
var finalizer_map_mux sync.RWMutex
var finalizer_map = make(map[IPointer][]func(interface{}))
func SetFinalizer(ptr interface{}, cb func(interface{}))(iptr IPointer){
v := reflect.ValueOf(ptr)
kd := v.Kind()
if kd != reflect.Ptr && kd != reflect.UnsafePointer {
panic(fmt.Errorf("ptr must be a type of pointer or unsafe.Pointer, not %v", kd))
}
iptr = ptrToIPointer(ptr)
finalizer_map_mux.RLock()
s, ok := finalizer_map[iptr]
finalizer_map_mux.RUnlock()
if !ok {
runtime.SetFinalizer((*struct{})((Pointer)(iptr[1])), func(*struct{}){
p := iPointerToPtr(iptr)
finalizer_map_mux.Lock()
s := finalizer_map[iptr]
delete(finalizer_map, iptr)
finalizer_map_mux.Unlock()
for _, cb := range s {
cb(p)
}
})
}
finalizer_map_mux.Lock()
finalizer_map[iptr] = append(s, cb)
finalizer_map_mux.Unlock()
return
}