Fix a false positive reported by scan-build

Arguably this also makes the logic a little clearer, so might as well
This commit is contained in:
ISSOtm
2024-08-23 01:34:36 +02:00
parent 44332ff4be
commit 77129b9e80

View File

@@ -128,46 +128,47 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
std::deque<FreeSpace> &bankMem = memory[section.type][location.bank - typeInfo.firstBank];
size_t spaceIdx = 0;
if (spaceIdx < bankMem.size())
if (spaceIdx < bankMem.size()) {
location.address = bankMem[spaceIdx].address;
// Process locations in that bank
while (spaceIdx < bankMem.size()) {
// If that location is OK, return it
if (isLocationSuitable(section, bankMem[spaceIdx], location))
return spaceIdx;
// Process locations in that bank
while (spaceIdx < bankMem.size()) {
// If that location is OK, return it
if (isLocationSuitable(section, bankMem[spaceIdx], location))
return spaceIdx;
// Go to the next *possible* location
if (section.isAddressFixed) {
// If the address is fixed, there can be only
// one candidate block per bank; if we already
// reached it, give up.
if (location.address < section.org)
location.address = section.org;
else
break; // Try again in next bank
} else if (section.isAlignFixed) {
// Move to next aligned location
// Move back to alignment boundary
location.address -= section.alignOfs;
// Ensure we're there (e.g. on first check)
location.address &= ~section.alignMask;
// Go to next align boundary and add offset
location.address += section.alignMask + 1 + section.alignOfs;
} else {
// Any location is fine, so, next free block
spaceIdx++;
if (spaceIdx < bankMem.size())
location.address = bankMem[spaceIdx].address;
// Go to the next *possible* location
if (section.isAddressFixed) {
// If the address is fixed, there can be only
// one candidate block per bank; if we already
// reached it, give up.
if (location.address < section.org)
location.address = section.org;
else
break; // Try again in next bank
} else if (section.isAlignFixed) {
// Move to next aligned location
// Move back to alignment boundary
location.address -= section.alignOfs;
// Ensure we're there (e.g. on first check)
location.address &= ~section.alignMask;
// Go to next align boundary and add offset
location.address += section.alignMask + 1 + section.alignOfs;
} else {
// Any location is fine, so, next free block
spaceIdx++;
if (spaceIdx < bankMem.size())
location.address = bankMem[spaceIdx].address;
}
// If that location is past the current block's end,
// go forwards until that is no longer the case.
while (spaceIdx < bankMem.size()
&& location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size)
spaceIdx++;
// Try again with the new location/free space combo
}
// If that location is past the current block's end,
// go forwards until that is no longer the case.
while (spaceIdx < bankMem.size()
&& location.address >= bankMem[spaceIdx].address + bankMem[spaceIdx].size)
spaceIdx++;
// Try again with the new location/free space combo
}
// Try again in the next bank, if one is available.
@@ -182,7 +183,8 @@ static ssize_t getPlacement(Section const &section, MemoryLocation &location) {
location.bank = scrambleROMX + 1;
else
return -1;
} else if (scrambleWRAMX && section.type == SECTTYPE_WRAMX && location.bank <= scrambleWRAMX) {
} else if (scrambleWRAMX && section.type == SECTTYPE_WRAMX
&& location.bank <= scrambleWRAMX) {
if (location.bank > typeInfo.firstBank)
location.bank--;
else if (scrambleWRAMX < typeInfo.lastBank)
@@ -246,8 +248,8 @@ static void placeSection(Section &section) {
bankMem.insert(
bankMem.begin() + spaceIdx + 1,
{.address = (uint16_t)(section.org + section.size),
.size =
(uint16_t)(freeSpace.address + freeSpace.size - section.org - section.size)}
.size = (uint16_t)(freeSpace.address + freeSpace.size - section.org - section.size)
}
);
// **`freeSpace` cannot be reused from this point on**, because `bankMem.insert`
// invalidates all references to itself!