Use a std::unordered_map for looking up sections by name (#1357)

This commit is contained in:
Sylvie
2024-03-13 19:45:52 -04:00
committed by GitHub
parent 8ec0d01fc4
commit eb99fc8681
3 changed files with 13 additions and 14 deletions

View File

@@ -5,6 +5,8 @@
#include <deque> #include <deque>
#include <stdint.h> #include <stdint.h>
#include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "linkdefs.hpp" #include "linkdefs.hpp"
@@ -49,6 +51,7 @@ struct SectionSpec {
}; };
extern std::deque<Section> sectionList; extern std::deque<Section> sectionList;
extern std::unordered_map<std::string, size_t> sectionMap; // Indexes into `sectionList`
extern Section *currentSection; extern Section *currentSection;
Section *sect_FindSectionByName(char const *name); Section *sect_FindSectionByName(char const *name);

View File

@@ -83,10 +83,8 @@ static uint32_t getSectIDIfAny(Section *sect) {
if (!sect) if (!sect)
return (uint32_t)-1; return (uint32_t)-1;
for (auto it = sectionList.begin(); it != sectionList.end(); it++) { if (auto search = sectionMap.find(sect->name); search != sectionMap.end())
if (&*it == sect) return (uint32_t)(sectionMap.size() - search->second - 1);
return it - sectionList.begin();
}
fatalerror("Unknown section '%s'\n", sect->name.c_str()); fatalerror("Unknown section '%s'\n", sect->name.c_str());
} }
@@ -373,8 +371,8 @@ void out_WriteObject() {
for (Symbol const *sym : objectSymbols) for (Symbol const *sym : objectSymbols)
writesymbol(*sym, f); writesymbol(*sym, f);
for (Section &sect : sectionList) for (auto it = sectionList.rbegin(); it != sectionList.rend(); it++)
writesection(sect, f); writesection(*it, f);
putlong(assertions.size(), f); putlong(assertions.size(), f);

View File

@@ -11,7 +11,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string>
#include "error.hpp" #include "error.hpp"
@@ -41,6 +40,7 @@ struct SectionStackEntry {
std::stack<UnionStackEntry> currentUnionStack; std::stack<UnionStackEntry> currentUnionStack;
std::deque<SectionStackEntry> sectionStack; std::deque<SectionStackEntry> sectionStack;
std::deque<Section> sectionList; std::deque<Section> sectionList;
std::unordered_map<std::string, size_t> sectionMap; // Indexes into `sectionList`
uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset)
Section *currentSection = nullptr; Section *currentSection = nullptr;
static Section *currentLoadSection = nullptr; static Section *currentLoadSection = nullptr;
@@ -110,11 +110,8 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size) {
} }
Section *sect_FindSectionByName(char const *name) { Section *sect_FindSectionByName(char const *name) {
for (Section &sect : sectionList) { auto search = sectionMap.find(name);
if (sect.name == name) return search != sectionMap.end() ? &sectionList[search->second] : nullptr;
return &sect;
}
return nullptr;
} }
#define mask(align) ((1U << (align)) - 1) #define mask(align) ((1U << (align)) - 1)
@@ -300,8 +297,9 @@ static Section *createSection(
uint16_t alignOffset, uint16_t alignOffset,
SectionModifier mod SectionModifier mod
) { ) {
// Add the new section to the list (order doesn't matter) // Add the new section to the list
Section &sect = sectionList.emplace_front(); Section &sect = sectionList.emplace_back();
sectionMap.emplace(name, sectionMap.size());
sect.name = name; sect.name = name;
sect.type = type; sect.type = type;