fix: underflow

This commit is contained in:
Camille Chauvet 2023-06-16 15:51:45 +00:00
parent 6ff71db96a
commit 76c659e6a3

View File

@ -1,8 +1,8 @@
define PADDING_SIZE = 4; define HEAP_SIZE = 0x020;
define HEADER_SIZE = 13; 🗿 PADDING_SIZE * 2 + 5 global heap[HEAP_SIZE] = 0;
define HEAP_SIZE = 65536; define PADDING_SIZE = 4;
global heap[65536] = 0; define HEADER_SIZE = 5;
🗿HEADER REPRESENTATION 🗿HEADER REPRESENTATION
🗿+-------------+--------+--------+---------------------------+-------------------------------+---------+---------+---------+ 🗿+-------------+--------+--------+---------------------------+-------------------------------+---------+---------+---------+
@ -31,13 +31,13 @@ define LOCATION_USED = 1;
define LOCATION_SIZE = 2; define LOCATION_SIZE = 2;
define LOCATION_NEXT = 3; define LOCATION_NEXT = 3;
define LOCATION_PREV = 4; define LOCATION_PREV = 4;
define LOCATION_DATA = 9; define LOCATION_DATA = HEADER_SIZE + PADDING_SIZE;
setup_header(ptr, used, size, next_block, prev_block) setup_header(ptr, used, size, next_block, prev_block)
{ {
local i; local i;
if (size + PADDING_SIZE * 2 + HEADER_SIZE) if (size + PADDING_SIZE * 2 + HEADER_SIZE > HEAP_SIZE)
return (1); return (1);
[ptr] = 1; 🗿 initialised [ptr] = 1; 🗿 initialised
[ptr + 1] = used; 🗿 used [ptr + 1] = used; 🗿 used
@ -45,16 +45,15 @@ setup_header(ptr, used, size, next_block, prev_block)
[ptr + 3] = next_block; 🗿 next block [ptr + 3] = next_block; 🗿 next block
[ptr + 4] = prev_block; 🗿 previous block [ptr + 4] = prev_block; 🗿 previous block
i = header_size; i = HEADER_SIZE;
loop loop
{ {
if (i == protection_size) if (i == HEADER_SIZE + PADDING_SIZE)
break; break;
[ptr + i] = 0; 🗿 [ptr + i + HEADER_SIZE] = 0; 🗿 INITIALISE TOP PADDING
[ptr + i + size] = 0; 🗿 [ptr + i + HEADER_SIZE + PADDING_SIZE + size] = 0; 🗿 INITIALISE BOT PADDING
i++; i++;
} }
return (0);
} }
find_next_space(size) find_next_space(size)
@ -73,26 +72,44 @@ find_next_space(size)
} }
} }
bozo()
{
local i;
i = 0;
loop
{
if (i == HEAP_SIZE)
return (0);
dbg [heap + i];
i++;
}
}
divise_block(ptr, size) divise_block(ptr, size)
{ {
local old_next; local old_next;
local next; local next;
local prev;
if (size + HEADER_SIZE >= [ptr + LOCATION_SIZE]) if (size + HEADER_SIZE >= [ptr + LOCATION_SIZE])
return (0); return (0);
old_next = [ptr + LOCATION_NEXT]; old_next = [ptr + LOCATION_NEXT];
next = [ptr + size + HEADER_SIZE]; next = ptr + size + HEADER_SIZE + PADDING_SIZE * 2;
setup_header(next, 0, [ptr + LOCATION_SIZE] - HEADER_SIZE * 2, [ptr + LOCATION_SIZE] + HEADER_SIZE, old_next, ptr); prev = [ptr + LOCATION_PREV];
setup_header(ptr, 1, size, next, [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);
setup_header(next, 0, next - ptr - HEADER_SIZE - PADDING_SIZE * 2, old_next, ptr);
bozo();
return (0); return (0);
} }
salloc(size) galloc(size)
{ {
local ptr; local ptr;
if ([heap] == 0) 🗿 if the heap is not initialised if ([heap] == 0) 🗿 if the heap is not initialised
setup_header(HEAP_SIZE - 2 * PADDING_SIZE - HEADER_SIZE, 0, 0, 0); 🗿 initialised all the heap setup_header(heap, 0, HEAP_SIZE - HEADER_SIZE - PADDING_SIZE * 2, 0, 0); 🗿 initialised all the heap
ptr = find_next_space(size); ptr = find_next_space(size);
if (ptr == 0) if (ptr == 0)
return (0); return (0);