示例#1
0
void WebProcess::platformSetCacheModel(CacheModel cacheModel)
{
#if USE(CFNETWORK)
    RetainPtr<CFStringRef> cfurlCacheDirectory;
#if USE(CFURLSTORAGESESSIONS)
    if (CFURLStorageSessionRef defaultStorageSession = ResourceHandle::defaultStorageSession())
        cfurlCacheDirectory.adoptCF(wkCopyFoundationCacheDirectory(defaultStorageSession));
    else
#endif
        cfurlCacheDirectory.adoptCF(wkCopyFoundationCacheDirectory(0));

    if (!cfurlCacheDirectory)
        cfurlCacheDirectory.adoptCF(WebCore::localUserSpecificStorageDirectory().createCFString());

    // As a fudge factor, use 1000 instead of 1024, in case the reported byte 
    // count doesn't align exactly to a megabyte boundary.
    uint64_t memSize = memorySize() / 1024 / 1000;
    uint64_t diskFreeSize = volumeFreeSize(cfurlCacheDirectory.get()) / 1024 / 1000;

    unsigned cacheTotalCapacity = 0;
    unsigned cacheMinDeadCapacity = 0;
    unsigned cacheMaxDeadCapacity = 0;
    double deadDecodedDataDeletionInterval = 0;
    unsigned pageCacheCapacity = 0;
    unsigned long urlCacheMemoryCapacity = 0;
    unsigned long urlCacheDiskCapacity = 0;

    calculateCacheSizes(cacheModel, memSize, diskFreeSize,
        cacheTotalCapacity, cacheMinDeadCapacity, cacheMaxDeadCapacity, deadDecodedDataDeletionInterval,
        pageCacheCapacity, urlCacheMemoryCapacity, urlCacheDiskCapacity);

    memoryCache()->setCapacities(cacheMinDeadCapacity, cacheMaxDeadCapacity, cacheTotalCapacity);
    memoryCache()->setDeadDecodedDataDeletionInterval(deadDecodedDataDeletionInterval);
    pageCache()->setCapacity(pageCacheCapacity);

    RetainPtr<CFURLCacheRef> cfurlCache;
#if USE(CFURLSTORAGESESSIONS)
    if (CFURLStorageSessionRef defaultStorageSession = ResourceHandle::defaultStorageSession())
        cfurlCache.adoptCF(wkCopyURLCache(defaultStorageSession));
    else
#endif // USE(CFURLSTORAGESESSIONS)
        cfurlCache.adoptCF(CFURLCacheCopySharedURLCache());

    CFURLCacheSetMemoryCapacity(cfurlCache.get(), urlCacheMemoryCapacity);
    CFURLCacheSetDiskCapacity(cfurlCache.get(), max<unsigned long>(urlCacheDiskCapacity, CFURLCacheDiskCapacity(cfurlCache.get()))); // Don't shrink a big disk cache, since that would cause churn.
#endif
}
void test_safeMallocTest_allocate_size_100_should_create_record_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory;
	MemoryBlock1 ptrBlock = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock.memory))-50);
	allocatedMemory =(char*)safeMallocTest(100);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(50,allocatedMemory-50);
	TEST_ASSERT_EQUAL(100,allocatedMemory);
	TEST_ASSERT_EQUAL(200,allocatedMemory+100);
	TEST_ASSERT_EQUAL(100,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'b',allocatedPool);
	
	_free_Expect(allocatedMemory);
    freeMemory(allocatedMemory);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_100_and_200_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(100,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(200,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(NULL,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_200_and_100_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "@@@@@@@@@@" , .memory[99] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "##########" , .memory[199] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,NULL,'b',allocatedPool);
	
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_200_and_100_and_300_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory300;
	
	MemoryBlock2 ptrBlock2 = {.header[49] = "##########" , .memory[199] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock1 ptrBlock1 = {.header[49] = "@@@@@@@@@@" , .memory[99] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "%%%%%%%%%%" , .memory[299] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(300,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_200_and_300_and_100_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory300;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "%%%%%%%%%%" , .memory[299] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(300,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_200_and_300_and_400_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory200,*allocatedMemory300,*allocatedMemory400;
	
	MemoryBlock2 ptrBlock2 = {.header[49] = "##########" , .memory[199] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "@@@@@@@@@@" , .memory[299] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock4 ptrBlock4 = {.header[49] = "%%%%%%%%%%" , .memory[399] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock4.memory))-50);
	allocatedMemory400 = (char*)safeMallocTest(400);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(300,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(200,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(400,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory400,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedMemory400);
    freeMemory(allocatedMemory400);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_400_and_300_and_200_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory200,*allocatedMemory300,*allocatedMemory400;
	
	MemoryBlock2 ptrBlock2 = {.header[49] = "##########" , .memory[199] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "@@@@@@@@@@" , .memory[299] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock4 ptrBlock4 = {.header[49] = "%%%%%%%%%%" , .memory[399] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+400+FOOTER_SIZE)),((char*)sizeof(ptrBlock4.memory))-50);
	allocatedMemory400 = (char*)safeMallocTest(400);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(300,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(200,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(400,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory400,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedMemory400);
    freeMemory(allocatedMemory400);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_300_and_100_and_200_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory300;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "%%%%%%%%%%" , .memory[299] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(300,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_100_and_400_and_200_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory400;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock4 ptrBlock4 = {.header[49] = "%%%%%%%%%%" , .memory[399] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+400+FOOTER_SIZE)),((char*)sizeof(ptrBlock4.memory))-50);
	allocatedMemory400 = (char*)safeMallocTest(400);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(400,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory400,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory400);
    freeMemory(allocatedMemory400);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_400_and_200_and_300_and_100_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory300,*allocatedMemory400;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "@@@@@@@@@@" , .memory[299] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock4 ptrBlock4 = {.header[49] = "%%%%%%%%%%" , .memory[399] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+400+FOOTER_SIZE)),((char*)sizeof(ptrBlock4.memory))-50);
	allocatedMemory400 = (char*)safeMallocTest(400);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(300,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(200,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(400,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory400,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftChildPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftChildPool));
	
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftChildPool);
	TEST_ASSERT_EQUAL_NODE(allocatedPool->left->left,NULL,'b',leftPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'b',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedMemory400);
    freeMemory(allocatedMemory400);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}

void test_safeMallocTest_allocate_size_500_400_and_200_and_300_and_100_should_create_descriptor_and_add_into_allocated_pool(void){
	initializePool();
	char *allocatedMemory100,*allocatedMemory200,*allocatedMemory300,*allocatedMemory400,*allocatedMemory500;
	
	MemoryBlock1 ptrBlock1 = {.header[49] = "##########" , .memory[99] = "abcdef", .footer[49] = "$$$$$$$$$$"};
	MemoryBlock2 ptrBlock2 = {.header[49] = "@@@@@@@@@@" , .memory[199] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock3 ptrBlock3 = {.header[49] = "@@@@@@@@@@" , .memory[299] = "abcdef123", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock4 ptrBlock4 = {.header[49] = "%%%%%%%%%%" , .memory[399] = "abcdef123456", .footer[49] = "&&&&&&&&&&"};
	MemoryBlock5 ptrBlock5 = {.header[49] = "%%%%%%%%%%" , .memory[499] = "abcdef123456789", .footer[49] = "&&&&&&&&&&"};
	
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+500+FOOTER_SIZE)),((char*)sizeof(ptrBlock5.memory))-50);
	allocatedMemory500 = (char*)safeMallocTest(500);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+400+FOOTER_SIZE)),((char*)sizeof(ptrBlock4.memory))-50);
	allocatedMemory400 = (char*)safeMallocTest(400);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+300+FOOTER_SIZE)),((char*)sizeof(ptrBlock3.memory))-50);
	allocatedMemory300 = (char*)safeMallocTest(300);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+200+FOOTER_SIZE)),((char*)sizeof(ptrBlock2.memory))-50);
	allocatedMemory200 = (char*)safeMallocTest(200);
	_malloc_ExpectAndReturn((sizeof(HEADER_SIZE+100+FOOTER_SIZE)),((char*)sizeof(ptrBlock1.memory))-50);
	allocatedMemory100 = (char*)safeMallocTest(100);
	
	TEST_ASSERT_NOT_NULL(allocatedPool);
	TEST_ASSERT_EQUAL(200,memorySize(allocatedPool));
	TEST_ASSERT_EQUAL(allocatedMemory200,memoryAddr(allocatedPool));
	TEST_ASSERT_EQUAL(100,memorySize(leftPool));
	TEST_ASSERT_EQUAL(allocatedMemory100,memoryAddr(leftPool));
	TEST_ASSERT_EQUAL(400,memorySize(rightPool));
	TEST_ASSERT_EQUAL(allocatedMemory400,memoryAddr(rightPool));
	TEST_ASSERT_EQUAL(300,memorySize(rightLeftChildPool));
	TEST_ASSERT_EQUAL(allocatedMemory300,memoryAddr(rightLeftChildPool));
	TEST_ASSERT_EQUAL(500,memorySize(rightChildPool));
	TEST_ASSERT_EQUAL(allocatedMemory500,memoryAddr(rightChildPool));
	
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightChildPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',rightLeftChildPool);
	TEST_ASSERT_EQUAL_NODE(NULL,NULL,'r',leftPool);
	TEST_ASSERT_EQUAL_NODE(rightLeftChildPool,rightChildPool,'b',rightPool);
	TEST_ASSERT_EQUAL_NODE(leftPool,rightPool,'b',allocatedPool);
	
	//Free memory and pool 
	_free_Expect(allocatedMemory100);
    freeMemory(allocatedMemory100);
	_free_Expect(allocatedMemory200);
    freeMemory(allocatedMemory200);
	_free_Expect(allocatedMemory300);
    freeMemory(allocatedMemory300);
	_free_Expect(allocatedMemory400);
    freeMemory(allocatedMemory400);
	_free_Expect(allocatedMemory500);
    freeMemory(allocatedMemory500);
	_free_Expect(allocatedPool);
    freeMemory(allocatedPool);
}
示例#3
0
   /*
   * Perform replica exchange move.
   */
   bool ReplicaMove::move()
   {
      MPI::Request request[4];
      MPI::Status  status;
      System::MoleculeIterator molIter;
      Molecule::AtomIterator   atomIter;
      int iA;
      int recvPt, sendPt;
  
      DArray<int> permutation;
      permutation.allocate(nProcs_);

      // Gather all derivatives of the perturbation Hamiltonians and parameters on processor with rank 0
      DArray<double> myDerivatives;
      myDerivatives.allocate(nParameters_);
      DArray<double> myParameters;
      myParameters.allocate(nParameters_);

      for (int i=0; i< nParameters_; i++) {
         myDerivatives[i] = system().perturbation().derivative(i);
         myParameters[i] = system().perturbation().parameter(i);
      }

      int size = 0;
      size += memorySize(myDerivatives);
      size += memorySize(myParameters);

      if (myId_ != 0) {

         MemoryOArchive sendCurrent;
         sendCurrent.allocate(size);

         sendCurrent << myDerivatives;
         sendCurrent << myParameters;

         sendCurrent.send(*communicatorPtr_, 0);
      } else {
         DArray< DArray<double> > allDerivatives;
         DArray< DArray<double> > allParameters;
         allDerivatives.allocate(nProcs_);
         allParameters.allocate(nProcs_);
   
         allDerivatives[0].allocate(nParameters_);
         allDerivatives[0] = myDerivatives;
         allParameters[0].allocate(nParameters_);
         allParameters[0] = myParameters;

         for (int i = 1; i<nProcs_; i++) {
            MemoryIArchive recvPartner;
            recvPartner.allocate(size);
            recvPartner.recv(*communicatorPtr_, i);
            allDerivatives[i].allocate(nParameters_);
            allParameters[i].allocate(nParameters_);
            recvPartner >> allDerivatives[i];
            recvPartner >> allParameters[i];
         }

         // Now we have the complete matrix U_ij = u_i(x_j), permutate nsampling steps according
         // to acceptance criterium
  
         // start with identity permutation
         for (int i = 0; i < nProcs_; i++)
            permutation[i] = i;

         for (int n =0; n < nSampling_; n++) {
            swapAttempt_++;
            // choose a pair i,j, i!= j at random
            int i = system().simulation().random().uniformInt(0,nProcs_);
            int j = system().simulation().random().uniformInt(0,nProcs_-1);
            if (i<=j) j++;

            // apply acceptance criterium
            double weight = 0;
            for (int k = 0; k < nParameters_; k++) {
               double deltaDerivative = allDerivatives[i][k] - allDerivatives[j][k];
               // the permutations operate on the states (the perturbation parameters)
               weight += (allParameters[permutation[j]][k] - allParameters[permutation[i]][k])*deltaDerivative;
             }
            double exponential = exp(-weight);
            int accept = system().simulation().random(). metropolis(exponential) ? 1 : 0;
   
            if (accept) {
               swapAccept_++;
               // swap states of pair i,j
               int tmp = permutation[i];
               permutation[i] = permutation[j];
               permutation[j] = tmp;
               }
         }
    
         // send exchange partner information to all other processors
         for (int i = 0; i < nProcs_; i++) {
            if (i != 0)
                communicatorPtr_->Send(&permutation[i], 1, MPI::INT, i, 0);
            else
                sendPt = permutation[i];

            if (permutation[i] != 0)
               communicatorPtr_->Send(&i, 1, MPI::INT, permutation[i], 1);
            else
               recvPt = i;
         }
      }

      
      if (myId_ != 0) {
         // partner id to receive from
         communicatorPtr_->Recv(&sendPt, 1, MPI::INT, 0, 0);
         // partner id to send to
         communicatorPtr_->Recv(&recvPt, 1, MPI::INT, 0, 1);
      }

      if (recvPt == myId_ || sendPt == myId_) {
         // no exchange necessary
         outputFile_ << sendPt << std::endl;
         return true;
      }

      assert(recvPt != myId_ && sendPt != myId_);

      Vector myBoundary;
      myBoundary = system().boundary().lengths();
            
      Vector ptBoundary;
            
      // Accomodate new boundary dimensions.
      request[0] = communicatorPtr_->Irecv(&ptBoundary, 1,
                                           MpiTraits<Vector>::type, recvPt, 1);

      // Send old boundary dimensions.
      request[1] = communicatorPtr_->Isend(&myBoundary, 1,
                                            MpiTraits<Vector>::type, sendPt, 1);

      request[0].Wait();
      request[1].Wait();
             
      system().boundary().setOrthorhombic(ptBoundary);

      // Pack atomic positions and types.
      iA = 0;
      for (int iSpec=0; iSpec < system().simulation().nSpecies(); ++iSpec){
         for (system().begin(iSpec, molIter); molIter.notEnd(); ++molIter){
            for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter) {
               myPositionPtr_[iA] = atomIter->position();
               iA++;
            }
         }
      }
      
      // Accomodate new configuration.
      request[2] = communicatorPtr_->Irecv(ptPositionPtr_, iA,
                       MpiTraits<Vector>::type, recvPt, 2); 

      // Send old configuration.
      request[3] = communicatorPtr_->Isend(myPositionPtr_, iA,
                       MpiTraits<Vector>::type, sendPt, 2);

      request[2].Wait();
      request[3].Wait();
      
      // Adopt the new atomic positions.
      iA = 0;
      for (int iSpec=0; iSpec < system().simulation().nSpecies(); ++iSpec){
         for (system().begin(iSpec, molIter); molIter.notEnd(); ++molIter){
            for (molIter->begin(atomIter); atomIter.notEnd(); ++atomIter){
               atomIter->position() = ptPositionPtr_[iA];
               ++iA;
            }
         }
      }
      

      // Notify component observers.
      sendRecvPair partners;
      partners[0] = sendPt;
      partners[1] = recvPt;
      Notifier<sendRecvPair>::notifyObservers(partners);

      // Log information about exchange partner to file
      outputFile_ << sendPt << std::endl;

      return true;

   }
