-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.odin
64 lines (56 loc) · 1.9 KB
/
test.odin
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
package block_allocator
import "core:fmt"
import "core:math/rand"
assert_allocator_layout_good :: proc(allocator: ^Block_Allocator) {
ok : bool
block := block_allocator_head(allocator)
for block.mem_next != BLOCK_UNUSED {
assert(block_is_used(block) || block_is_used(allocator.blocks[block.mem_next]))
assert(allocator.blocks[block.mem_next].offset == (block.offset + block.size))
assert(block.offset < block.offset + block.size)
block, _ = block_allocator_next(allocator, block)
}
}
main :: proc() {
ROUNDS_COUNT :: 1000
ALLOCS_COUNT :: 1000
ALLOC_MAX_SIZE :: 1024 * 1024 * 100
fmt.println("Beginning validation of allocator")
allocator := block_allocator_init(1024 * 1024 * 1024)
defer block_allocator_destroy(&allocator)
// Stack allocate an array to put allocation info into
allocs: [ALLOCS_COUNT]Block_Allocation
// Initialise the array with allocations
for i in 0 ..< ALLOCS_COUNT {
size := 256 * (1 + (rand.int31() % (ALLOC_MAX_SIZE / 256)))
alloc, ok := block_alloc(&allocator, u32(size))
assert_allocator_layout_good(&allocator)
if ok {
allocs[i] = alloc
} else {
allocs[i].metadata = BLOCK_UNUSED
}
}
// Repeatedly free and allocate half of the allocations
for r in 0 ..< ROUNDS_COUNT {
for i in 0 ..< ALLOCS_COUNT / 2 {
old_alloc := allocs[2 * i + (r % 2)]
if old_alloc.metadata == BLOCK_UNUSED {continue}
block_free(&allocator, old_alloc)
allocs[2 * i + (r % 2)].metadata = BLOCK_UNUSED
assert_allocator_layout_good(&allocator)
}
for i in 0 ..< ALLOCS_COUNT / 2 {
size := 256 * (1 + (rand.int31() % (ALLOC_MAX_SIZE / 256)))
new_alloc, ok := block_alloc(&allocator, u32(size))
assert_allocator_layout_good(&allocator)
if ok {
allocs[2 * i + (r % 2)] = new_alloc
} else {
allocs[2 * i + (r % 2)].metadata = BLOCK_UNUSED
}
}
}
assert_allocator_layout_good(&allocator)
fmt.println("Allocator integrity validated at all points")
}