TEST( MemoryRouter, differentAllocationMethods ) { DefaultAllocator defaultAllocator; // at the beginning, no memory is allocated CPPUNIT_ASSERT_EQUAL( (ulong)0, defaultAllocator.getMemoryUsed() ); MemoryRouter& router = TSingleton< MemoryRouter >::getInstance(); void* ptr = router.alloc( 12, AM_ALIGNED_16, &defaultAllocator ); // memory was indeed allocated - but since we were allocating an aligned block, // a larger block was allocated CPPUNIT_ASSERT( ptr != NULL ); CPPUNIT_ASSERT( (ulong)12 < defaultAllocator.getMemoryUsed() ); // and that the returned address is aligned to a 16 byte boundary bool isAligned = MemoryUtils::isAddressAligned( ptr ); CPPUNIT_ASSERT( isAligned ); // cleanup router.dealloc( ptr, AM_ALIGNED_16 ); // allocated memory was correctly released - the entire amount CPPUNIT_ASSERT_EQUAL( (ulong)0, defaultAllocator.getMemoryUsed() ); }
TEST( DefaultAllocator, threadSafety ) { Thread thread1; Thread thread2; DefaultAllocator allocator; CPPUNIT_ASSERT_EQUAL( (ulong)0, allocator.getMemoryUsed() ); MultithreadedAllocFunc allocOperator1( allocator ); MultithreadedAllocFunc allocOperator2( allocator ); thread1.start( allocOperator1 ); thread2.start( allocOperator2 ); thread1.join(); thread2.join(); CPPUNIT_ASSERT_EQUAL( (ulong)0, allocator.getMemoryUsed() ); }
TEST( MemoryRouter, objectsPlacement ) { DefaultAllocator defaultAllocator; MemoryRouter& router = TSingleton< MemoryRouter >::getInstance(); ulong initialAllocatedMemorySize = router.getMemoryUsed(); TestClass* obj = new ( &defaultAllocator ) TestClass(); CPPUNIT_ASSERT( obj != NULL ); // no memory was allocated in the pool internally managed by the router CPPUNIT_ASSERT_EQUAL( (ulong)0, router.getMemoryUsed() - initialAllocatedMemorySize ); // entire memory was allocated in the specified external pool ulong expectedSize = MemoryUtils::calcAlignedSize( (ulong)(sizeof( TestClass ) + sizeof(void*)) ); CPPUNIT_ASSERT_EQUAL( expectedSize, defaultAllocator.getMemoryUsed() ); delete obj; CPPUNIT_ASSERT_EQUAL( (ulong)0, router.getMemoryUsed() - initialAllocatedMemorySize ); CPPUNIT_ASSERT_EQUAL( (ulong)0, defaultAllocator.getMemoryUsed() ); }