aboutsummaryrefslogtreecommitdiff
path: root/librsync/pointers.go
blob: d14f41f37734f01157cb96cd643fe1e187e7985b (plain)
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
package librsync

import (
	"sync"
)

// pointerMap holds Go *Patcher objects to pass to C.
// Don't touch this data structure, instead use the storePatcher, getPatcher,
// and dropPatcher functions.
var patcherStore = struct {
	lock  sync.Mutex
	store map[uintptr]*Patcher
}{
	store: make(map[uintptr]*Patcher),
}

// storePatcher stores the value and returns a reference to it, for use in a CGo
// call. Use the same reference for dropPatcher. C callbacks can use getPatcher
// to get the original value.
func storePatcher(patcher *Patcher, id uintptr) {
	patcherStore.lock.Lock()
	defer patcherStore.lock.Unlock()

	if _, ok := patcherStore.store[id]; ok {
		// Just to be on the safe side.
		panic("pointer already stored")
	}
	patcherStore.store[id] = patcher
}

// getPatcher returns the value for the reference id. It returns nil if there
// is no such reference.
func getPatcher(id uintptr) *Patcher {
	patcherStore.lock.Lock()
	defer patcherStore.lock.Unlock()

	return patcherStore.store[id]
}

// dropPatcher unreferences the value so the garbage collector can free it's
// memory.
func dropPatcher(id uintptr) {
	patcherStore.lock.Lock()
	defer patcherStore.lock.Unlock()

	if _, ok := patcherStore.store[id]; !ok {
		panic("pointer not stored")
	}

	delete(patcherStore.store, id)
}