Example #1
0
void testAllocator()
{
	printf("Test Firebird::MemoryPool\n");
	MemoryPool* parent = getDefaultMemoryPool();
	MemoryPool* pool = MemoryPool::createPool(parent);

	MallocAllocator allocator;
	BePlusTree<AllocItem, AllocItem, MallocAllocator, DefaultKeyValue<AllocItem>, AllocItem> items(&allocator),
		bigItems(&allocator);

	Vector<void*, LARGE_ITEMS> la;
	printf("Allocate %d large items: ", LARGE_ITEMS);
	int i;
	for (i = 0; i<LARGE_ITEMS; i++) {
		la.add(pool->allocate(LARGE_ITEM_SIZE));
		VERIFY_POOL(pool);
	}
	VERIFY_POOL(pool);
	printf(" DONE\n");

	printf("Allocate %d items: ", ALLOC_ITEMS);
	int n = 0;
	VERIFY_POOL(pool);
	for (i = 0; i < ALLOC_ITEMS; i++) {
		n = n * 47163 - 57412;
		// n = n * 45578 - 17651;
		AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE) / 2 + 1)};
		items.add(temp);
	}
	printf(" DONE\n");
	VERIFY_POOL(pool);
	VERIFY_POOL(parent);

	printf("Deallocate half of items in quasi-random order: ");
	n = 0;
	if (items.getFirst()) do {
		pool->deallocate(items.current().item);
		n++;
	} while (n < ALLOC_ITEMS / 2 && items.getNext());
	printf(" DONE\n");
	VERIFY_POOL(pool);
	VERIFY_POOL(parent);

	printf("Allocate %d big items: ", BIG_ITEMS);
	n = 0;
	VERIFY_POOL(pool);
	for (i = 0; i < BIG_ITEMS; i++) {
		n = n * 47163 - 57412;
		// n = n * 45578 - 17651;
		AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE) / 2 + 1)};
		bigItems.add(temp);
	}
	printf(" DONE\n");
	VERIFY_POOL(pool);
	VERIFY_POOL(parent);

	printf("Deallocate the rest of small items in quasi-random order: ");
	while (items.getNext()) {
		pool->deallocate(items.current().item);
	}
	printf(" DONE\n");
	VERIFY_POOL(pool);
	VERIFY_POOL(parent);

	printf("Deallocate big items in quasi-random order: ");
	if (bigItems.getFirst()) do {
		pool->deallocate(bigItems.current().item);
	} while (bigItems.getNext());
	printf(" DONE\n");

	printf("Deallocate %d large items: ", LARGE_ITEMS/2);
	for (i = 0; i<LARGE_ITEMS/2; i++)
		pool->deallocate(la[i]);
	VERIFY_POOL(pool);
	printf(" DONE\n");


//	pool->verify_pool();
//	parent->verify_pool();
	pool->print_contents(stdout, false);
	parent->print_contents(stdout, false);
	MemoryPool::deletePool(pool);
//	parent->verify_pool();
//  TODO:
//	Test critically low memory conditions
//  Test that tree correctly recovers in low-memory conditions
}