From c93e82fa285edf0d5ed2447e0b32b9f6ce17f49e Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sat, 25 Apr 2026 03:34:30 -0400 Subject: Fix steal_line(), no more crashes. --- src/pool.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/pool.c') diff --git a/src/pool.c b/src/pool.c index 1417a02..bd1111d 100644 --- a/src/pool.c +++ b/src/pool.c @@ -1,6 +1,7 @@ #include -#include "pool.h" -#include "addrs.h" +#include "screen.h" +// #include "pool.h" +// #include "addrs.h" #include "memclear.h" screen_t screens[MAX_SCREENS]; @@ -19,6 +20,11 @@ void init_pools(void) { if((u16)OS.memlo < SCR_LOW_END - sizeof(pool_t)) add_to_pool(0, (u16)OS.memlo, SCR_LOW_END); + + /* TODO: for pool 0, add whatever RAM exists between the end + of the BSS and $3600. also, under-OS XL RAM if enabled. */ + + /* TODO: support pools 1 and up for other banks! */ } void add_to_pool(char p, u16 start, u16 end) { @@ -55,7 +61,7 @@ char get_smallest_pool(void) { } line_t *steal_line(char s) { - line_t *l; + line_t *l, *last_l; char candidate, victim; u16 maxheight; @@ -66,6 +72,9 @@ line_t *steal_line(char s) { /* only steal from the same pool */ if(screens[candidate].pool != screens[s].pool) continue; + /* only steal from screens that exist */ + if(screens[candidate].status == SCR_UNUSED) + continue; /* steal from the screen with the most lines */ if(screens[candidate].line_count > maxheight) { maxheight = screens[candidate].line_count; @@ -74,15 +83,20 @@ line_t *steal_line(char s) { } /* find 2nd to last line of the victim screen */ - for(l = screens[victim].line_list; l->next->next != (line_t *)END_MARKER; l = l->next) - ; + l = screens[victim].line_list; + while(l->next != (line_t *)END_MARKER) { + last_l = l; + l = l->next; + } + /* at the end of the loop, l is the last line before the + end marker (the line we're stealing), and last_l is the line + before that. */ - /* prepend last line to screen s */ - l->next->next = screens[s].line_list; - screens[s].line_list = l->next; - memclear(screens[s].line_list->data, LINE_LEN); + /* make old_l the last line */ + last_l->next = (line_t *)END_MARKER; - l->next = (line_t *)END_MARKER; + /* clear out old screen data, then return the stolen line */ + memclear(l->data, LINE_LEN); return l; } -- cgit v1.2.3