【C语言】数组和链表结合实现内存池管理

/* 内存池基本操作 */

#define MAX_ID_LEN 24

#pragma pack(1)

typedef struct TAG_INFO
{
	S_Time time;
	uint32_t sys_sec;
	uint8_t fin;
	uint8_t type;
	uint8_t freq;
	uint8_t ant;
	uint8_t rssi;
	uint8_t id[MAX_ID_LEN];
}S_TAG_INFO, *P_TAG_INFO;		

typedef struct TAG_NODE
{
	S_TAG_INFO tag;
	struct TAG_NODE *next;
}S_TAG_NODE, *P_TAG_NODE;

#pragma pack()

#define MAX_TAG_MEMP		10
static S_TAG_NODE tag_memp_tab[MAX_TAG_MEMP] = {0};
static S_TAG_NODE tag_memp_list_head;

void tag_memp_init(void)
{
	uint16_t i;
	P_TAG_NODE cur_node = &tag_memp_list_head;
	
	for(i=0; i<MAX_TAG_MEMP; i++)
	{
		memset(&tag_memp_tab[i].tag, 0, sizeof(S_TAG_INFO));
		tag_memp_tab[i].next = NULL;
		
		cur_node->next = &tag_memp_tab[i];
		cur_node = cur_node->next;
	}
}

P_TAG_NODE tag_memp_malloc(void)
{
	P_TAG_NODE cur_node = &tag_memp_list_head;
	P_TAG_NODE temp = NULL;
	
	if( cur_node->next == NULL )
		return NULL;
	
	temp = cur_node->next;
	cur_node->next = cur_node->next->next;
	temp->next = NULL;
	
	return temp;
}

void tag_memp_free(P_TAG_NODE new_node)
{
	P_TAG_NODE cur_node = &tag_memp_list_head;
	
	if( new_node == NULL )
	{
		LOG_E("野指针");
		return ;
	}
	
	new_node->next = cur_node->next;
	cur_node->next = new_node;
}

/* 链表基本操作:以下代码还未测试!!! */

static uint32_t node_count = 0;

P_TAG_NODE find_node(P_TAG_NODE head, P_TAG_INFO tag)
{
	P_TAG_NODE cur_node = NULL;

	if( head == NULL || tag == NULL )
	{
		LOG_E("野指针");
		return NULL;
	}
	
	cur_node = head->next;
	while( cur_node != NULL )
	{
		if( memcmp(cur_node->tag.id, tag->id, MAX_ID_LEN) == 0)
		{
			return cur_node;
		}
		cur_node = cur_node->next;
	}
	
	return NULL;
}

uint8_t add_node(P_TAG_NODE head, P_TAG_INFO tag)
{
	P_TAG_NODE new_node = NULL;

	if( head == NULL || tag == NULL )
	{
		LOG_E("野指针");
		return 1;
	}

	if( find_node(head, tag) != NULL )
	{
		return 1;
	}
	
	new_node = tag_memp_malloc();
	memcpy(new_node, tag, sizeof(S_TAG_NODE));

	new_node->next = head->next;
	head->next = new_node;
	node_count++;
	LOG_D("新增节点成功,节点数量%d", node_count);
	return 0;
}

void del_node(P_TAG_NODE head, P_TAG_INFO tag)
{
	P_TAG_NODE pre_node = NULL;
	P_TAG_NODE cur_node = NULL;

	if( head == NULL || tag == NULL )
	{
		LOG_E("野指针");
		return ;
	}

	pre_node = head;
	cur_node = head->next;
	while( cur_node != NULL )
	{
		if( memcmp(cur_node->tag.id, tag->id, MAX_ID_LEN) == 0 )
		{
			break;
		}
		pre_node = cur_node;
		cur_node = cur_node->next;
	}
	
	if( cur_node != NULL )
	{
		pre_node->next = cur_node->next;
		tag_memp_free(cur_node);
		node_count--;
		LOG_D("删除节点成功,节点数量%d", node_count);
		return 0;
	}
	else
	{
		return 1;
	}
}

void list_init(P_TAG_NODE head)
{
	memset(head, 0, sizeof(S_TAG_NODE));
	head->next = NULL;
	node_count = 0;
	LOG_D("链表初始化,节点数量%d", node_count);
}

void list_free(P_TAG_NODE head)
{
	P_TAG_NODE temp_node = NULL;
	P_TAG_NODE cur_node = NULL;

	cur_node = head;
	while( cur_node->next != NULL )
	{
		temp_node = cur_node->next;
		cur_node->next = cur_node->next->next;
		tag_memp_free(temp_node);
		node_count--;
		LOG_D("释放节点成功,节点数量%d", node_count);
		cur_node = cur_node->next;
	}
}

#define TAG_INFO_NUM	100
static S_TAG_NODE my_list_head = {0};
static P_TAG_INFO tag_info[TAG_INFO_NUM] = {0};

void main(void)
{
	uint8_t i;
	
	/* 内存池初始化 */
	tag_memp_init();
	
	/* 链表初始化 */
	list_init(&my_list_head);
	
	/* 创建一些标签信息 */
	for(i=0; i<TAG_INFO_NUM; i++)
	{
		memset(&tag_info[i], 0, sizeof(P_TAG_INFO));
		tag_info[i].id[0] = i;
		tag_info[i]->next = NULL;
	}
	
	/* 将标签信息添加进链表 */
	for(i=0; i<TAG_INFO_NUM; i++)
	{
		add_node(&my_list_head, &tag_info[i]);
	}
	
	/* 将标签从链表中删除 */
	for(i=0; i<TAG_INFO_NUM; i++)
	{
		del_node(&my_list_head, &tag_info[i]);
	}

	/* 将标签信息添加进链表 */
	for(i=0; i<TAG_INFO_NUM; i++)
	{
		add_node(&my_list_head, &tag_info[i]);
	}

	/* 释放链表 */
	list_free(&my_list_head);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值