Merge pull request #1478 from magcius/memmap-cleanup

MemArena: Fix a memory leak caused by pointer confusion
This commit is contained in:
Lioncash 2014-11-02 23:30:38 -05:00
commit 7c698b0ece
1 changed files with 17 additions and 17 deletions

View File

@ -174,18 +174,6 @@ static bool Memory_TryBase(u8 *base, MemoryView *views, int num_views, u32 flags
{ {
// OK, we know where to find free space. Now grab it! // OK, we know where to find free space. Now grab it!
// We just mimic the popular BAT setup. // We just mimic the popular BAT setup.
u32 shm_position = 0;
// Zero all the pointers to be sure.
for (int i = 0; i < num_views; i++)
{
views[i].mapped_ptr = nullptr;
if (!(views[i].flags & MV_MIRROR_PREVIOUS) && i > 0)
shm_position += views[i - 1].size;
views[i].shm_position = shm_position;
}
int i; int i;
for (i = 0; i < num_views; i++) for (i = 0; i < num_views; i++)
@ -234,16 +222,28 @@ bail:
return false; return false;
} }
u8 *MemoryMap_Setup(MemoryView *views, int num_views, u32 flags, MemArena *arena) static void MemoryMap_InitializeViews(MemoryView *views, int num_views, u32 flags)
{ {
u32 total_mem = 0; u32 shm_position = 0;
for (int i = 0; i < num_views; i++) for (int i = 0; i < num_views; i++)
{ {
// Zero all the pointers to be sure.
views[i].mapped_ptr = nullptr;
SKIP(flags, views[i].flags); SKIP(flags, views[i].flags);
if ((views[i].flags & MV_MIRROR_PREVIOUS) == 0)
total_mem += views[i].size; views[i].shm_position = shm_position;
if (!(views[i].flags & MV_MIRROR_PREVIOUS))
shm_position += views[i].size;
} }
}
u8 *MemoryMap_Setup(MemoryView *views, int num_views, u32 flags, MemArena *arena)
{
MemoryMap_InitializeViews(views, num_views, flags);
u32 total_mem = views[num_views - 1].shm_position;
arena->GrabSHMSegment(total_mem); arena->GrabSHMSegment(total_mem);
@ -267,7 +267,7 @@ void MemoryMap_Shutdown(MemoryView *views, int num_views, u32 flags, MemArena *a
for (int i = 0; i < num_views; i++) for (int i = 0; i < num_views; i++)
{ {
MemoryView* view = &views[i]; MemoryView* view = &views[i];
if (view->mapped_ptr && *(u8*)view->mapped_ptr && !freeset.count(view->mapped_ptr)) if (view->mapped_ptr && !freeset.count(view->mapped_ptr))
{ {
arena->ReleaseView(view->mapped_ptr, view->size); arena->ReleaseView(view->mapped_ptr, view->size);
freeset.insert(view->mapped_ptr); freeset.insert(view->mapped_ptr);