From 77129b9e806acffc9218b2960c52df7c9b05d599 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Fri, 23 Aug 2024 01:34:36 +0200 Subject: [PATCH] Fix a false positive reported by `scan-build` Arguably this also makes the logic a little clearer, so might as well --- src/link/assign.cpp | 80 +++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/src/link/assign.cpp b/src/link/assign.cpp index a8b706d7..924330f8 100644 --- a/src/link/assign.cpp +++ b/src/link/assign.cpp @@ -128,46 +128,47 @@ static ssize_t getPlacement(Section const §ion, MemoryLocation &location) { std::deque &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 §ion, 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 §ion) { 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!