-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena.go
67 lines (57 loc) · 1.61 KB
/
arena.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
package memoryArena
import (
"fmt"
"reflect"
"unsafe"
)
// interface for MemoryArena and ConcurrentArena behaviours
type Arena interface {
Reset()
AllocateObject(obj interface{}) (unsafe.Pointer, error)
}
// Object is being allocated in the Arena.
func AllocateObject[T any](arena Arena, obj T) (unsafe.Pointer, error) {
return arena.AllocateObject(obj)
}
// Resetting the Arena.
func Reset(arena Arena) {
arena.Reset()
}
// NewObject allocate memory through AllocateObject, returns pointer to T or error handle.
func NewObject[T any](arena Arena, obj T) (*T, error) {
ptr, err := AllocateObject(arena, obj)
if err != nil {
return nil, err
}
return (*T)(ptr), nil
}
// AppendSlice appends object to slice and returns pointer to slice or error handle.
func AppendSlice[T any](obj *T, arena Arena, slice *[]T) (*[]T, error) {
*slice = append(*slice, *obj)
ptr, err := AllocateObject(arena, slice)
if err != nil {
return nil, err
}
return (*[]T)(ptr), nil
}
// InsertMap inserts object to hashmap and returns pointer to hashmap or error handle.
func InsertMap[T any](obj *T, arena Arena, hashmap *map[string]T, key string) (*map[string]T, error) {
(*hashmap)[key] = *obj
ptr, err := AllocateObject(arena, hashmap)
if err != nil {
return nil, err
}
return (*map[string]T)(ptr), nil
}
// SetNewValue sets new value to pointer.
func SetNewValue(ptr *unsafe.Pointer, obj interface{}) (unsafe.Pointer, error) {
if ptr == nil || *ptr == nil {
return nil, fmt.Errorf("invalid pointer provided")
}
newValue := reflect.NewAt(
reflect.TypeOf(obj),
*ptr,
).Elem()
newValue.Set(reflect.ValueOf(obj))
return *ptr, nil
}