Skip to content

Commit 8242702

Browse files
authored
Create pool_allocator.hcs
1 parent 02e1435 commit 8242702

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct Pool [
2+
slots: List<Int>,
3+
free_list: List<Int>
4+
]
5+
6+
fun pool_new() -> Pool [
7+
let p = Pool([], [])
8+
end p
9+
]
10+
11+
!! Zajmuje slot (z listy wolnych, jesli sa - w przeciwnym razie dokleja
12+
!! nowy) i zapisuje w nim `value`. Zwraca indeks slotu.
13+
fun pool_alloc(p: Pool, value: Int) -> Int [
14+
if p.free_list.len() > 0 [
15+
let idx = p.free_list.pop().unwrap()
16+
p.slots[idx] = value
17+
end idx
18+
]
19+
p.slots = p.slots + [value]
20+
end p.slots.len() - 1
21+
]
22+
23+
!! Zwalnia slot pod danym indeksem - trafia z powrotem do free_list i
24+
!! bedzie ponownie uzyty przy kolejnym `pool_alloc`.
25+
fun pool_free(p: Pool, idx: Int) [
26+
p.free_list = p.free_list + [idx]
27+
end
28+
]
29+
30+
fun pool_get(p: Pool, idx: Int) -> Int [
31+
end p.slots[idx]
32+
]

0 commit comments

Comments
 (0)