aboutsummaryrefslogtreecommitdiffstats
path: root/cshared/objects.go
blob: d83f2249bd6c9b1c72551b9bbbcfcec3914fa2db (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// +build ignore
package main

import (
	"C"
	"fmt"
	"math"
	"reflect"
	"sync"
)

type Handle uint64

const (
	ErrorCodeSuccess  = iota
	ErrorCodeNotFound = -iota
	ErrorCodeInternal = -iota
)

const MessageNotFound string = "object not found"
const InvalidHandle Handle = 0
const IH uint64 = uint64(InvalidHandle)

var counter Handle = InvalidHandle
var opMutex sync.Mutex
var registryHandle2Obj map[Handle]interface{} = map[Handle]interface{}{}
var registryObj2Handle map[uintptr][]Handle = map[uintptr][]Handle{}
var trace bool = false

func getNewHandle() Handle {
	counter++
	if counter == math.MaxUint64 {
		panic("Handle cache is exhausted")
	}
	return counter
}

func RegisterObject(obj interface{}) Handle {
	data_ptr := reflect.ValueOf(&obj).Elem().InterfaceData()[1]
	if trace {
		fmt.Printf("RegisterObject 0x%x\t%v\n", data_ptr, obj)
	}
	opMutex.Lock()
	defer opMutex.Unlock()
	handles, ok := registryObj2Handle[data_ptr]
	if ok {
		for _, h := range handles {
			other, ok := registryHandle2Obj[h]
			if !ok {
				panic("Inconsistent internal object mapping state (1)")
			}
			if other == obj {
				if trace {
					fmt.Printf("RegisterObject 0x%x reused %d\n", data_ptr, h)
				}
				return h
			}
		}
	}
	handle := getNewHandle()
	registryHandle2Obj[handle] = obj
	registryObj2Handle[data_ptr] = append(registryObj2Handle[data_ptr], handle)
	if trace {
		c_dump_objects()
	}
	return handle
}

func UnregisterObject(handle Handle) int {
	if trace {
		fmt.Printf("UnregisterObject %d\n", handle)
	}
	if handle == InvalidHandle {
		return ErrorCodeNotFound
	}
	opMutex.Lock()
	defer opMutex.Unlock()
	obj, ok := registryHandle2Obj[handle]
	if !ok {
		return ErrorCodeNotFound
	}
	delete(registryHandle2Obj, handle)
	data_ptr := reflect.ValueOf(&obj).Elem().InterfaceData()[1]
	other_handles, ok := registryObj2Handle[data_ptr]
	if !ok {
		panic(fmt.Sprintf("Inconsistent internal object mapping state (2): %d",
			handle))
	}
	hi := -1
	for i, h := range other_handles {
		if h == handle {
			hi = i
			break
		}
	}
	if hi < 0 {
		panic(fmt.Sprintf("Inconsistent internal object mapping state (3): %d",
			handle))
	}
	if len(other_handles) == 1 {
		delete(registryObj2Handle, data_ptr)
	} else {
		registryObj2Handle[data_ptr] = append(other_handles[:hi], other_handles[hi+1:]...)
	}
	if trace {
		c_dump_objects()
	}
	return ErrorCodeSuccess
}

func GetObject(handle Handle) (interface{}, bool) {
	if handle == InvalidHandle {
		return nil, false
	}
	opMutex.Lock()
	defer opMutex.Unlock()
	a, b := registryHandle2Obj[handle]
	return a, b
}

func GetHandle(obj interface{}) (Handle, bool) {
	data_ptr := reflect.ValueOf(&obj).Elem().InterfaceData()[1]
	opMutex.Lock()
	defer opMutex.Unlock()
	handles, ok := registryObj2Handle[data_ptr]
	if !ok {
		return InvalidHandle, false
	}
	for _, h := range handles {
		candidate := registryHandle2Obj[h]
		if candidate == obj {
			return h, true
		}
	}
	return InvalidHandle, false
}

func CopyString(str string) string {
	buf := make([]byte, len(str))
	copy(buf, []byte(str))
	return string(buf)
}

// https://github.com/golang/go/issues/14838
func CBytes(bytes []byte) *C.char {
	ptr := C.malloc(C.size_t(len(bytes)))
	copy((*[1 << 30]byte)(ptr)[:], bytes)
	return (*C.char)(ptr)
}

func SafeIsNil(v reflect.Value) bool {
	defer func() { recover() }()
	return v.IsNil()
}

//export c_dispose
func c_dispose(handle uint64) {
	UnregisterObject(Handle(handle))
}

//export c_objects_size
func c_objects_size() int {
	return len(registryHandle2Obj)
}

//export c_dump_objects
func c_dump_objects() {
	fmt.Printf("handles (%d):\n", len(registryHandle2Obj))
	for h, obj := range registryHandle2Obj {
		fmt.Printf("0x%x\t0x%x  %v\n", h,
			reflect.ValueOf(&obj).Elem().InterfaceData()[1], obj)
	}
	fmt.Println()
	phs := 0
	for _, h := range registryObj2Handle {
		phs += len(h)
	}
	fmt.Printf("pointers (%d):\n", phs)
	for ptr, h := range registryObj2Handle {
		fmt.Printf("0x%x\t%v\n", ptr, h)
	}
}

//export c_set_trace
func c_set_trace(val bool) {
	trace = val
}

// dummy main() is needed by the linker
func main() {}