Exemplo n.º 1
0
void app_run()
{
	char ch;	

	while(1)
	{	
		app_menu();	
		ch=getch();			
		switch(ch)
		{
			case '1':	front_push();				break;
			case '2':	back_push();				break;
			case '3':	front_pop();				break;
			case '4':	back_pop();					break;
			case '5':   return;
		}		
		system("pause");
	}
}
Exemplo n.º 2
0
/* 生成内存池
* pBuf: 给定的内存buffer起始地址
* sBufSize: 给定的内存buffer大小
* 返回生成的内存池指针
************************************************************************/
PMEMORYPOOL CreateMemoryPool(void* pBuf, size_t sBufSize)
{
#ifdef THREAD
	INITMUTEX(hMutex);
#endif
	memset(pBuf, 0, sBufSize);
	PMEMORYPOOL mem_pool = (PMEMORYPOOL)pBuf;
	// 计算需要多少memory map单元格
	size_t mem_pool_struct_size = sizeof(MEMORYPOOL);
	mem_pool->mem_map_pool_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
	mem_pool->mem_map_unit_count = (sBufSize - mem_pool_struct_size + MINUNITSIZE - 1) / MINUNITSIZE;
	mem_pool->pmem_map = (memory_block*)((char*)pBuf + mem_pool_struct_size);
	mem_pool->pfree_mem_chunk_pool = (memory_chunk*)((char*)pBuf + mem_pool_struct_size + sizeof(memory_block) * mem_pool->mem_map_unit_count);
	
	mem_pool->memory = (char*)pBuf + mem_pool_struct_size+ sizeof(memory_block) * mem_pool->mem_map_unit_count + sizeof(memory_chunk) * mem_pool->mem_map_pool_count;
	mem_pool->size = sBufSize - mem_pool_struct_size - sizeof(memory_block) * mem_pool->mem_map_unit_count - sizeof(memory_chunk) * mem_pool->mem_map_pool_count;
	size_t align = check_align_addr(mem_pool->memory);
	mem_pool->size -= align;
	mem_pool->size = check_align_block(mem_pool->size);
	mem_pool->mem_block_count = mem_pool->size / MINUNITSIZE;
	// 链表化
	mem_pool->pfree_mem_chunk_pool = create_list(mem_pool->pfree_mem_chunk_pool, mem_pool->mem_map_pool_count);
	// 初始化 pfree_mem_chunk,双向循环链表
	memory_chunk* tmp = front_pop(mem_pool->pfree_mem_chunk_pool);
	tmp->pre = tmp;
	tmp->next = tmp;
	tmp->pfree_mem_addr = NULL;
	mem_pool->mem_map_pool_count--;
	
	// 初始化 pmem_map
	mem_pool->pmem_map[0].count = mem_pool->mem_block_count;
	mem_pool->pmem_map[0].pmem_chunk = tmp;
	mem_pool->pmem_map[mem_pool->mem_block_count-1].start = 0;
	
	tmp->pfree_mem_addr = mem_pool->pmem_map;
	push_back(mem_pool->pfree_mem_chunk, tmp);
	mem_pool->free_mem_chunk_count = 1;
	mem_pool->mem_used_size = 0;
	return mem_pool;
}
Exemplo n.º 3
0
void FreeMemory(void *ptrMemoryBlock, PMEMORYPOOL pMem)   
{  
    size_t current_index = addr2index(pMem, ptrMemoryBlock);  
    size_t size = pMem->pmem_map[current_index].count * MINUNITSIZE;  
    // 判断与当前释放的内存块相邻的内存块是否可以与当前释放的内存块合并  
    memory_block* pre_block = NULL;  
    memory_block* next_block = NULL;  
    memory_block* current_block = &(pMem->pmem_map[current_index]);  
    // 第一个  
    if (current_index == 0) {  
        if (current_block->count < pMem->mem_block_count) {  
            next_block = &(pMem->pmem_map[current_index+current_block->count]);  
            // 如果后一个内存块是空闲的,合并  
            if (next_block->pmem_chunk != NULL) {  
                next_block->pmem_chunk->pfree_mem_addr = current_block;  
                pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;  
                current_block->count += next_block->count;  
                current_block->pmem_chunk = next_block->pmem_chunk;  
                next_block->pmem_chunk = NULL;  
            } else {  // 如果后一块内存不是空闲的,在pfree_mem_chunk中增加一个chunk 
                memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);  
                new_chunk->pfree_mem_addr = current_block;  
                current_block->pmem_chunk = new_chunk;  
                push_back(pMem->pfree_mem_chunk, new_chunk);  
                pMem->mem_map_pool_count--;  
                pMem->free_mem_chunk_count++;  
            }  
        } else {  
            memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);  
            new_chunk->pfree_mem_addr = current_block;  
            current_block->pmem_chunk = new_chunk;  
            push_back(pMem->pfree_mem_chunk, new_chunk);  
            pMem->mem_map_pool_count--;  
            pMem->free_mem_chunk_count++;  
        }         
    } else if (current_index == pMem->mem_block_count-1) {  // 最后一个  
        if (current_block->count < pMem->mem_block_count) {  
            pre_block = &(pMem->pmem_map[current_index-1]);  
            size_t index = pre_block->count;  
            pre_block = &(pMem->pmem_map[index]);  
              
            // 如果前一个内存块是空闲的,合并  
            if (pre_block->pmem_chunk != NULL) {  
                pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;  
                pre_block->count += current_block->count;  
                current_block->pmem_chunk = NULL;  
            } else {  // 如果前一块内存不是空闲的,在pfree_mem_chunk中增加一个chunk 
                memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);  
                new_chunk->pfree_mem_addr = current_block;  
                current_block->pmem_chunk = new_chunk;  
                push_back(pMem->pfree_mem_chunk, new_chunk);  
                pMem->mem_map_pool_count--;  
                pMem->free_mem_chunk_count++;  
            }  
        } else {  
            memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);  
            new_chunk->pfree_mem_addr = current_block;  
            current_block->pmem_chunk = new_chunk;  
            push_back(pMem->pfree_mem_chunk, new_chunk);  
            pMem->mem_map_pool_count--;  
            pMem->free_mem_chunk_count++;  
        }  
    } else {         
        next_block = &(pMem->pmem_map[current_index+current_block->count]);  
        pre_block = &(pMem->pmem_map[current_index-1]);  
        size_t index = pre_block->start;  
        pre_block = &(pMem->pmem_map[index]);  
        bool is_back_merge = false;  
        if (next_block->pmem_chunk == NULL && pre_block->pmem_chunk == NULL) {  
            memory_chunk* new_chunk = front_pop(pMem->pfree_mem_chunk_pool);  
            new_chunk->pfree_mem_addr = current_block;  
            current_block->pmem_chunk = new_chunk;  
            push_back(pMem->pfree_mem_chunk, new_chunk);  
            pMem->mem_map_pool_count--;  
            pMem->free_mem_chunk_count++;  
        }  
        // 后一个内存块  
        if (next_block->pmem_chunk != NULL) {  
            next_block->pmem_chunk->pfree_mem_addr = current_block;  
            pMem->pmem_map[current_index+current_block->count+next_block->count-1].start = current_index;  
            current_block->count += next_block->count;  
            current_block->pmem_chunk = next_block->pmem_chunk;  
            next_block->pmem_chunk = NULL;  
            is_back_merge = true;  
        }  
        // 前一个内存块  
        if (pre_block->pmem_chunk != NULL) {  
            pMem->pmem_map[current_index+current_block->count-1].start = current_index - pre_block->count;  
            pre_block->count += current_block->count;  
            if (is_back_merge) {  
                delete_chunk(pMem->pfree_mem_chunk, current_block->pmem_chunk);  
                push_front(pMem->pfree_mem_chunk_pool, current_block->pmem_chunk);  
                pMem->free_mem_chunk_count--;  
                pMem->mem_map_pool_count++;  
            }  
            current_block->pmem_chunk = NULL;              
        }         
    }

    pMem->mem_used_size -= size;  
}