From 949b03ad96336f4658eb0050f58abd517dd79c79 Mon Sep 17 00:00:00 2001
From: Robert Sprowson <rsprowson@gitlab.riscosopen.org>
Date: Sat, 14 Mar 2020 11:57:40 +0000
Subject: [PATCH] Special case extension of aligned RMA claims

During ModHandReason_ClaimAligned care is taken to start on a multiple of 32 and end on a multiple of 32 + 16 (such that the next block in the heap would be at +20 same as plain ModHandReason_Claim would do). However, during a ModHandReason_ExtendBlock there's a possibility that those criteria can't be retained, due to having to move to a larger gap elsewhere, leaving the end at an inconvenient +36.
Similar allocation functions in other OS' don't guarantee to return an extended block with the same alignment, so take the position that for extends we always allocate a fresh block, copy the contents, and free the original. Shrinks are left in place.
Tested with a random allocator which juggles 16 aligned allocations 50,000 times.

Version 6.36. Tagged as 'Kernel-6_36'
---
 VersionASM | 12 ++++++------
 VersionNum | 20 ++++++++++----------
 s/ModHand  | 32 ++++++++++++++++++++++++++++++--
 3 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/VersionASM b/VersionASM
index 9f99a48..b6adce6 100644
--- a/VersionASM
+++ b/VersionASM
@@ -9,12 +9,12 @@
                         GBLS    Module_ApplicationDate
                         GBLS    Module_HelpVersion
                         GBLS    Module_ComponentName
-Module_MajorVersion     SETS    "6.35"
-Module_Version          SETA    635
+Module_MajorVersion     SETS    "6.36"
+Module_Version          SETA    636
 Module_MinorVersion     SETS    ""
-Module_Date             SETS    "02 Mar 2020"
-Module_ApplicationDate  SETS    "02-Mar-20"
+Module_Date             SETS    "18 Mar 2020"
+Module_ApplicationDate  SETS    "18-Mar-20"
 Module_ComponentName    SETS    "Kernel"
-Module_FullVersion      SETS    "6.35"
-Module_HelpVersion      SETS    "6.35 (02 Mar 2020)"
+Module_FullVersion      SETS    "6.36"
+Module_HelpVersion      SETS    "6.36 (18 Mar 2020)"
                         END
diff --git a/VersionNum b/VersionNum
index 7317692..abcbea7 100644
--- a/VersionNum
+++ b/VersionNum
@@ -1,21 +1,21 @@
-/* (6.35)
+/* (6.36)
  *
  * This file is automatically maintained by srccommit, do not edit manually.
  *
  */
-#define Module_MajorVersion_CMHG        6.35
+#define Module_MajorVersion_CMHG        6.36
 #define Module_MinorVersion_CMHG        
-#define Module_Date_CMHG                02 Mar 2020
+#define Module_Date_CMHG                18 Mar 2020
 
-#define Module_MajorVersion             "6.35"
-#define Module_Version                  635
+#define Module_MajorVersion             "6.36"
+#define Module_Version                  636
 #define Module_MinorVersion             ""
-#define Module_Date                     "02 Mar 2020"
+#define Module_Date                     "18 Mar 2020"
 
-#define Module_ApplicationDate          "02-Mar-20"
+#define Module_ApplicationDate          "18-Mar-20"
 
 #define Module_ComponentName            "Kernel"
 
-#define Module_FullVersion              "6.35"
-#define Module_HelpVersion              "6.35 (02 Mar 2020)"
-#define Module_LibraryVersionInfo       "6:35"
+#define Module_FullVersion              "6.36"
+#define Module_HelpVersion              "6.36 (18 Mar 2020)"
+#define Module_LibraryVersionInfo       "6:36"
diff --git a/s/ModHand b/s/ModHand
index 5b25590..61ddbb1 100644
--- a/s/ModHand
+++ b/s/ModHand
@@ -1151,14 +1151,42 @@ Module_ExtendBlock ROUT
          Push   "R0, r1, R3, lr"
 
          ADD     R3, R3, #31
-         BIC     R3, R3, #31
+         BICS    R3, R3, #31
+         BMI     %FT10                       ; shrinks don't move the block
 
+         TST     R2, #31                     ; starts at 32*n
+         BEQ     %FT30                       ; => grow an aligned block
+10
          MOV     R0, #HeapReason_ExtendBlock
          BL      DoRMAHeapOpWithExtension
-
+20
          STRVS   R0, [stack]
          Pull   "R0, r1, R3, lr"
          B       SLVK_TestV
+30
+         LDR     R0, [R2, #-4]               ; grow may cause move, so always move rather than lose the carefully
+         SUB     R0, R0, #4                  ; arranged next block alignment from RMAClaim_ChunkAligned
+         Push   "R0, R2"
+         ADD     R3, R0, R3
+         BL      RMAClaim_Chunk
+         ADDVS   SP, SP, #4*2
+         BVS     %BT20
+
+         Pull   "R0, R3"                     ; size, old block
+         Push   "R2"                         ; new block
+40
+         SUBS    R0, R0, #4
+         LDRPL   LR, [R3, R0]
+         STRPL   LR, [R2, R0]
+         BPL     %BT40
+
+         MOV     R0, #HeapReason_Free
+         MOV     R1, #RMAAddress
+         MOV     R2, R3
+         SWI     XOS_Heap                    ; free old
+
+         Pull   "R2"                         ; return new
+         B       %BT20
 
 ;*************************************************************
 ; New Incarnation
-- 
GitLab