首页| JavaScript| HTML/CSS| Matlab| PHP| Python| Java| C/C++/VC++| C#| ASP| 其他|
购买积分 购买会员 激活码充值

您现在的位置是:虫虫源码 > 其他 > 内存乒乓缓存机制和消息分发机制的C代码实现

内存乒乓缓存机制和消息分发机制的C代码实现

  • 资源大小:0.17M
  • 上传时间:2021-08-16
  • 下载次数:0次
  • 浏览次数:0次
  • 资源积分:1积分
  • 标      签: 一般编程问题

资 源 简 介

用C代码实现乒乓内存缓冲机制,具体实用价值,帮助您提高内存响应速度与及时数据的处理。unsigned long writeunsigned long greadunsigned long overflowST 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*正常返回消息体的指针,异常返回NULLT TWTMSG* generateMsg(tT-TWTMSG* pmSg=nULL,if(NULL =-(ptMsg malloc(sizeof(T TWTMSG)))return NULL;memset(ptMsg, 0, sizeof(T TWTMSG)return pmSg;destroy a Msgvoid destroy Msg(t TWTMSG ptMsgif(NULL ptMsg->pfDestroyMsg)pt Msg->pfDestroy Msg(ptMsg->pvMsg)if (NULL != pt Msg)free(pmSgfree a Msg Queuevoid freeTWTMsg Que(T TWTMSG QUEUE* ptMsgQif(NULL =ptMsg Afree(ptMsg Q);Init a Msg QT TWTMSG QUEUE* initTWTMsg QueoT TWTMSG QUEUE pmSg Q= NULlif (NULL ==(ptMsgQ malloc(sizeof(T_ TWTMSG QUeue)goto ErrRetmemset(ptMsgQ, 0, sizeof(T TWTMSG QUEUE))return pmSg Q;Errretprintf( initTWTMsg Que Fail! ")freeTWTMsgQue(ptMsg Q)return nullPop a pvMsg packet from a msg packet queues param g is the packet queue from which to pop the pbuf@return pointer to pvMsg packet if available, NULl otherwiseT TWTMSG* TWTMsg Get(T_ TWTMSG QUEUE aT TWTMSG*//*加锁if(TWT_ QUEUE_ EMPTY(a))iReturn a NUll pointer if the queue is emptypmSg=NULL;else is The queue is not empty so return the next frame from itand adjust the read pointer accordinglypmSg=g->pvMsglq >greadg->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 fullnt TWTMsg Send(T tWTMSg*p, T TWTMSG QUEUE *qint ret/if(!TWT_ QUEUE FULL(al)iThe queue isn t full so we add the new frame at the currentw write position and move the write pointer.g->pvMsgla->write]=pg- >write =((q->qwrite+1)% TIMEWEIGHT TASKQUEUE SIZE;ret =oThe stack is full so we are throwing away this value. Keep trackof the number of times this happensg->overflow++ret =-1://*解锁return(ret)**米**米来米***来米*半米*米*半米*米求***半*米米求半**米求半**半求半和*米*//消息分发机制//*算法是//*正常返回0,出错返回-1水米米******水*米*水**米*半*水米米冰半**水水*水米米半米冰水*米水水*水*米水水extern int RecToFile MsgProc(T_ MSG_ REC2 FILE* ptMsg);int DispatchMsg(T TWTMSG *ptMsgitif(NULl = ptMsg goto ErrRet/*dispatch msg*/switch(ptMsg->en Msg Type)case TWT PINgPoNgBuff recRecTo File Msg Procl(TMSG_REC2FE) (pmSg->pMsg);/*处理消息*/destroy Msg( pmSg;/*消毁消息breakdefault.printf("Dispatch Msg Msgtype Error! n")break.return 0ErrRetprintf("Dispatch Msg Fail! ";return-1./*buff size*/#defineP| NGPONG_ BUFF BSIZE0X20000//10*1024*1024/*10M*/*ping pong buff*///chargacPINGBUFF[PINGPONG BUFF BSIZE]; /* Ping Buff*///chargacPONGBUFF[PINGPONG_ BUFF BSIZE] *Pong Buff*/水米米*********米*水**米*半*水米米水**冰水*水米米半半水半米冰水*米水水*水*米米//*释放 ping pong buff/必然成功//*无返回木***木*水****本**水*水水*水****本水**水水****水水***本***米*水void Destroy Ping Pong Butt(T PINGPONGBUFF USE DES ptPing Pong Butt)nLoopif (NULL = pt Ping pong buffreturnfor (nLoop=0; nLooptIng 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 BuffSizeT PINGPONGBUFF USE DES* ptBuffDes=NULLintnLoop/*获取buf描述*if NULL==(ptBuffDes=malloc(sizeof(T PINGPONGBUFF USE DES))))goto ErrRetmemset(pt BuffDes, 0, sizeof(T_PINGPONGBUFF USE_ DES));/*分别初始化ping和pong*/for(nLoop=0; nLooptIng BuffUsenLoop pcHeadAddr =malloc n BuffSize))goto Err Ret;ptBuffDes->tIng BuffUselnLoop] nBuffSize nBuffsizeptBuffDes->tPing BuffUse[nLoop]. oFfsetptBuffDes->tPing BuffUsenLoop) eUseStatus-=BUFF WRITEABLE;pt BuffDes->eCurUseIDBUFF PINGreturn pt BuffDesErrretprintf("lnitPing Pong Buff Fail!");DestroyIng Pong Buff(pt BuffDes)turn null平**米**米*米***来米米*米*米*半米*米米米来*半米平**米米求*来*半求半来*米求*和*米*/*Reset ping pong buff//*正常返回0,出错返回-1米米米米水冰米*米米水**米米冰*米水米米米米水米水*水米米来米米x米来米米水冰来来宋来水米来米来冰#define ResetBuffUse(ptBuffuse)pt BuffUse->oFfset0pt BuffUse->eUseStatus BUFF WRITEABLEgenerate a file rec msg*正常返回消息体的指针,异常返回NULLT_MSG_REC2lGK SenFRMSB(T_BUFF_USE_DES *ptBuffUse, REC_FILE_DESLIST *ptFileListRT MSG REC2FILE KE* ptRFMsg= NULL;if(NULL ==(ptRFMsg malloc(sizeof(T_ MSG REC2 FILE)return nUllptRFMsg- >pt BuffUse pt BuffUseptRFMsg->pt Filelist ptFilelist;return ptRFMsg
VIP VIP
0.183090s