📄 cetc_heap.c
字号:
/**
* Copyright (c) 2008, USEE
* All rights reserved.
*
* filename: cetc_heap.h
* abstract: about private heap header;
*
*
* current version: 1.0
* authors: bolidehi
* date: 2008-8-27
*
* history version
* version:
* authors:
* date:
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "cetc_heap.h"
#include "cetc_slist.h"
#include "cetc_type.h"
/**
* func : initilaze the privivate heap;
* @head : private heap;
* @block_num : block total;
* @block_size : block size;
* $return : if success return 0 else return non zero
*/
int cetc_init_privheap(tagPrivHeap *heap, int block_num, size_t block_size)
{
int i;
void *buf;
tagSListNode *node;
size_t heap_size;
CETC_DBG_INFO("Enter!\n");
heap_size = block_num * (block_size + sizeof(tagSListNode));
printf("block_size=%d heap_size=%d\n", block_size, heap_size);
heap->data = malloc(heap_size);
if(NULL == heap->data)
{
printf("[+++qgc+++] %s %s allocate heap failed.\n", __FILE__,__func__);
return -1;
}
buf = heap->data;
cetc_init_slist(&heap->list);
for (i=0; i< block_num; i++)
{
node = (tagSListNode *)buf ;
node->next = NULL;
node->data = buf + sizeof(tagSListNode);
cetc_add_slist_tail(&heap->list, node);
buf += block_size + sizeof(tagSListNode);
}
heap->block_num = block_num;
heap->block_size = block_size;
pthread_mutex_init(&heap->lock, NULL);
CETC_DBG_INFO("LEAVE!\n");
return 0;
}
/**
* func : destroy the privivate heap;
* @head : private heap;
* @block_num : block total;
* @block_size : block size;
*/
void cetc_destroy_privheap(tagPrivHeap *heap)
{
CETC_DBG_INFO("Enter!\n");
pthread_mutex_lock(&heap->lock);
free(heap->data);
heap->data = NULL;
heap->block_num = 0;
heap->block_size = 0;
pthread_mutex_unlock(&heap->lock);
CETC_DBG_INFO("LEAVE!\n");
}
/**
* func : allcoate memery from the privivate heap;
* @head : private heap;
* $return : return from private heap block;
*/
void * cetc_alloc_from_privheap(tagPrivHeap *heap)
{
tagSListNode *node;
void *data = NULL;
CETC_DBG_INFO("Enter!\n");
pthread_mutex_lock(&heap->lock);
node = heap->list.head;
if(node != NULL)
{
printf("allocate form private heap\n");
cetc_remove_slist_node(&heap->list, node);
data = node->data;
}
else
{
printf("allocate from os heap by malloc function\n");
data = malloc(heap->block_size);
}
pthread_mutex_unlock(&heap->lock);
CETC_DBG_INFO("LEAVE!\n");
return data;
}
/**
* funct : free block data to private heap;
* @head : private heap;
* @data : that allocate form private heap;
*/
void cetc_free_to_privheap(tagPrivHeap *heap, void *data)
{
tagSListNode *node;
size_t heap_size;
CETC_DBG_INFO("Enter!\n");
heap_size = heap->block_num * (heap->block_size + sizeof(tagSListNode));
if ((data >= heap->data) && (data<heap->data + heap_size))
{
printf("free back to private heap.\n");
pthread_mutex_lock(&heap->lock);
node = (tagSListNode *)(data - sizeof(tagSListNode));
cetc_add_slist_tail(&heap->list, node);
pthread_mutex_unlock(&heap->lock);
}
else
{
printf("free back to os heap by free function\n");
free(data);
}
CETC_DBG_INFO("LEAVE!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -