diff --git a/include/asm/section.hpp b/include/asm/section.hpp index b841e194..8b7d7d4f 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -3,6 +3,7 @@ #ifndef RGBDS_SECTION_H #define RGBDS_SECTION_H +#include #include #include "linkdefs.hpp" @@ -23,7 +24,7 @@ struct Section { uint32_t bank; uint8_t align; // Exactly as specified in `ALIGN[]` uint16_t alignOfs; - struct Patch *patches; + std::deque *patches; uint8_t *data; }; diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 35ac193f..0340b0b9 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -35,7 +35,6 @@ struct Patch { uint8_t type; uint32_t rpnSize; uint8_t *rpn; - struct Patch *next; }; struct Assertion { @@ -55,18 +54,6 @@ static std::deque assertions; static struct FileStackNode *fileStackNodes = NULL; -// Count the number of patches used in this object -static uint32_t countPatches(struct Section const *sect) -{ - uint32_t r = 0; - - for (struct Patch const *patch = sect->patches; patch != NULL; - patch = patch->next) - r++; - - return r; -} - // Write a long to a file (little-endian) static void putlong(uint32_t i, FILE *f) { @@ -176,10 +163,9 @@ static void writesection(struct Section const *sect, FILE *f) if (sect_HasData(sect->type)) { fwrite(sect->data, 1, sect->size, f); - putlong(countPatches(sect), f); + putlong(sect->patches->size(), f); - for (struct Patch const *patch = sect->patches; patch != NULL; - patch = patch->next) + for (struct Patch const *patch : *sect->patches) writepatch(patch, f); } } @@ -187,15 +173,11 @@ static void writesection(struct Section const *sect, FILE *f) static void freesection(struct Section const *sect) { if (sect_HasData(sect->type)) { - struct Patch *patch = sect->patches; - - while (patch != NULL) { - struct Patch *next = patch->next; - + for (struct Patch *patch : *sect->patches) { free(patch->rpn); free(patch); - patch = next; } + delete sect->patches; } } @@ -328,8 +310,6 @@ static void writerpn(uint8_t *rpnexpr, uint32_t *rpnptr, const uint8_t *rpn, } } -// Allocate a new patch structure and link it into the list -// WARNING: all patches are assumed to eventually be written, so the file stack node is registered static struct Patch *allocpatch(uint32_t type, struct Expression const *expr, uint32_t ofs) { struct Patch *patch = (struct Patch *)malloc(sizeof(*patch)); @@ -345,6 +325,7 @@ static struct Patch *allocpatch(uint32_t type, struct Expression const *expr, ui patch->type = type; patch->src = node; + // All patches are assumed to eventually be written, so the file stack node is registered out_RegisterNode(node); patch->lineNo = lexer_GetLineNo(); patch->offset = ofs; @@ -379,8 +360,8 @@ void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs, // before those bytes. patch->pcOffset -= pcShift; - patch->next = currentSection->patches; - currentSection->patches = patch; + // Add the patch to the list + currentSection->patches->push_front(patch); } // Creates an assert that will be written to the object file diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 6858021d..7ecb61d5 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -2,8 +2,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -283,7 +285,9 @@ static struct Section *createSection(char const *name, enum SectionType type, sect->bank = bank; sect->align = alignment; sect->alignOfs = alignOffset; - sect->patches = NULL; + sect->patches = new(std::nothrow) std::deque(); + if (sect->patches == NULL) + fatalerror("Not enough memory for section patches: %s\n", strerror(errno)); // It is only needed to allocate memory for ROM sections. if (sect_HasData(type)) {