-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (129 loc) · 2.91 KB
/
Copy pathmain.go
File metadata and controls
143 lines (129 loc) · 2.91 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
)
const INIT_BYTES = 1 * 1024 * 1024 // 1mb
type MemBlock struct {
// Where it starts in the byte slice
Offset int
// how big this block is
Size uint
// is this block available
Free bool
// Next block in the list
Next *MemBlock
// Previous block in the list
Prev *MemBlock
}
type SAllocator struct {
arena []byte
head *MemBlock
}
func InitAllocator(number_of_bytes uint) *SAllocator {
sa := &SAllocator{
arena: make([]byte, number_of_bytes),
head: &MemBlock{
Offset: 0,
Size: number_of_bytes,
Free: true,
Next: nil,
Prev: nil,
},
}
return sa
}
func (s *SAllocator) Alloc(number_of_bytes uint) (int, error) {
if number_of_bytes == 0 {
return -1, fmt.Errorf("Invalid memory request of %d bytes is not allowed", number_of_bytes)
}
curr := s.head
for curr != nil {
if curr.Free && curr.Size >= number_of_bytes {
// remainder of what we have after we share memory
rem := curr.Size - number_of_bytes
// if rem is 0 then use the whole block
// else rem is not 0 then we can split this block to the next
if rem == 0 {
curr.Free = false
return curr.Offset, nil
} else {
curr.Free = false
curr.Size = number_of_bytes
s.createNewNode(curr.Next, curr, rem, uint(curr.Offset)+number_of_bytes)
return curr.Offset, nil
}
}
curr = curr.Next
}
return -1, fmt.Errorf("Invalid memory request of %d bytes was not found ", number_of_bytes)
}
func (s *SAllocator) DeAlloc(offset uint) error {
curr := s.head
for curr != nil {
if !curr.Free && curr.Offset == int(offset) {
for curr.Next != nil && curr.Next.Free {
// we need to delete the next node
// update the size of this node
s.deleteNode(curr, curr.Next)
}
for curr.Prev != nil && curr.Prev.Free {
// move to that node and delete the current node im on
curr = curr.Prev
s.deleteNode(curr, curr.Next)
}
curr.Free = true
return nil
}
curr = curr.Next
}
return fmt.Errorf("no allocated block found at offset %d", offset)
}
func (s *SAllocator) createNewNode(next, prev *MemBlock, newSize, offset uint) {
new_node := &MemBlock{
Offset: int(offset),
Size: newSize,
Free: true,
Next: next,
Prev: prev,
}
if prev != nil {
prev.Next = new_node
}
if next != nil {
next.Prev = new_node
}
}
func (s *SAllocator) deleteNode(prev, curr *MemBlock) {
nxt := curr.Next
if prev != nil {
prev.Size += curr.Size
prev.Next = nxt
}
if nxt != nil {
nxt.Prev = prev
}
}
func (s *SAllocator) TearDown() {
s.head = &MemBlock{
Offset: 0,
Size: uint(len(s.arena)),
Free: true,
}
}
func main() {
sa := InitAllocator(1024)
offset, err := sa.Alloc(100)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Allocated at offset %d\n", offset)
err = sa.DeAlloc(uint(offset))
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Deallocated offset %d\n", offset)
sa.TearDown()
fmt.Println("Allocator reset")
}