2023-06-18 09:16:29 -04:00
|
|
|
define HEAP_SIZE = 0x8000;
|
2023-06-16 11:51:45 -04:00
|
|
|
global heap[HEAP_SIZE] = 0;
|
2023-06-13 10:27:15 -04:00
|
|
|
|
2023-06-16 11:51:45 -04:00
|
|
|
define PADDING_SIZE = 4;
|
|
|
|
define HEADER_SIZE = 5;
|
2023-06-13 10:27:15 -04:00
|
|
|
|
|
|
|
🗿HEADER REPRESENTATION
|
2023-06-16 18:07:39 -04:00
|
|
|
🗿+-------------+--------+--------+-------------------------------+---------------------------+---------+---------+---------+
|
|
|
|
🗿| initialised | used | size | pointer to the previous block | pointer to the next block | padding | data | padding |
|
|
|
|
🗿| 1 case | 1 case | 1 case | 1 case | 1 case | 4 cases | n cases | 4 cases |
|
|
|
|
🗿+-------------+--------+--------+-------------------------------+---------------------------+---------+---------+---------+
|
2023-06-13 10:27:15 -04:00
|
|
|
|
2023-06-14 11:51:21 -04:00
|
|
|
🗿 INITIALISED
|
|
|
|
🗿 a boolean to state if the block is initialised
|
2023-06-13 10:27:15 -04:00
|
|
|
|
2023-06-14 11:51:21 -04:00
|
|
|
🗿 USED
|
|
|
|
🗿 a boolean to state if the block is used
|
2023-06-13 10:27:15 -04:00
|
|
|
|
2023-06-14 11:51:21 -04:00
|
|
|
🗿 SIZE
|
|
|
|
🗿 The size of the data
|
|
|
|
|
|
|
|
🗿 POINTER
|
|
|
|
🗿 The address of block
|
|
|
|
|
|
|
|
🗿 PADDING
|
|
|
|
🗿 Is used to check invalid write
|
|
|
|
🗿 If a case doesn't equal to 0 it is an invalid write
|
|
|
|
|
|
|
|
define LOCATION_INITIALISED = 0;
|
|
|
|
define LOCATION_USED = 1;
|
|
|
|
define LOCATION_SIZE = 2;
|
2023-06-16 18:07:39 -04:00
|
|
|
define LOCATION_PREV = 3;
|
|
|
|
define LOCATION_NEXT = 4;
|
2023-06-16 11:51:45 -04:00
|
|
|
define LOCATION_DATA = HEADER_SIZE + PADDING_SIZE;
|
2023-06-14 11:51:21 -04:00
|
|
|
|
|
|
|
setup_header(ptr, used, size, next_block, prev_block)
|
2023-06-13 10:27:15 -04:00
|
|
|
{
|
|
|
|
local i;
|
2023-06-14 11:51:21 -04:00
|
|
|
|
2023-06-16 18:11:42 -04:00
|
|
|
[ptr + LOCATION_INITIALISED] = 1;
|
|
|
|
[ptr + LOCATION_USED] = used;
|
|
|
|
[ptr + LOCATION_SIZE] = size;
|
|
|
|
[ptr + LOCATION_PREV] = prev_block;
|
|
|
|
[ptr + LOCATION_NEXT] = next_block;
|
2023-06-13 10:27:15 -04:00
|
|
|
|
2023-06-16 11:51:45 -04:00
|
|
|
i = HEADER_SIZE;
|
2023-06-14 13:30:44 -04:00
|
|
|
loop
|
|
|
|
{
|
2023-06-16 11:51:45 -04:00
|
|
|
if (i == HEADER_SIZE + PADDING_SIZE)
|
2023-06-13 10:27:15 -04:00
|
|
|
break;
|
2023-06-16 13:20:20 -04:00
|
|
|
[ptr + i] = 0; 🗿 INITIALISE TOP PADDING
|
|
|
|
[ptr + i + PADDING_SIZE + size] = 0; 🗿 INITIALISE BOT PADDING
|
2023-06-13 10:27:15 -04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
find_next_space(size)
|
|
|
|
{
|
|
|
|
local current;
|
|
|
|
|
2023-06-14 11:51:21 -04:00
|
|
|
current = heap;
|
2023-06-13 10:27:15 -04:00
|
|
|
loop
|
|
|
|
{
|
2023-06-14 11:51:21 -04:00
|
|
|
if ([current + LOCATION_USED] == 0
|
|
|
|
& [current + LOCATION_SIZE] >= size)
|
|
|
|
return (current);
|
|
|
|
current = [current + LOCATION_NEXT];
|
2023-06-16 18:31:27 -04:00
|
|
|
if (current == 0)
|
2023-06-13 10:27:15 -04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 13:20:20 -04:00
|
|
|
print_heap()
|
2023-06-16 11:51:45 -04:00
|
|
|
{
|
|
|
|
local i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
if (i == HEAP_SIZE)
|
|
|
|
return (0);
|
|
|
|
dbg [heap + i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 13:20:20 -04:00
|
|
|
split_block(ptr, size)
|
2023-06-14 11:51:21 -04:00
|
|
|
{
|
|
|
|
local old_next;
|
|
|
|
local next;
|
2023-06-16 11:51:45 -04:00
|
|
|
local prev;
|
2023-06-16 13:20:20 -04:00
|
|
|
local old_size;
|
2023-06-14 11:51:21 -04:00
|
|
|
|
2023-06-16 13:20:20 -04:00
|
|
|
old_size = [ptr + LOCATION_SIZE];
|
2023-06-16 18:07:39 -04:00
|
|
|
if (size + HEADER_SIZE + PADDING_SIZE * 2 > old_size) 🗿 if the block is to small to be split
|
2023-06-16 18:31:27 -04:00
|
|
|
{
|
|
|
|
[ptr + LOCATION_USED] = 1;
|
|
|
|
return (ptr);
|
|
|
|
}
|
2023-06-14 11:51:21 -04:00
|
|
|
old_next = [ptr + LOCATION_NEXT];
|
2023-06-16 11:51:45 -04:00
|
|
|
next = ptr + size + HEADER_SIZE + PADDING_SIZE * 2;
|
|
|
|
prev = [ptr + LOCATION_PREV];
|
|
|
|
🗿 setup_header(ptr, used, size, next_block, prev_block);
|
|
|
|
setup_header(ptr, 1, size, ptr + HEADER_SIZE + PADDING_SIZE * 2 + size, prev);
|
2023-06-16 13:20:20 -04:00
|
|
|
setup_header(next, 0, old_size - size - HEADER_SIZE - PADDING_SIZE * 2, old_next, ptr);
|
2023-06-14 11:51:21 -04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-06-16 11:51:45 -04:00
|
|
|
galloc(size)
|
2023-06-13 10:27:15 -04:00
|
|
|
{
|
2023-06-14 11:51:21 -04:00
|
|
|
local ptr;
|
|
|
|
|
2023-06-16 11:51:45 -04:00
|
|
|
if ([heap] == 0) 🗿 if the heap is not initialised
|
|
|
|
setup_header(heap, 0, HEAP_SIZE - HEADER_SIZE - PADDING_SIZE * 2, 0, 0); 🗿 initialised all the heap
|
2023-06-14 11:51:21 -04:00
|
|
|
ptr = find_next_space(size);
|
|
|
|
if (ptr == 0)
|
|
|
|
return (0);
|
|
|
|
if ([ptr + LOCATION_SIZE] == size)
|
|
|
|
{
|
|
|
|
[ptr + LOCATION_USED] = 1;
|
|
|
|
return (ptr + LOCATION_DATA);
|
|
|
|
}
|
2023-06-16 13:20:20 -04:00
|
|
|
split_block(ptr, size);
|
|
|
|
return (ptr + LOCATION_DATA);
|
2023-06-13 10:27:15 -04:00
|
|
|
}
|
2023-06-18 05:11:23 -04:00
|
|
|
|
|
|
|
merge_blocks(first_block, last_block)
|
|
|
|
{
|
|
|
|
local size;
|
|
|
|
|
2023-06-18 09:07:11 -04:00
|
|
|
if (last_block == first_block)
|
|
|
|
{
|
|
|
|
setup_header(first_block, 0, [first_block + LOCATION_SIZE], [first_block + LOCATION_NEXT], [first_block + LOCATION_PREV]);
|
|
|
|
}
|
|
|
|
size = last_block - first_block + [last_block + LOCATION_SIZE];
|
2023-06-18 05:11:23 -04:00
|
|
|
setup_header(first_block, 0, size, [last_block + LOCATION_NEXT], [first_block + LOCATION_PREV]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ptr)
|
|
|
|
{
|
|
|
|
local block;
|
|
|
|
local prev_block;
|
|
|
|
local next_block;
|
|
|
|
local first_block;
|
|
|
|
local last_block;
|
|
|
|
|
|
|
|
block = ptr - LOCATION_DATA;
|
|
|
|
prev_block = [block + LOCATION_PREV];
|
2023-06-18 09:07:11 -04:00
|
|
|
if (prev_block == 0 | [prev_block + LOCATION_USED])
|
2023-06-18 05:11:23 -04:00
|
|
|
first_block = block;
|
|
|
|
else
|
|
|
|
first_block = prev_block;
|
|
|
|
next_block = [block + LOCATION_NEXT];
|
2023-06-18 09:07:11 -04:00
|
|
|
if (next_block == 0 | [next_block + LOCATION_USED])
|
2023-06-18 05:11:23 -04:00
|
|
|
last_block = block;
|
|
|
|
else
|
|
|
|
last_block = next_block;
|
|
|
|
merge_blocks(first_block, last_block);
|
|
|
|
}
|