Use std::deque for section patches

This commit is contained in:
Rangi42
2024-02-21 11:06:36 -05:00
committed by Sylvie
parent b87ee62e6c
commit 8083ef605f
3 changed files with 14 additions and 28 deletions

View File

@@ -3,6 +3,7 @@
#ifndef RGBDS_SECTION_H #ifndef RGBDS_SECTION_H
#define RGBDS_SECTION_H #define RGBDS_SECTION_H
#include <deque>
#include <stdint.h> #include <stdint.h>
#include "linkdefs.hpp" #include "linkdefs.hpp"
@@ -23,7 +24,7 @@ struct Section {
uint32_t bank; uint32_t bank;
uint8_t align; // Exactly as specified in `ALIGN[]` uint8_t align; // Exactly as specified in `ALIGN[]`
uint16_t alignOfs; uint16_t alignOfs;
struct Patch *patches; std::deque<struct Patch *> *patches;
uint8_t *data; uint8_t *data;
}; };

View File

@@ -35,7 +35,6 @@ struct Patch {
uint8_t type; uint8_t type;
uint32_t rpnSize; uint32_t rpnSize;
uint8_t *rpn; uint8_t *rpn;
struct Patch *next;
}; };
struct Assertion { struct Assertion {
@@ -55,18 +54,6 @@ static std::deque<struct Assertion *> assertions;
static struct FileStackNode *fileStackNodes = NULL; 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) // Write a long to a file (little-endian)
static void putlong(uint32_t i, FILE *f) 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)) { if (sect_HasData(sect->type)) {
fwrite(sect->data, 1, sect->size, f); 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; for (struct Patch const *patch : *sect->patches)
patch = patch->next)
writepatch(patch, f); writepatch(patch, f);
} }
} }
@@ -187,15 +173,11 @@ static void writesection(struct Section const *sect, FILE *f)
static void freesection(struct Section const *sect) static void freesection(struct Section const *sect)
{ {
if (sect_HasData(sect->type)) { if (sect_HasData(sect->type)) {
struct Patch *patch = sect->patches; for (struct Patch *patch : *sect->patches) {
while (patch != NULL) {
struct Patch *next = patch->next;
free(patch->rpn); free(patch->rpn);
free(patch); 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) static struct Patch *allocpatch(uint32_t type, struct Expression const *expr, uint32_t ofs)
{ {
struct Patch *patch = (struct Patch *)malloc(sizeof(*patch)); 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->type = type;
patch->src = node; patch->src = node;
// All patches are assumed to eventually be written, so the file stack node is registered
out_RegisterNode(node); out_RegisterNode(node);
patch->lineNo = lexer_GetLineNo(); patch->lineNo = lexer_GetLineNo();
patch->offset = ofs; patch->offset = ofs;
@@ -379,8 +360,8 @@ void out_CreatePatch(uint32_t type, struct Expression const *expr, uint32_t ofs,
// before those bytes. // before those bytes.
patch->pcOffset -= pcShift; patch->pcOffset -= pcShift;
patch->next = currentSection->patches; // Add the patch to the list
currentSection->patches = patch; currentSection->patches->push_front(patch);
} }
// Creates an assert that will be written to the object file // Creates an assert that will be written to the object file

View File

@@ -2,8 +2,10 @@
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <new>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
@@ -283,7 +285,9 @@ static struct Section *createSection(char const *name, enum SectionType type,
sect->bank = bank; sect->bank = bank;
sect->align = alignment; sect->align = alignment;
sect->alignOfs = alignOffset; sect->alignOfs = alignOffset;
sect->patches = NULL; sect->patches = new(std::nothrow) std::deque<struct Patch *>();
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. // It is only needed to allocate memory for ROM sections.
if (sect_HasData(type)) { if (sect_HasData(type)) {