-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.go
91 lines (73 loc) · 1.88 KB
/
stack.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package example
import (
"errors"
"unsafe"
"github.com/pboyd/malloc"
)
// ErrStackOverflow is returned by Push when the memory has been exhausted.
var ErrStackOverflow = errors.New("stack overflow")
// ErrStackUnderflow is returned by Pop when the stack is empty.
var ErrStackUnderflow = errors.New("stack underflow")
// Stack is a simple stack for a single data type which uses a fixed amount of memory.
type Stack[T any] struct {
arena *malloc.Arena
top *stackItemHeader
}
type stackItemHeader struct {
data unsafe.Pointer
prev *stackItemHeader
}
// NewStack returns a stack using a specific amount of memory. Size is in
// bytes. The actual memory size may be rounded up.
func NewStack[T any](size uint64) *Stack[T] {
return &Stack[T]{
arena: malloc.NewArena(size),
}
}
// Push adds a copy of an item to the stack.
//
// Returns ErrStackOverflow if the stack is full.
func (s *Stack[T]) Push(item *T) error {
header, err := s.newItemHeader()
if err != nil {
return err
}
data, err := malloc.Malloc[T](s.arena)
if err != nil {
malloc.Free(s.arena, header)
if errors.Is(err, malloc.ErrOutOfMemory) {
return ErrStackOverflow
}
return err
}
*data = *item
header.data = unsafe.Pointer(data)
header.prev = s.top
s.top = header
return nil
}
func (s *Stack[T]) newItemHeader() (*stackItemHeader, error) {
header, err := malloc.Malloc[stackItemHeader](s.arena)
if err != nil {
if errors.Is(err, malloc.ErrOutOfMemory) {
return nil, ErrStackOverflow
}
return nil, err
}
return header, nil
}
// Pop removes the last item pushed onto the stack and returns it.
//
// If the stack is empty ErrStackUnderflow is returned.
func (s *Stack[T]) Pop() (*T, error) {
if s.top == nil {
return nil, ErrStackUnderflow
}
item := (*T)(s.top.data)
dup := *item
oldTop := s.top
s.top = s.top.prev
malloc.Free(s.arena, item)
malloc.Free(s.arena, oldTop)
return &dup, nil
}