-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
63 lines (47 loc) · 1.02 KB
/
int.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
package goumem
import (
"github.com/exapsy/goumem/allocator"
"sync"
"unsafe"
)
type PointerInt struct {
allocatedBlock *allocator.AllocatedBlock
mutex sync.RWMutex
}
func NewInt(i int) (*PointerInt, error) {
ptr := &PointerInt{}
var err error
var block *allocator.AllocatedBlock
block, err = mem.Alloc(unsafe.Sizeof(i))
if err != nil {
return nil, err
}
ptr.allocatedBlock = block
ptr.Set(i)
return ptr, nil
}
func (ptr *PointerInt) Address() uintptr {
ptr.mutex.RLock()
defer ptr.mutex.RUnlock()
return ptr.allocatedBlock.Addr()
}
func (ptr *PointerInt) Value() int {
ptr.mutex.RLock()
defer ptr.mutex.RUnlock()
return *(*int)(unsafe.Pointer(ptr.allocatedBlock.Addr()))
}
func (ptr *PointerInt) Set(i int) {
ptr.mutex.Lock()
defer ptr.mutex.Unlock()
*(*int)(unsafe.Pointer(ptr.allocatedBlock.Addr())) = i
}
func (ptr *PointerInt) Free() error {
ptr.mutex.Lock()
defer ptr.mutex.Unlock()
return mem.Free(ptr.allocatedBlock)
}
func init() {
if mem == nil {
mem = allocator.Default()
}
}