From 76c659e6a36bf0ea1ef70bac78e60470d9ee6d4c Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Fri, 16 Jun 2023 15:51:45 +0000 Subject: [PATCH] fix: underflow --- src/galloc.🗿 | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/galloc.🗿 b/src/galloc.🗿 index 8221ef0..ba8cb1f 100644 --- a/src/galloc.🗿 +++ b/src/galloc.🗿 @@ -1,8 +1,8 @@ -define PADDING_SIZE = 4; -define HEADER_SIZE = 13; 🗿 PADDING_SIZE * 2 + 5 +define HEAP_SIZE = 0x020; +global heap[HEAP_SIZE] = 0; -define HEAP_SIZE = 65536; -global heap[65536] = 0; +define PADDING_SIZE = 4; +define HEADER_SIZE = 5; 🗿HEADER REPRESENTATION 🗿+-------------+--------+--------+---------------------------+-------------------------------+---------+---------+---------+ @@ -31,13 +31,13 @@ define LOCATION_USED = 1; define LOCATION_SIZE = 2; define LOCATION_NEXT = 3; define LOCATION_PREV = 4; -define LOCATION_DATA = 9; +define LOCATION_DATA = HEADER_SIZE + PADDING_SIZE; setup_header(ptr, used, size, next_block, prev_block) { local i; - if (size + PADDING_SIZE * 2 + HEADER_SIZE) + if (size + PADDING_SIZE * 2 + HEADER_SIZE > HEAP_SIZE) return (1); [ptr] = 1; 🗿 initialised [ptr + 1] = used; 🗿 used @@ -45,16 +45,15 @@ setup_header(ptr, used, size, next_block, prev_block) [ptr + 3] = next_block; 🗿 next block [ptr + 4] = prev_block; 🗿 previous block - i = header_size; + i = HEADER_SIZE; loop { - if (i == protection_size) + if (i == HEADER_SIZE + PADDING_SIZE) break; - [ptr + i] = 0; - [ptr + i + size] = 0; +🗿 [ptr + i + HEADER_SIZE] = 0; 🗿 INITIALISE TOP PADDING +🗿 [ptr + i + HEADER_SIZE + PADDING_SIZE + size] = 0; 🗿 INITIALISE BOT PADDING i++; } - return (0); } 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) { local old_next; local next; + local prev; if (size + HEADER_SIZE >= [ptr + LOCATION_SIZE]) return (0); old_next = [ptr + LOCATION_NEXT]; - next = [ptr + size + HEADER_SIZE]; - setup_header(next, 0, [ptr + LOCATION_SIZE] - HEADER_SIZE * 2, [ptr + LOCATION_SIZE] + HEADER_SIZE, old_next, ptr); - setup_header(ptr, 1, size, next, [ptr + LOCATION_PREV]); + 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); + setup_header(next, 0, next - ptr - HEADER_SIZE - PADDING_SIZE * 2, old_next, ptr); + bozo(); return (0); } -salloc(size) +galloc(size) { local ptr; - 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 + 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 ptr = find_next_space(size); if (ptr == 0) return (0);