shithub: rgbds

Download patch

ref: 6e123ccc364b8bed9c38f884666271810c37578c
parent: f2724df5662ed204e41660907ac49ead75f79f34
author: Antonio Niño Díaz <[email protected]>
date: Sun Apr 23 21:39:13 EDT 2017

Optimize allocation of buffers for sections

Instead of allocating 0x4000 bytes for all sections and resize them as
needed, allocate 0x8000 bytes and don't let them to be resized. This is
the max possible size (ROM0 when ROMX sections aren't present).

Buffers are not needed for RAM sections, this patch changes the code so
that it only allocates buffers for ROM sections.

Signed-off-by: Antonio Niño Díaz <[email protected]>

--- a/include/asm/localasm.h
+++ b/include/asm/localasm.h
@@ -80,8 +80,6 @@
 
  */
 
-#define MAXSECTIONSIZE	0x4000
-
 #define	NAME_DB			"db"
 #define	NAME_DW			"dw"
 #define	NAME_RB			"rb"
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -17,7 +17,12 @@
 #include "asm/fstack.h"
 #include "extern/err.h"
 
-#define SECTIONCHUNK	0x4000
+/*
+ * Size allocated for code sections. This is the max size for any ROM section
+ * (which corresponds to a ROM0 section when the tiny flag is used in rgblink.
+ * RAM sections don't need to allocate any memory.
+ */
+#define MAXSECTIONSIZE	0x8000
 
 void out_SetCurrentSection(struct Section * pSect);
 
@@ -446,32 +451,20 @@
 	checksection();
 	if (pCurrentSection->nType != SECT_ROM0 &&
 	    pCurrentSection->nType != SECT_ROMX) {
-		errx(1, "Section '%s' cannot contain code or data (not a "
-		    "ROM0 or ROMX)", pCurrentSection->pzName);
+		errx(1, "Section '%s' cannot contain code or data (not a ROM0 or ROMX)",
+		     pCurrentSection->pzName);
 	}
 	if (pCurrentSection->nPC + size > MAXSECTIONSIZE) {
 		/*
-		 * N.B.: This check is not sufficient to ensure the section
-		 * will fit, because there can be multiple sections of this
-		 * type. The definitive check must be done at the linking
-		 * stage.
+		 * This check is here to trap broken code that generates
+		 * sections that are too big and to prevent the assembler from
+		 * generating huge object files. The size used to check is the
+		 * biggest possible section in a GB (a ROM0 section when ROMX
+		 * sections aren't used).
+		 * The real check must be done at the linking stage.
 		 */
-		errx(1, "Section '%s' is too big (old size %d + %d > %d)",
-		    pCurrentSection->pzName, pCurrentSection->nPC, size,
-		    MAXSECTIONSIZE);
+		errx(1, "Section '%s' is too big.", pCurrentSection->pzName);
 	}
-	if (((pCurrentSection->nPC % SECTIONCHUNK) >
-	    ((pCurrentSection->nPC + size) % SECTIONCHUNK)) &&
-	    (pCurrentSection->nType == SECT_ROM0 ||
-	    pCurrentSection->nType == SECT_ROMX)) {
-		pCurrentSection->tData = realloc(pCurrentSection->tData,
-		    ((pCurrentSection->nPC + size) / SECTIONCHUNK + 1) *
-		    SECTIONCHUNK);
-
-		if (pCurrentSection->tData == NULL) {
-			err(1, "Could not expand section");
-		}
-	}
 	return;
 }
 
@@ -580,10 +573,14 @@
 			pSect->charmap = NULL;
 			pPatchSymbols = NULL;
 
-			if ((pSect->tData = malloc(SECTIONCHUNK)) != NULL) {
-				return (pSect);
-			} else
-				fatalerror("Not enough memory for section");
+			pSect->tData = NULL;
+			if (secttype == SECT_ROM0 || secttype == SECT_ROMX) {
+				/* It is only needed to allocate memory for ROM
+				 * sections. */
+				if ((pSect->tData = malloc(MAXSECTIONSIZE)) == NULL)
+					fatalerror("Not enough memory for section");
+			}
+			return (pSect);
 		} else
 			fatalerror("Not enough memory for sectionname");
 	} else