示例#4
0
/** 
	This function will be call to check 
	the header and footer content of the record in allocatedPool 
	and freePool. 
**/
void _safeSummary(int lineNumber,char *fileName){
	int compareHeaderStr,compareFooterStr;
	char *headerContent = HEADERCONTENT,*footerContent = FOOTERCONTENT;
	//AllocatedPool
	if(allocatedPool !=NULL){
		compareHeaderStr = strcmp(memoryAddr(allocatedPool)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(allocatedPool)+memorySize(allocatedPool),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in root of the \nallocatedPool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in root of the \nallocatedPool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in root of the \nallocatedPool at file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
		}
	}else{
		printf("No record in allocatedPool at file: %s,line: %d\n",fileName,lineNumber);
		return;
	}
	if(allocatedPool->left !=NULL){
		compareHeaderStr = strcmp(memoryAddr(allocatedPool->left)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(allocatedPool->left)+memorySize(allocatedPool->left),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in left node of \nthe allocatedPool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in left node of the allocatedPool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in left node of the allocatedPool \nat file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
		}
	}
	if(allocatedPool->right !=NULL){
		compareHeaderStr = strcmp(memoryAddr(allocatedPool->right)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(allocatedPool->right)+memorySize(allocatedPool->right),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in right node of \nthe allocatedPool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in right node of the allocatedPool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in right node of the allocatedPool \nat file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
			
		}
	}
	//FreePool
	if(freePool !=NULL){
		compareHeaderStr = strcmp(memoryAddr(freePool)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(freePool)+memorySize(freePool),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in root of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in root of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in root of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
		}
	}else{
		printf("No record in freePool at file: %s,line: %d\n",fileName,lineNumber);
		return;
	}
	if(freePool->left !=NULL){
		compareHeaderStr = strcmp(memoryAddr(freePool->left)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(freePool->left)+memorySize(freePool->left),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in left node of \nthe freePool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in left node of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in left node of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
		}
	}
	if(freePool->right !=NULL){
		compareHeaderStr = strcmp(memoryAddr(freePool->right)-50,headerContent);
		compareFooterStr = strcmp(memoryAddr(freePool->right)+memorySize(freePool->right),footerContent);
		if(compareHeaderStr != 0){
			if(compareFooterStr !=0){
				printf("Header and Footer memory had been modified in right node of \nthe freePool at file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_FOOTER_MEMORY);
			}else{
				printf("Header memory had been modified in right node of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
				Throw(ERR_CORRUPTED_HEADER_MEMORY);
			}
		}else if(compareFooterStr !=0){
			printf("Warning! Write into Footer memory in right node of the freePool \nat file: %s,line: %d\n",fileName,lineNumber);
			Throw(ERR_CORRUPTED_FOOTER_MEMORY);
			
		}
	}
}
示例#5
0
void validate(boost::any& v,
              const std::vector<std::string>& values,
              memorySize* target_type, int)
{
//    using namespace boost::program_options;

    // Make sure no previous assignment to 'a' was made.
    po::validators::check_first_occurrence(v);
    // Extract the first string from 'values'. If there is more than
    // one string, it's an error, and exception will be thrown.
    const std::string& memSize = po::validators::get_single_string(values);

    /* parse the string. Support "number" or "numberX" where
       X is one of {k,K,m,M,g,G} with the expected meaning of X

       e.g.

       1024, 1k, 3.4M, 4.5G
    */
    float       value=0;
    char        units='\0';
    if (2 == sscanf(memSize.c_str(), "%f%c", &value, &units))
    {
        float       multiplier=1;
        if (islower(units))
        {
            units = toupper(units);
        }
        switch (units)
        {
            case 'G':
                multiplier *= 1024;
                // fall through
            case 'M':
                multiplier *= 1024;
                // fall through
            case 'K':
                multiplier *= 1024;
                break;
            default:
                std::stringstream   e;
                e << "Invalid units in rtalloc-mem-size option: " <<  memSize 
                  << ". Valid units: 'k','m','g' (case-insensitive).";
                throw po::invalid_option_value(e.str());
        }
        value *= multiplier;
    }
    else if (1 == sscanf(memSize.c_str(), "%f", &value))
    {
        // nothing to do
    }
    else
    {
        throw po::invalid_option_value("Could not parse rtalloc-mem-size option: " + memSize);
    }

    // provide some basic sanity checking
    // Note that TLSF has its own internal checks on the value.
    // TLSF's minimum size varies with build options, but it is
    // several kilobytes at least (6-8k on Mac OS X Snow Leopard
    // with 64-bit build, ~3k on Ubuntu Jaunty with 32-bit build).
    if (! (0 <= value) )
    {
        std::stringstream   e;
        e << "Invalid memory size of " << value << " given. Value must be >= 0.";
        throw po::invalid_option_value(e.str());
    }

    v = memorySize((size_t)value);
}