实例介绍
用C代码实现乒乓内存缓冲机制,具体实用价值,帮助您提高内存响应速度与及时数据的处理。
unsigned long write unsigned long gread unsigned long overflow ST TWTMSG QUEUE: / Helper macros for accessing Msg queues. * #define tWt QUEUE EMPty(a)\ (((g->write==(q)->gread)? 1: 0) #define twt_ QUeUe full(a)\ (((((q)->qwrite +1% TIMEWEIGHT TASKQUEUE SIZED)==(q)->gread)?1: 0) 米 generate a Msg entity *正常返回消息体的指针,异常返回NULL T TWTMSG* generateMsg(t T-TWTMSG* pmSg=nULL, if(NULL =-(ptMsg malloc(sizeof(T TWTMSG)))return NULL; memset(ptMsg, 0, sizeof(T TWTMSG) return pmSg; destroy a Msg void destroy Msg(t TWTMSG ptMsg if(NULL ptMsg->pfDestroyMsg)pt Msg->pfDestroy Msg(ptMsg->pvMsg) if (NULL != pt Msg)free(pmSg free a Msg Queue void freeTWTMsg Que(T TWTMSG QUEUE* ptMsgQ if(NULL =ptMsg Afree(ptMsg Q); Init a Msg Q T TWTMSG QUEUE* initTWTMsg Queo T TWTMSG QUEUE pmSg Q= NULl if (NULL ==(ptMsgQ malloc(sizeof(T_ TWTMSG QUeue)goto ErrRet memset(ptMsgQ, 0, sizeof(T TWTMSG QUEUE)) return pmSg Q; Errret printf( initTWTMsg Que Fail! \n") freeTWTMsgQue(ptMsg Q) return null Pop a pvMsg packet from a msg packet queue s param g is the packet queue from which to pop the pbuf @return pointer to pvMsg packet if available, NULl otherwise T TWTMSG* TWTMsg Get(T_ TWTMSG QUEUE a T TWTMSG* //*加锁 if(TWT_ QUEUE_ EMPTY(a))i Return a NUll pointer if the queue is empty pmSg=NULL; else i s The queue is not empty so return the next frame from it and adjust the read pointer accordingly pmSg=g->pvMsglq >gread g->gread =((q->gread +1)% TIMEWEIGHT_TASKQUEUE_ SIZE) /*解锁 return(pmSg); Push a pmSg packet onto a pvMsg packet queue @param p is the pmsg to push onto the packet queue @param g is the packet queue. W @return 0 if successful, -1 if q is full nt TWTMsg Send(T tWTMSg*p, T TWTMSG QUEUE *q int ret / if(!TWT_ QUEUE FULL(al)i The queue isn t full so we add the new frame at the current w write position and move the write pointer. g->pvMsgla->write]=p g- >write =((q->qwrite+1)% TIMEWEIGHT TASKQUEUE SIZE; ret =o The stack is full so we are throwing away this value. Keep track of the number of times this happens g->overflow++ ret =-1: //*解锁 return(ret) **米**米来米***来米*半米*米*半米*米求***半*米米求半**米求半**半求半和*米* //消息分发机制 //*算法是 //*正常返回0,出错返回-1 水米米******水*米*水**米*半*水米米冰半**水水*水米米半米冰水*米水水*水*米水水 extern int RecToFile MsgProc(T_ MSG_ REC2 FILE* ptMsg); int DispatchMsg(T TWTMSG *ptMsgit if(NULl = ptMsg goto ErrRet /*dispatch msg*/ switch(ptMsg->en Msg Type) case TWT PINgPoNgBuff rec RecTo File Msg Procl(TMSG_REC2FE) (pmSg->pMsg);/*处理消息*/ destroy Msg( pmSg;/*消毁消息 break default. printf("Dispatch Msg Msgtype Error! n") break. return 0 ErrRet printf("Dispatch Msg Fail! \n"; return-1. /*buff size*/ #define P| NGPONG_ BUFF BSIZE0X20000//10*1024*1024/*10M* /*ping pong buff*/ //char gacPINGBUFF[PINGPONG BUFF BSIZE]; /* Ping Buff*/ // char gacPONGBUFF[PINGPONG_ BUFF BSIZE] *Pong Buff*/ 水米米*********米*水**米*半*水米米水**冰水*水米米半半水半米冰水*米水水*水*米米 //*释放 ping pong buff /必然成功 //*无返回 木***木*水****本**水*水水*水****本水**水水****水水***本***米*水 void Destroy Ping Pong Butt(T PINGPONGBUFF USE DES ptPing Pong Butt) nLoop if (NULL = pt Ping pong buffreturn for (nLoop=0; nLoop<BUFF PIPONUM; nLoop++) if (NULL ptPingPong Buff->tIng BuffUse[nLoop]. pcHeadAddr)free(ptPing Pong ->t Ping BuffUse[nLoop]. pcHeadAddr) free(pt Ping pong Buff) /初始化 ping pong buff 返回 pign pong buff的描述指针 //*正常返回0,出错返回-1 水水水水水水水水水木水木水水水水水木水木水水水水水水水水本水水水水水水水水水本水水水水水水水水水水水水水 T PINGPONGBUFF USE DES InitPing Pong Buff(unsigned int n BuffSize T PINGPONGBUFF USE DES* ptBuffDes=NULL int nLoop /*获取buf描述* if NULL==(ptBuffDes=malloc(sizeof(T PINGPONGBUFF USE DES))))goto ErrRet memset(pt BuffDes, 0, sizeof(T_PINGPONGBUFF USE_ DES)); /*分别初始化ping和pong*/ for(nLoop=0; nLoop<BUFF PIPONUM; nLoop++) if (NULl ==(pt BuffDes->tIng BuffUsenLoop pcHeadAddr =malloc n BuffSize))goto Err Ret; ptBuffDes->tIng BuffUselnLoop] nBuffSize nBuffsize ptBuffDes->tPing BuffUse[nLoop]. oFfset ptBuffDes->tPing BuffUsenLoop) eUseStatus-=BUFF WRITEABLE; pt BuffDes->eCurUseID BUFF PING return pt BuffDes Errret printf("lnitPing Pong Buff Fail!\n"); DestroyIng Pong Buff(pt BuffDes) turn null 平**米**米*米***来米米*米*米*半米*米米米来*半米平**米米求*来*半求半来*米求*和*米* /*Reset ping pong buff //*正常返回0,出错返回-1 米米米米水冰米*米米水**米米冰*米水米米米米水米水*水米米来米米x米来米米水冰来来宋来水米来米来冰 #define ResetBuffUse(ptBuffuse) pt BuffUse->oFfset 0 pt BuffUse->eUseStatus BUFF WRITEABLE generate a file rec msg *正常返回消息体的指针,异常返回NULL T_MSG_REC2lGK SenFRMSB(T_BUFF_USE_DES *ptBuffUse, REC_FILE_DESLIST *ptFileListR T MSG REC2FILE K E* ptRFMsg= NULL; if(NULL ==(ptRFMsg malloc(sizeof(T_ MSG REC2 FILE)return nUll ptRFMsg- >pt BuffUse pt BuffUse ptRFMsg->pt Filelist ptFilelist; return ptRFMsg 【实例截图】
【核心代码】
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论