-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv8_tracer.go
82 lines (68 loc) · 1.74 KB
/
v8_tracer.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
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
//go:build v8_tracer
package isolates
//#include "v8_c_bridge.h"
//#cgo CXXFLAGS: -I/usr/local/include/v8 -std=c++17
import "C"
import (
"fmt"
"runtime"
"sync"
)
type allocation struct {
RefCount int
StackTrace []byte
}
type _tracer struct {
mutex sync.Mutex
retained map[any]allocation
released map[any]allocation
}
var tracer = newTracer()
func newTracer() *_tracer {
return &_tracer{
retained: map[any]allocation{},
released: map[any]allocation{},
}
}
func (t *_tracer) Retain(object any) {
t.mutex.Lock()
defer t.mutex.Unlock()
if a, ok := t.released[object]; ok {
fmt.Println("\n\n*******************")
fmt.Println("*** UNDERRETAIN ***")
fmt.Println("*******************\n\n")
fmt.Println("First retained here:\n", string(a.StackTrace))
fmt.Println("*******************\n\n")
panic("under retain")
}
if a, ok := t.retained[object]; ok {
a.RefCount++
} else {
stack := make([]byte, 40*1024)
n := runtime.Stack(stack, false)
t.retained[object] = allocation{RefCount: 1, StackTrace: stack[:n]}
}
}
func (t *_tracer) Release(object any) {
t.mutex.Lock()
defer t.mutex.Unlock()
if a, ok := t.released[object]; ok {
fmt.Println("\n\n*********************")
fmt.Println("*** OVERRELEASE ***")
fmt.Println("*******************\n\n")
fmt.Println("First retained here:\n", string(a.StackTrace))
fmt.Println("*******************\n\n")
}
if allocation, ok := t.retained[object]; ok {
allocation.RefCount--
if allocation.RefCount == 0 {
delete(t.retained, object)
t.released[object] = allocation
}
} else {
fmt.Println("\n\n******************************")
fmt.Println("*** RELEASE WITHOUT RETAIN ***")
fmt.Println("******************************\n\n")
panic("release without retain")
}
}