Exemplo n.º 1
0
static void delete_chunk_chain (Chunk_Type *c)
{
   while (c != NULL)
     {
	Chunk_Type *next;
	next = c->next;
	delete_chunk (c);
	c = next;
     }
}
Exemplo n.º 2
0
void* GetMemory(size_t sMemorySize, PMEMORYPOOL pMem)  
{  
    sMemorySize = check_align_size(sMemorySize);  
    size_t index = 0;  
    memory_chunk* tmp = pMem->pfree_mem_chunk;  
    for (index = 0; index < pMem->free_mem_chunk_count; index++) {  
        if (tmp->pfree_mem_addr->count * MINUNITSIZE >= sMemorySize) {             
            break;  
        }  
          
        tmp = tmp->next;  
    }  
      
    if (index == pMem->free_mem_chunk_count) {  
        return NULL;  
    }  
    pMem->mem_used_size += sMemorySize;  
    if (tmp->pfree_mem_addr->count * MINUNITSIZE == sMemorySize) {  
        // 当要分配的内存大小与当前chunk中的内存大小相同时,从pfree_mem_chunk链表中删除此chunk  
        size_t current_index = (tmp->pfree_mem_addr - pMem->pmem_map);  
        delete_chunk(pMem->pfree_mem_chunk, tmp);  
        tmp->pfree_mem_addr->pmem_chunk = NULL;  
          
        push_front(pMem->pfree_mem_chunk_pool, tmp);  
        pMem->free_mem_chunk_count--;  
        pMem->mem_map_pool_count++;  
          
        return index2addr(pMem, current_index);  
    } else {  
        // 当要分配的内存小于当前chunk中的内存时,更改pfree_mem_chunk中相应chunk的pfree_mem_addr  
          
        // 复制当前mem_map_unit  
        memory_block copy;  
        copy.count = tmp->pfree_mem_addr->count;  
        copy.pmem_chunk = tmp;  
        // 记录该block的起始和结束索引  
        memory_block* current_block = tmp->pfree_mem_addr;  
        current_block->count = sMemorySize / MINUNITSIZE;  
        size_t current_index = (current_block - pMem->pmem_map);  
        pMem->pmem_map[current_index+current_block->count-1].start = current_index;  
        current_block->pmem_chunk = NULL; // NULL表示当前内存块已被分配  
        // 当前block被一分为二,更新第二个block中的内容  
        pMem->pmem_map[current_index+current_block->count].count = copy.count - current_block->count;  
        pMem->pmem_map[current_index+current_block->count].pmem_chunk = copy.pmem_chunk;  
        // 更新原来的pfree_mem_addr  
        tmp->pfree_mem_addr = &(pMem->pmem_map[current_index+current_block->count]);  
      
        size_t end_index = current_index + copy.count - 1;  
        pMem->pmem_map[end_index].start = current_index + current_block->count;

        return index2addr(pMem, current_index);  
    }     
}
Exemplo n.º 3
0
static void list_delete_elem (SLang_List_Type *list, SLindex_Type *indxp)
{
   SLang_Object_Type *elem;
   Chunk_Type *c;
   char *src, *dest, *src_max;
   SLindex_Type indx;

   indx = *indxp;
   if (NULL == (elem = find_nth_element (list, indx, &c)))
     return;

   if (indx < 0) indx += list->length; /* checked by find_nth_element */

   SLang_free_object (elem);
   c->num_elements--;
   list->length--;

   if (c->num_elements == 0)
     {
	if (list->first == c)
	  list->first = c->next;
	if (list->last == c)
	  list->last = c->prev;
	if (c->next != NULL)
	  c->next->prev = c->prev;
	if (c->prev != NULL)
	  c->prev->next = c->next;
	delete_chunk (c);
	if (list->recent == c)
	  list->recent = NULL;
	return;
     }

   src = (char *) (elem + 1);
   dest = (char *) elem;
   src_max = src + sizeof(SLang_Object_Type)*((c->elements+c->num_elements)-elem);
   while (src < src_max)
     *dest++ = *src++;

   if ((list->recent != NULL) && (list->recent_num > indx))
     list->recent_num--;
}
Exemplo n.º 4
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;  
}