From a26e07c5531b6e153876ef096176f9e3d292b241 Mon Sep 17 00:00:00 2001
From: Robert Sprowson <rsprowson@gitlab.riscosopen.org>
Date: Sat, 29 Apr 2023 21:41:22 +0100
Subject: [PATCH] [580] Fix for excessive RMA jumps when *IconSprites on RISC
 OS 3.10

Pre Medusa sprites are held in the RMA. Wimp 5.26 adjusted the RMA behaviour
where previously after *IconSprites a resize of -256MB would surprise apps
that wanted some RMA for things in their !Boot or !Run files but found there
was none due to having looked at their sprites.

However, the resize of -256MB does not always result in 0kB free space, so
each *IconSprites command ended up net growing the RMA by whatever the shrink
left *plus* the previous free amount.

Fix this by reading the free space in the RMA and adjusting up or down as
needed to always end up with a safe free space position.
---
 s/Wimp01 | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/s/Wimp01 b/s/Wimp01
index 26f7b3b..bdc7d2b 100644
--- a/s/Wimp01
+++ b/s/Wimp01
@@ -3025,16 +3025,9 @@ greys_16
 ; tries "<filename>23" if hi-res mono, and/or "<filename>22" if 2x2 OS units
 
 IconSprites_Code
-        Push    "R7-R9,LR"
+        Push    "R7-R8,LR"
         LDR     wsptr,[R12]
 ;
-      [ :LNOT: Medusa
-        Push    "R0"
-        MOV     R0,#ModHandReason_RMADesc
-        SWI     XOS_Module
-        MOV     R9,R3                   ; how much free RMA there was before merging
-        Pull    "R0"
-      ]
         MOV     R8,R0                   ; R8 -> original filename
         MOV     R5,R0                   ; R5 = position at which to insert system variable
 01      LDRB    LR,[R0],#1              ; skip leading space
@@ -3159,16 +3152,16 @@ IconSprites_Code
  ]
 
       [ :LNOT: Medusa                   ; Medusa uses DA
-        MOV     R0,#1                   ; The merge might have bumped up the RMA while both sets of sprites
-        MOV     R1,#-&10000000          ; were loaded. Try and shrink RMA.
-        SWI     XOS_ChangeDynamicArea   ; -256M will give "Unable to move memory"
-        MOV     R0,#1
-        MOV     R1,R9                   ; Put back the same amount of free space as on entry
-        SWI     XOS_ChangeDynamicArea   ; May fail if APPSPACE is in use, that's ok.
+        MOV     R0,#ModHandReason_RMADesc
+        SWI     XOS_Module              ; The merge might have bumped up the RMA while both sets of sprites
+        RSBS    R1,R3,#128*1024         ; were loaded. Resize the RMA, leaving a cushion of 128k. 
+        MOVNE   R0,#1
+        SWINE   XOS_ChangeDynamicArea   ; May fail if APPSPACE is in use, that's ok.
+90
         CLRV
       ]
 95
-        Pull    "R7-R9,PC"
+        Pull    "R7-R8,PC"
 
 IconThemeSysVar = "<Wimp$IconTheme>"
 IconThemeSysVarLen * . - IconThemeSysVar
-- 
GitLab