Skip to content

Commit

Permalink
Added a test for deallocation. Deallocation currently doesn't work, t…
Browse files Browse the repository at this point in the history
…he freelist

alsways has one request still in it. The list is not being cleared out after
each request.

Secondly, I think the semaphores are incorrect too. They are related to the
queues I believe.
  • Loading branch information
Jeremy Wright committed Nov 6, 2011
1 parent 9604d3b commit 5823696
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions BuddyAllocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class BuddyAllocator {
freeList[level].freeBlocks.pop_front();
unlock(level);
}
if(p == 0)
cerr << "Level: " << level << endl;
assert(p != 0);
}

Expand Down Expand Up @@ -244,6 +246,7 @@ class BuddyAllocator {

if(freeList[level-1].waitingRequests.size() >0)
{
MemoryRequest* remembered = freeList[level-1].waitingRequests.front();
remembered->request = B;
freeList[level-1].waitingRequests.pop();
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/TestBuddyAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,30 @@ TEST_F(TestBuddyAllocator, MaxAllocation)

}

TEST_F(TestBuddyAllocator, Deallocate)
{
BuddyAllocator<int, 10> LargeAlloc;

BuddyAllocator<int, 10>::BlockPtr NulBlockPtr = NULL;
int number = static_cast<int>(pow(2,10));
BuddyAllocator<int, 10>::BlockPtr handles[number];
for(int i=0; i < number; i++)
{
handles[i] = LargeAlloc.allocate(1);
ASSERT_NE(handles[i], NulBlockPtr);
*(handles[i]) = i;
}

for(int i = 0; i < number; i++)
{
EXPECT_EQ(*(handles[i]), i);
}

EXPECT_THROW(LargeAlloc.allocate(1), std::bad_alloc); //Max out the allocator

//Release 1 block
LargeAlloc.deallocate(handles[0], 1);
EXPECT_THROW(LargeAlloc.allocate(1), std::bad_alloc); //Max out the allocator

}

0 comments on commit 5823696

Please sign in to comment.