mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use automatic allocation for sections
This commit is contained in:
@@ -12,7 +12,7 @@ struct Expression;
|
||||
struct FileStackNode;
|
||||
|
||||
extern const char *objectName;
|
||||
extern std::deque<struct Section *> sectionList;
|
||||
extern std::deque<struct Section> sectionList;
|
||||
|
||||
void out_RegisterNode(struct FileStackNode *node);
|
||||
void out_ReplaceNode(struct FileStackNode *node);
|
||||
|
||||
@@ -46,7 +46,7 @@ struct Assertion {
|
||||
|
||||
const char *objectName;
|
||||
|
||||
std::deque<struct Section *> sectionList;
|
||||
std::deque<struct Section> sectionList;
|
||||
|
||||
// List of symbols to put in the object file
|
||||
static std::vector<struct Symbol *> objectSymbols;
|
||||
@@ -102,20 +102,18 @@ This is code intended to replace a node, which is pretty useless until ref count
|
||||
#endif
|
||||
}
|
||||
|
||||
// Return a section's ID
|
||||
static uint32_t getsectid(struct Section const *sect)
|
||||
// Return a section's ID, or -1 if the section is not in the list
|
||||
static uint32_t getSectIDIfAny(struct Section *sect)
|
||||
{
|
||||
auto search = std::find(RANGE(sectionList), sect);
|
||||
if (!sect)
|
||||
return (uint32_t)-1;
|
||||
|
||||
for (auto it = sectionList.begin(); it != sectionList.end(); it++) {
|
||||
if (&*it == sect)
|
||||
return it - sectionList.begin();
|
||||
}
|
||||
|
||||
if (search == sectionList.end())
|
||||
fatalerror("Unknown section '%s'\n", sect->name);
|
||||
|
||||
return search - sectionList.begin();
|
||||
}
|
||||
|
||||
static uint32_t getSectIDIfAny(struct Section const *sect)
|
||||
{
|
||||
return sect ? getsectid(sect) : (uint32_t)-1;
|
||||
}
|
||||
|
||||
// Write a patch to a file
|
||||
@@ -133,39 +131,39 @@ static void writepatch(struct Patch const *patch, FILE *f)
|
||||
}
|
||||
|
||||
// Write a section to a file
|
||||
static void writesection(struct Section const *sect, FILE *f)
|
||||
static void writesection(struct Section const §, FILE *f)
|
||||
{
|
||||
putstring(sect->name, f);
|
||||
putstring(sect.name, f);
|
||||
|
||||
putlong(sect->size, f);
|
||||
putlong(sect.size, f);
|
||||
|
||||
bool isUnion = sect->modifier == SECTION_UNION;
|
||||
bool isFragment = sect->modifier == SECTION_FRAGMENT;
|
||||
bool isUnion = sect.modifier == SECTION_UNION;
|
||||
bool isFragment = sect.modifier == SECTION_FRAGMENT;
|
||||
|
||||
putc(sect->type | isUnion << 7 | isFragment << 6, f);
|
||||
putc(sect.type | isUnion << 7 | isFragment << 6, f);
|
||||
|
||||
putlong(sect->org, f);
|
||||
putlong(sect->bank, f);
|
||||
putc(sect->align, f);
|
||||
putlong(sect->alignOfs, f);
|
||||
putlong(sect.org, f);
|
||||
putlong(sect.bank, f);
|
||||
putc(sect.align, f);
|
||||
putlong(sect.alignOfs, f);
|
||||
|
||||
if (sect_HasData(sect->type)) {
|
||||
fwrite(sect->data, 1, sect->size, f);
|
||||
putlong(sect->patches->size(), f);
|
||||
if (sect_HasData(sect.type)) {
|
||||
fwrite(sect.data, 1, sect.size, f);
|
||||
putlong(sect.patches->size(), f);
|
||||
|
||||
for (struct Patch const *patch : *sect->patches)
|
||||
for (struct Patch const *patch : *sect.patches)
|
||||
writepatch(patch, f);
|
||||
}
|
||||
}
|
||||
|
||||
static void freesection(struct Section const *sect)
|
||||
static void freesection(struct Section const §)
|
||||
{
|
||||
if (sect_HasData(sect->type)) {
|
||||
for (struct Patch *patch : *sect->patches) {
|
||||
if (sect_HasData(sect.type)) {
|
||||
for (struct Patch *patch : *sect.patches) {
|
||||
free(patch->rpn);
|
||||
free(patch);
|
||||
}
|
||||
delete sect->patches;
|
||||
delete sect.patches;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -439,7 +437,7 @@ void out_WriteObject(void)
|
||||
for (struct Symbol const *sym : objectSymbols)
|
||||
writesymbol(sym, f);
|
||||
|
||||
for (struct Section *sect : sectionList) {
|
||||
for (struct Section § : sectionList) {
|
||||
writesection(sect, f);
|
||||
freesection(sect);
|
||||
}
|
||||
|
||||
@@ -110,9 +110,9 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size)
|
||||
|
||||
struct Section *sect_FindSectionByName(char const *name)
|
||||
{
|
||||
for (struct Section *sect : sectionList) {
|
||||
if (strcmp(name, sect->name) == 0)
|
||||
return sect;
|
||||
for (struct Section § : sectionList) {
|
||||
if (strcmp(name, sect.name) == 0)
|
||||
return §
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -267,38 +267,36 @@ 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 = (struct Section *)malloc(sizeof(*sect));
|
||||
// Add the new section to the list (order doesn't matter)
|
||||
struct Section § = sectionList.emplace_front();
|
||||
|
||||
if (sect == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
|
||||
sect->name = strdup(name);
|
||||
if (sect->name == NULL)
|
||||
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 = new(std::nothrow) std::deque<struct Patch *>();
|
||||
if (sect->patches == NULL)
|
||||
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 = 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.
|
||||
if (sect_HasData(type)) {
|
||||
sect->data = (uint8_t *)malloc(sectionTypeInfo[type].size);
|
||||
if (sect->data == NULL)
|
||||
sect.data = (uint8_t *)malloc(sectionTypeInfo[type].size);
|
||||
if (sect.data == NULL)
|
||||
fatalerror("Not enough memory for section: %s\n", strerror(errno));
|
||||
} else {
|
||||
sect->data = NULL;
|
||||
sect.data = NULL;
|
||||
}
|
||||
|
||||
return sect;
|
||||
return §
|
||||
}
|
||||
|
||||
// Find a section by name and type. If it doesn't exist, create it.
|
||||
@@ -371,8 +369,6 @@ static struct Section *getSection(char const *name, enum SectionType type, uint3
|
||||
mergeSections(sect, type, org, bank, alignment, alignOffset, mod);
|
||||
} else {
|
||||
sect = createSection(name, type, org, bank, alignment, alignOffset, mod);
|
||||
// Add the new section to the list (order doesn't matter)
|
||||
sectionList.push_front(sect);
|
||||
}
|
||||
|
||||
return sect;
|
||||
|
||||
Reference in New Issue
Block a user