shithub: rgbds

Download patch

ref: 76d88629009d98292890804161ecb7e455f0751f
parent: 1dafc1c762b4ea50030272775e0ca217fe93c0c0
author: Rangi <[email protected]>
date: Sun Feb 28 11:12:03 EST 2021

Refactor part of getSection into createSection

getSection validates its parameters and calls
either mergeSections or createSection.

--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -245,8 +245,48 @@
 #undef fail
 
 /*
- * Find a section by name and type. If it doesn't exist, create it
+ * Create a new section, not yet in the list.
  */
+static struct Section *createSection(char const *name, enum SectionType type,
+				     uint32_t org, uint32_t bank, uint8_t alignment,
+				     uint16_t alignOffset, enum SectionModifier mod)
+{
+	struct Section *sect = malloc(sizeof(*sect));
+
+	if (sect == NULL)
+		fatalerror("Not enough memory for section: %s\n", strerror(errno));
+
+	sect->name = strdup(name);
+	if (sect->name == NULL)
+		fatalerror("Not enough memory for section name: %s\n", strerror(errno));
+
+	sect->type = type;
+	sect->modifier = mod;
+	sect->src = fstk_GetFileStack();
+	sect->fileLine = lexer_GetLineNo();
+	sect->size = 0;
+	sect->org = org;
+	sect->bank = bank;
+	sect->align = alignment;
+	sect->alignOfs = alignOffset;
+	sect->next = NULL;
+	sect->patches = NULL;
+
+	/* It is only needed to allocate memory for ROM sections. */
+	if (sect_HasData(type)) {
+		sect->data = malloc(maxsize[type]);
+		if (sect->data == NULL)
+			fatalerror("Not enough memory for section: %s\n", strerror(errno));
+	} else {
+		sect->data = NULL;
+	}
+
+	return sect;
+}
+
+/*
+ * Find a section by name and type. If it doesn't exist, create it.
+ */
 static struct Section *getSection(char const *name, enum SectionType type, uint32_t org,
 				  struct SectionSpec const *attrs, enum SectionModifier mod)
 {
@@ -313,43 +353,12 @@
 
 	if (sect) {
 		mergeSections(sect, type, org, bank, alignment, alignOffset, mod);
-		return sect;
-	}
-
-	sect = malloc(sizeof(*sect));
-	if (sect == NULL)
-		fatalerror("Not enough memory for section: %s\n", strerror(errno));
-
-	sect->name = strdup(name);
-	if (sect->name == NULL)
-		fatalerror("Not enough memory for section name: %s\n", strerror(errno));
-
-	sect->type = type;
-	sect->modifier = mod;
-	sect->src = fstk_GetFileStack();
-	sect->fileLine = lexer_GetLineNo();
-	sect->size = 0;
-	sect->org = org;
-	sect->bank = bank;
-	sect->align = alignment;
-	sect->alignOfs = alignOffset;
-	sect->patches = NULL;
-
-	/* It is only needed to allocate memory for ROM sections. */
-	if (sect_HasData(type)) {
-		uint32_t sectsize;
-
-		sectsize = maxsize[type];
-		sect->data = malloc(sectsize);
-		if (sect->data == NULL)
-			fatalerror("Not enough memory for section: %s\n", strerror(errno));
 	} else {
-		sect->data = NULL;
+		sect = createSection(name, type, org, bank, alignment, alignOffset, mod);
+		// Add the new section to the list (order doesn't matter)
+		sect->next = pSectionList;
+		pSectionList = sect;
 	}
-
-	// Add the new section to the list (order doesn't matter)
-	sect->next = pSectionList;
-	pSectionList = sect;
 
 	return sect;
 }