-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathappend.go
More file actions
107 lines (100 loc) · 2.89 KB
/
append.go
File metadata and controls
107 lines (100 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package slices
import (
"solod.dev/so/c"
"solod.dev/so/mem"
)
// Append appends elements to a heap-allocated slice, growing it if needed.
// If the allocator is nil, uses the system allocator.
// Returns an updated allocated slice; the caller owns it.
//
//so:extern
func Append[T any](a mem.Allocator, s []T, elems ...T) []T {
return append(s, elems...)
}
// Extend appends all elements from another heap-allocated slice, growing if needed.
// If the allocator is nil, uses the system allocator.
// Returns an updated allocated slice; the caller owns it.
//
//so:extern
func Extend[T any](a mem.Allocator, s []T, other []T) []T {
return append(s, other...)
}
// nextcap computes the capacity for a grown slice using Go's growth
// formula: 2x for small slices (< 256 elements), transitioning to ~1.25x
// for larger ones.
//
//so:inline
func slices_nextcap(newLen, oldCap int) int {
newCap := oldCap
doubleCap := newCap + newCap
if newLen > doubleCap {
return newLen
}
const threshold = 256
if oldCap < threshold {
return doubleCap
}
for {
newCap += (newCap + 3*threshold) >> 2
if newCap >= newLen {
break
}
}
return newCap
}
// grow grows a slice's backing allocation to hold at least newLen elements.
// Returns a result with the updated slice or an error if reallocation fails.
// If the allocator is nil, uses the system allocator.
//
//so:inline
func slices_grow(a mem.Allocator, s Slice, newLen, elemSize, elemAlign int) sliceResult {
if a == nil {
a = mem.System
}
if newLen <= s.cap {
return sliceResult{val: s, err: nil}
}
newCap := slices_nextcap(newLen, s.cap)
oldSize := s.cap * elemSize
newSize := newCap * elemSize
var newPtr any
var err error
if s.cap == 0 {
newPtr, err = a.Alloc(newSize, elemAlign)
} else {
newPtr, err = a.Realloc(s.ptr, oldSize, newSize, elemAlign)
}
if err != nil {
return sliceResult{val: s, err: err}
}
s.ptr = newPtr.(*byte)
s.cap = newCap
return sliceResult{val: s, err: nil}
}
// tryExtend appends all elements from another slice, growing if needed.
// Returns a result with the updated slice or an error if reallocation fails.
// If the allocator is nil, uses the system allocator.
//
//so:inline
func slices_tryExtend(a mem.Allocator, s Slice, other Slice, elemSize, elemAlign int) sliceResult {
res := slices_grow(a, s, s.len+other.len, elemSize, elemAlign)
if res.err != nil {
return res
}
s = res.val
mem.Copy(c.PtrAdd(s.ptr, s.len*elemSize), other.ptr, other.len*elemSize)
s.len += other.len
return sliceResult{val: s, err: nil}
}
// extend appends all elements from another slice, growing if needed.
// Returns the updated slice or panics on allocation failure.
// If the allocator is nil, uses the system allocator.
//
//so:inline
func slices_extend(a mem.Allocator, s Slice, other Slice, elemSize, elemAlign int) Slice {
res := slices_tryExtend(a, s, other, elemSize, elemAlign)
if res.err != nil {
panic(res.err)
}
return res.val
}