-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.go
More file actions
85 lines (67 loc) · 1.52 KB
/
alloc.go
File metadata and controls
85 lines (67 loc) · 1.52 KB
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
package alloc
import (
"arena"
"github.com/Inspirate789/alloc/internal/generation"
"runtime"
)
type Getter[T any] interface {
Get() *T
}
type SliceGetter[T any] interface {
Get() []T
}
type getter[T any] struct {
metadata *generation.ObjectMetadata
get func() *T
}
func (g getter[T]) Get() *T {
return g.get()
}
type sliceGetter[T any] struct {
metadata *generation.ObjectMetadata
get func() []T
}
func (g sliceGetter[T]) Get() []T {
return g.get()
}
func New[T any]() Getter[T] {
metadata := allocateObject[T](mainHypervisor.mem)
g := getter[T]{
metadata: metadata,
get: func() *T {
metadata.RLock()
res := (*T)(metadata.Address)
metadata.RUnlock()
return res
},
}
runtime.SetFinalizer(&g, func(_ *getter[T]) {
metadata.Finalized.Swap(true)
})
return g
}
func MakeSlice[T any](len, cap int) SliceGetter[T] {
metadata := allocateSlice[T](mainHypervisor.mem, len, cap)
g := sliceGetter[T]{
metadata: &metadata.ObjectMetadata,
get: func() []T {
//metadata.RLock()
res := generation.MakeSliceFromPtr[T](metadata.Address, metadata.Len, metadata.Cap)
//metadata.RUnlock()
return res
},
}
runtime.SetFinalizer(&g, func(_ *sliceGetter[T]) {
metadata.Finalized.Swap(true)
})
return g
}
func Append[T any](getter SliceGetter[T], elems ...T) {
appendSlice(mainHypervisor.mem, getter.Get(), elems...)
}
func Clone[T any](getter Getter[T]) T {
return arena.Clone[T](*getter.Get())
}
func CloneSlice[S ~[]E, E any](getter SliceGetter[E]) S {
return arena.Clone[S](getter.Get())
}