mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-23 23:04:36 +01:00
fix and detect freeing invalid image lists
This commit is contained in:
@@ -14,8 +14,11 @@
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include "../core/Console.hpp"
|
||||
#include "../core/Guard.hpp"
|
||||
#include "../core/Memory.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
@@ -35,13 +38,34 @@ struct ImageList
|
||||
static bool _initialised = false;
|
||||
static std::list<ImageList> _freeLists;
|
||||
|
||||
#ifdef DEBUG
|
||||
static std::list<ImageList> _allocatedLists;
|
||||
|
||||
static bool AllocatedListContains(uint32 baseImageId, uint32 count)
|
||||
{
|
||||
bool contains = std::any_of(
|
||||
_allocatedLists.begin(),
|
||||
_allocatedLists.end(),
|
||||
[baseImageId, count](const ImageList &imageList) -> bool
|
||||
{
|
||||
return imageList.BaseId == baseImageId && imageList.Count == count;
|
||||
});
|
||||
return contains;
|
||||
}
|
||||
#endif
|
||||
|
||||
static uint32 AllocateImageList(uint32 count)
|
||||
{
|
||||
Guard::Assert(count != 0);
|
||||
|
||||
if (!_initialised)
|
||||
{
|
||||
_initialised = true;
|
||||
_freeLists.clear();
|
||||
_freeLists.push_back({ BASE_IMAGE_ID, MAX_IMAGES });
|
||||
#ifdef DEBUG
|
||||
_allocatedLists.clear();
|
||||
#endif
|
||||
}
|
||||
|
||||
for (auto it = _freeLists.begin(); it != _freeLists.end(); it++)
|
||||
@@ -57,6 +81,9 @@ static uint32 AllocateImageList(uint32 count)
|
||||
_freeLists.push_back(remainder);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
_allocatedLists.push_back({ imageList.BaseId, count });
|
||||
#endif
|
||||
return imageList.BaseId;
|
||||
}
|
||||
}
|
||||
@@ -68,6 +95,11 @@ static void FreeImageList(uint32 baseImageId, uint32 count)
|
||||
Guard::Assert(_initialised);
|
||||
Guard::Assert(baseImageId >= BASE_IMAGE_ID);
|
||||
|
||||
#ifdef DEBUG
|
||||
bool contains = AllocatedListContains(baseImageId, count);
|
||||
Guard::Assert(contains);
|
||||
#endif
|
||||
|
||||
// TODO validate that this was an allocated list
|
||||
_freeLists.push_back({ baseImageId, count });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user