博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C工具库10:带引用计数的buffer
阅读量:5818 次
发布时间:2019-06-18

本文共 2684 字,大约阅读时间需要 8 分钟。

前面写了一系列文章都是关于网络底层机制封装的,后面要慢慢的将这些机制组装起来,实现一套可用的网络库。

既然是网络库,自然少不了对缓存的管理,本文实现一个带引用计数的buffer,作为packet的底层缓存组件.packet

的具体设计思路可见:

#ifndef _BUFFER_H#define _BUFFER_H/*        Copyright (C) <2012>  
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see
.*//** 带引用计数的buffer*/typedef struct buffer{ long ref_count; unsigned long capacity; unsigned long size; struct buffer *next; char buf[0];}*buffer_t;buffer_t buffer_create_and_acquire(buffer_t,unsigned long);buffer_t buffer_acquire(buffer_t,buffer_t);void buffer_release(buffer_t*);#endif
#include 
#include
#include "buffer.h"static buffer_t buffer_create(unsigned long capacity){ unsigned long size = sizeof(struct buffer) + capacity; buffer_t b = calloc(1,size); if(b) { b->ref_count = 0; b->size = 0; b->capacity = capacity; } return b;}static void buffer_destroy(buffer_t *b){ printf("buffer destroy\n"); if((*b)->next) buffer_release(&(*b)->next); free(*b); *b = 0;}buffer_t buffer_create_and_acquire(buffer_t b,unsigned long capacity){ buffer_t nb = buffer_create(capacity); return buffer_acquire(b,nb);}buffer_t buffer_acquire(buffer_t b1,buffer_t b2){ if(b1 == b2) return b1; if(b2) ++b2->ref_count; if(b1) buffer_release(&b1); return b2;}void buffer_release(buffer_t *b){ if(--(*b)->ref_count <= 0) buffer_destroy(b); *b = 0;}

测试用例如下:

buffer_t cur = 0;    buffer_t b1 = buffer_create_and_acquire(0,16);    buffer_t b2 = buffer_acquire(0,b1);    buffer_release(&b1);    buffer_release(&b2);    b1 = buffer_create_and_acquire(0,16); //b1指向bufferA    b1 = buffer_create_and_acquire(b1,16);//b1指向bufferB,bufferA计数减为0,被释放    buffer_release(&b1);    //创建3个buffer形成链表    b1 = buffer_create_and_acquire(0,16);    b1->next = buffer_create_and_acquire(0,16);    b1->next->next = buffer_create_and_acquire(0,16);    cur = b1;    while(cur)    {        cur = buffer_acquire(cur,cur->next);    }    //遍历结束,所有buffer的计数减为0,全部被释放

转载于:https://www.cnblogs.com/sniperHW/archive/2012/05/10/2493876.html

你可能感兴趣的文章
Virtual Machine Manager 2012 SP1部署过程
查看>>
梭子鱼邮件归档设备配置
查看>>
nginx多站点,ssl偏爱第一个证书
查看>>
《价值50亿的10句话》读后感(学生作业分享)
查看>>
Ponemon:优化SIEM时所面临的挑战
查看>>
听比喻,懂原理(1)超五类双绞线和六类双绞线的区别
查看>>
HTML 5实现的手机摇一摇
查看>>
NLB单播和多播区别
查看>>
轻松应对IDC机房带宽突然暴涨问题
查看>>
Skype for Business Server 2015-07-边缘服务器-2-设计拓朴
查看>>
轻量级HTTP服务器Nginx(配置与调试Nginx)
查看>>
分享Silverlight/WPF/Windows Phone一周学习导读(10月22日-10月29日)
查看>>
2013Q1财报:谁在“补贴”苹果?
查看>>
Lync server 2013数据库镜像报错及解决
查看>>
颠覆与重构——戴尔助力徐工集团等行业客户实现业务转型
查看>>
poj3176
查看>>
[转载]Android布局文件中命名空间的解析
查看>>
超越极限编程 - 视频分享第8弹!
查看>>
ZOJ-2770 Burn the Linked Camp 差分约束
查看>>
为Visual Studio添加配色方案
查看>>