Cleanup: Simplify GRFLabel linked-list with std::vector. (#10284)

This commit is contained in:
PeterN 2022-12-25 22:32:22 +00:00 committed by GitHub
parent c53f29df53
commit 4f26f6b8aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 31 deletions

View File

@ -429,13 +429,7 @@ static GRFFile *GetFileByFilename(const char *filename)
/** Reset all NewGRFData that was used only while processing data */ /** Reset all NewGRFData that was used only while processing data */
static void ClearTemporaryNewGRFData(GRFFile *gf) static void ClearTemporaryNewGRFData(GRFFile *gf)
{ {
/* Clear the GOTO labels used for GRF processing */ gf->labels.clear();
for (GRFLabel *l = gf->label; l != nullptr;) {
GRFLabel *l2 = l->next;
free(l);
l = l2;
}
gf->label = nullptr;
} }
/** /**
@ -6745,15 +6739,15 @@ static void SkipIf(ByteReader *buf)
* file. The jump will always be the first matching label that follows * file. The jump will always be the first matching label that follows
* the current nfo_line. If no matching label is found, the first matching * the current nfo_line. If no matching label is found, the first matching
* label in the file is used. */ * label in the file is used. */
GRFLabel *choice = nullptr; const GRFLabel *choice = nullptr;
for (GRFLabel *label = _cur.grffile->label; label != nullptr; label = label->next) { for (const auto &label : _cur.grffile->labels) {
if (label->label != numsprites) continue; if (label.label != numsprites) continue;
/* Remember a goto before the current line */ /* Remember a goto before the current line */
if (choice == nullptr) choice = label; if (choice == nullptr) choice = &label;
/* If we find a label here, this is definitely good */ /* If we find a label here, this is definitely good */
if (label->nfo_line > _cur.nfo_line) { if (label.nfo_line > _cur.nfo_line) {
choice = label; choice = &label;
break; break;
} }
} }
@ -7601,23 +7595,9 @@ static void DefineGotoLabel(ByteReader *buf)
byte nfo_label = buf->ReadByte(); byte nfo_label = buf->ReadByte();
GRFLabel *label = MallocT<GRFLabel>(1); _cur.grffile->labels.emplace_back(nfo_label, _cur.nfo_line, _cur.file->GetPos());
label->label = nfo_label;
label->nfo_line = _cur.nfo_line;
label->pos = _cur.file->GetPos();
label->next = nullptr;
/* Set up a linked list of goto targets which we will search in an Action 0x7/0x9 */ grfmsg(2, "DefineGotoLabel: GOTO target with label 0x%02X", nfo_label);
if (_cur.grffile->label == nullptr) {
_cur.grffile->label = label;
} else {
/* Attach the label to the end of the list */
GRFLabel *l;
for (l = _cur.grffile->label; l->next != nullptr; l = l->next) {}
l->next = label;
}
grfmsg(2, "DefineGotoLabel: GOTO target with label 0x%02X", label->label);
} }
/** /**

View File

@ -98,7 +98,8 @@ struct GRFLabel {
byte label; byte label;
uint32 nfo_line; uint32 nfo_line;
size_t pos; size_t pos;
struct GRFLabel *next;
GRFLabel(byte label, uint32 nfo_line, size_t pos) : label(label), nfo_line(nfo_line), pos(pos) {}
}; };
/** Dynamic data of a loaded NewGRF */ /** Dynamic data of a loaded NewGRF */
@ -121,7 +122,7 @@ struct GRFFile : ZeroedMemoryAllocator {
uint32 param[0x80]; uint32 param[0x80];
uint param_end; ///< one more than the highest set parameter uint param_end; ///< one more than the highest set parameter
GRFLabel *label; ///< Pointer to the first label. This is a linked list, not an array. std::vector<GRFLabel> labels; ///< List of labels
std::vector<CargoLabel> cargo_list; ///< Cargo translation table (local ID -> label) std::vector<CargoLabel> cargo_list; ///< Cargo translation table (local ID -> label)
uint8 cargo_map[NUM_CARGO]; ///< Inverse cargo translation table (CargoID -> local ID) uint8 cargo_map[NUM_CARGO]; ///< Inverse cargo translation table (CargoID -> local ID)