void findMemoryBlock_ShouldReturnNullForNonePositiveParam(void) { // fixture setup MemoryType invalid_type_one = 0, invalid_type_two = -1; // system under control void* actual_one = findMemoryBlock(invalid_type_one); void* actual_two = findMemoryBlock(invalid_type_two); // verify void* expected = NULL; CU_ASSERT_EQUAL(actual_one, expected); CU_ASSERT_EQUAL(actual_two, expected); }
void findMemoryBlock_ShouldReturnNullForTypeNotContained(void) { // fixture setup MemoryType type_not_contained = 12345; // system under control void* actual = findMemoryBlock(type_not_contained); // verify void* expected = NULL; CU_ASSERT_EQUAL(actual, expected); }
void findMemoryBlock_ShouldFindNextMemoryBlockOfType(void) { // fixture setup MemoryType type = 321; void* expected_one = makeMemoryBlock(12, type); CU_ASSERT_NOT_EQUAL_FATAL(expected_one, NULL); void* expected_two = makeMemoryBlock(12, type); CU_ASSERT_NOT_EQUAL_FATAL(expected_two, NULL); void* expected_empty = NULL; // system under control void* actual_two = findMemoryBlock(type); void* actual_one = findMemoryBlock(type); void* actual_empty = findMemoryBlock(type); // verify CU_ASSERT_EQUAL(actual_one, expected_one); CU_ASSERT_EQUAL(actual_two, expected_two); CU_ASSERT_EQUAL(actual_empty, expected_empty); }
void * BasicMemoryPool::newMemory(const unsigned int size, const UNIT u){ const unsigned int sizeb = sizeInByte_(size, u); const unsigned int chunkNum = sizeb % CHUNK_SIZE_B == 0 ? sizeb/CHUNK_SIZE_B : sizeb/CHUNK_SIZE_B + 1; std::list<BLOCK_PTR>::iterator blockIter = findMemoryBlock(sizeb); if( blockIter == blocks_.end() ){ std::cerr << "find not suitable block. " << std::endl; return NULL; } else{ BLOCK_PTR block = *blockIter; // split block into 2 blocks if necessary if( block->length * CHUNK_SIZE_B > sizeb ){ BLOCK_PTR newblock = BLOCK_PTR(new BLOCK); newblock->isAlloc = true; newblock->length = chunkNum; newblock->baseChunkIndex = block->baseChunkIndex; for(int i = 0; i < newblock->length; i++) chunks_[ newblock->baseChunkIndex + i ]->isAlloc = true; blocks_.insert(blockIter, newblock); block->isAlloc = false; block->length -= newblock->length; block->baseChunkIndex += newblock->length; return chunks_[ newblock->baseChunkIndex ]->base; }else{ block->isAlloc = true; for(int i = 0; i < block->length; i++) chunks_[ block->baseChunkIndex + i ]->isAlloc = true; return chunks_[ block->baseChunkIndex ]->base; } } }