Debian Codegraph @

generic/arena.go

raw
1 // Copyright 2023 Huan Du. All rights reserved.
2 // Licensed under the MIT license that can be found in the LICENSE file.
3
4 //go:build go1.20 && goexperiment.arenas
5 // +build go1.20,goexperiment.arenas
6
7 package clone
8
9 import (
10 "arena"
11 "reflect"
12 "runtime"
13 "unsafe"
14
15 "github.com/huandu/go-clone"
16 )
17
18 // The arenaAllocator allocates memory from arena.
19 var arenaAllocatorMethods = &clone.AllocatorMethods{
20 New: arenaNew,
21 MakeSlice: arenaMakeSlice,
22 MakeMap: arenaMakeMap,
23 MakeChan: arenaMakeChan,
24 }
25
26 // FromArena creates an allocator using arena a to allocate memory.
27 func FromArena(a *arena.Arena) *clone.Allocator {
28 return clone.NewAllocator(unsafe.Pointer(a), arenaAllocatorMethods)
29 }
30
31 // ArenaClone recursively deep clones v to a new value in arena a.
32 // It works in the same way as Clone, except it allocates all memory from arena.
33 func ArenaClone[T any](a *arena.Arena, v T) (nv T) {
34 src := reflect.ValueOf(v)
35 cloned := FromArena(a).Clone(src)
36
37 if !cloned.IsValid() {
38 return
39 }
40
41 dst := reflect.ValueOf(&nv).Elem()
42 dst.Set(cloned)
43 return
44 }
45
46 // ArenaCloneSlowly recursively deep clones v to a new value in arena a.
47 // It works in the same way as Slowly, except it allocates all memory from arena.
48 func ArenaCloneSlowly[T any](a *arena.Arena, v T) (nv T) {
49 src := reflect.ValueOf(v)
50 cloned := FromArena(a).CloneSlowly(src)
51
52 if !cloned.IsValid() {
53 return
54 }
55
56 dst := reflect.ValueOf(&nv).Elem()
57 dst.Set(cloned)
58 return
59 }
60
61 func arenaNew(pool unsafe.Pointer, t reflect.Type) reflect.Value {
62 return reflect.ArenaNew((*arena.Arena)(pool), reflect.PtrTo(t))
63 }
64
65 // Define the slice header again to mute golint's warning.
66 type sliceHeader reflect.SliceHeader
67
68 func arenaMakeSlice(pool unsafe.Pointer, t reflect.Type, len, cap int) reflect.Value {
69 a := (*arena.Arena)(pool)
70
71 // As of go1.20, there is no reflect method to allocate slice in arena.
72 // Following code is a hack to allocate a large enough byte buffer
73 // and then cast it to T[].
74 et := t.Elem()
75 l := int(et.Size())
76 total := l * cap
77
78 data := arena.MakeSlice[byte](a, total, total)
79 ptr := unsafe.Pointer(&data[0])
80 elem := reflect.NewAt(et, ptr)
81 slicePtr := reflect.ArenaNew(a, reflect.PtrTo(t))
82 *(*sliceHeader)(slicePtr.UnsafePointer()) = sliceHeader{
83 Data: elem.Pointer(),
84 Len: l,
85 Cap: cap,
86 }
87 runtime.KeepAlive(elem)
88
89 slice := slicePtr.Elem()
90 return slice.Slice3(0, len, cap)
91 }
92
93 func arenaMakeMap(pool unsafe.Pointer, t reflect.Type, n int) reflect.Value {
94 // As of go1.20, there is no way to allocate map in arena.
95 // Fallback to heap allocation.
96 return reflect.MakeMapWithSize(t, n)
97 }
98
99 func arenaMakeChan(pool unsafe.Pointer, t reflect.Type, buffer int) reflect.Value {
100 // As of go1.20, there is no way to allocate chan in arena.
101 // Fallback to heap allocation.
102 return reflect.MakeChan(t, buffer)
103 }