-
Notifications
You must be signed in to change notification settings - Fork 8
/
handles.go
67 lines (55 loc) · 1.24 KB
/
handles.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"sync"
"unsafe"
)
// #include "uplink_definitions.h"
import "C"
func mallocHandle(h handle) unsafe.Pointer {
p := calloc(1, C.sizeof_UplinkHandle)
handle := (*C.UplinkHandle)(p)
handle._handle = h
return p
}
// handle is a generic handle.
type handle = C.size_t
// handles stores different Go values that need to be accessed from Go side.
type handles struct {
lock sync.Mutex
nextid handle
values map[handle]interface{}
}
// newHandles creates a place to store go files by handle.
func newHandles() *handles {
return &handles{
values: make(map[handle]interface{}),
}
}
// Add adds a value to the table.
func (m *handles) Add(x interface{}) handle {
m.lock.Lock()
defer m.lock.Unlock()
m.nextid++
m.values[m.nextid] = x
return m.nextid
}
// Get gets a value.
func (m *handles) Get(x handle) interface{} {
m.lock.Lock()
defer m.lock.Unlock()
return m.values[x]
}
// Del deletes the value.
func (m *handles) Del(x handle) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.values, x)
}
// Empty returns whether the handles is empty.
func (m *handles) Empty() bool {
m.lock.Lock()
defer m.lock.Unlock()
return len(m.values) == 0
}