Assume arena is of 1024B and each chunk is 8B
So heap alloc chunks of 128 bools to represent whether a chunk is being used or not
Allocate 22B and 6B
round up 22 to multiple of 8, 24B
round up 6 to multiple of 8, 6B
| |- | 24B | -| | 8B | |||
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 | ... | 0 |
Reallocate 22B to 32B
32B takes 4 chunks but next chunk is already in use so find 4 chunks that are not in use aka false flagged, copy the old data to new one, and mark old chunks are free/ false and return new memory location = chunks * chunk_size + arena memory pool
| |- | 24B | -| | 8B | |- | - | 32B | -| | |||
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | ... | 0 |
Reallocate 32B to 16B
since 16B only takes 2 chunks, mark other 2 chunks as free
and return the same pointer
| 8B | |- | 16B | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | ... | 0 |
Free memory 16B
mark 2 chunks of 16B as false
| 8B | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | ... | 0 |
Top comments (0)