pvMsglq>greadg->gread=((q->gread+1)%TIMEWEIGHT_TASKQUEUE_SIZE)/*解锁return(pmSg);PushapmSgpacketontoapvMsgpacketqueue@parampisthepmsgtopushontothepacketqueue@paramgisthepacketqueue.W@return0ifsuccessful,-1ifqisfullntTWTMsgSend(TtWTMSg*p,TTWTMSGQUEUE*qintret/if(!TWT_QUEUEFULL(al)iThequeueisntfullsoweaddthenewframeatthecurrentwwritepositionandmovethewritepointer.g->pvMsgla->write]=pg->write=((q->qwrite+1)%TIMEWEIGHTTASKQUEUESIZE;ret=oThestackisfullsowearethrowingawaythisvalue.Keeptrackofthenumberoftimesthishappensg->overflow++ret=-1://*解锁return(ret)**米**米来米***来米*半米*米*半米*米求***半*米米求半**米求半**半求半和*米*//消息分发机制//*算法是//*正常返回0,出错返回-1水米米******水*米*水**米*半*水米米冰半**水水*水米米半米冰水*米水水*水*米水水externintRecToFileMsgProc(T_MSG_REC2FILE*ptMsg);intDispatchMsg(TTWTMSG*ptMsgitif(NULl=ptMsggotoErrRet/*dispatchmsg*/switch(ptMsg->enMsgType)caseTWTPINgPoNgBuffrecRecToFileMsgProcl(TMSG_REC2FE)(pmSg->pMsg);/*处理消息*/destroyMsg(pmSg;/*消毁消息breakdefault.printf("DispatchMsgMsgtypeError!n")break.return0ErrRetprintf("DispatchMsgFail!";return-1./*buffsize*/#defineP|NGPONG_BUFFBSIZE0X20000//10*1024*1024/*10M*/*pingpongbuff*///chargacPINGBUFF[PINGPONGBUFFBSIZE];/*PingBuff*///chargacPONGBUFF[PINGPONG_BUFFBSIZE]*PongBuff*/水米米*********米*水**米*半*水米米水**冰水*水米米半半水半米冰水*米水水*水*米米//*释放pingpongbuff/必然成功//*无返回木***木*水****本**水*水水*水****本水**水水****水水***本***米*水voidDestroyPingPongButt(TPINGPONGBUFFUSEDESptPingPongButt)nLoopif(NULL=ptPingpongbuffreturnfor(nLoop=0;nLooptIngBuffUse[nLoop].pcHeadAddr)free(ptPingPong->tPingBuffUse[nLoop].pcHeadAddr)free(ptPingpongBuff)/初始化pingpongbuff返回pignpongbuff的描述指针//*正常返回0,出错返回-1水水水水水水水水水木水木水水水水水木水木水水水水水水水水本水水水水水水水水水本水水水水水水水水水水水水水TPINGPONGBUFFUSEDESInitPingPongBuff(unsignedintnBuffSizeTPINGPONGBUFFUSEDES*ptBuffDes=NULLintnLoop/*获取buf描述*ifNULL==(ptBuffDes=malloc(sizeof(TPINGPONGBUFFUSEDES))))gotoErrRetmemset(ptBuffDes,0,sizeof(T_PINGPONGBUFFUSE_DES));/*分别初始化ping和pong*/for(nLoop=0;nLooptIngBuffUsenLooppcHeadAddr=mallocnBuffSize))gotoErrRet;ptBuffDes->tIngBuffUselnLoop]nBuffSizenBuffsizeptBuffDes->tPingBuffUse[nLoop].oFfsetptBuffDes->tPingBuffUsenLoop)eUseStatus-=BUFFWRITEABLE;ptBuffDes->eCurUseIDBUFFPINGreturnptBuffDesErrretprintf("lnitPingPongBuffFail!");DestroyIngPongBuff(ptBuffDes)turnnull平**米**米*米***来米米*米*米*半米*米米米来*半米平**米米求*来*半求半来*米求*和*米*/*Resetpingpongbuff//*正常返回0,出错返回-1米米米米水冰米*米米水**米米冰*米水米米米米水米水*水米米来米米x米来米米水冰来来宋来水米来米来冰#defineResetBuffUse(ptBuffuse)ptBuffUse->oFfset0ptBuffUse->eUseStatusBUFFWRITEABLEgenerateafilerecmsg*正常返回消息体的指针,异常返回NULLT_MSG_REC2lGKSenFRMSB(T_BUFF_USE_DES*ptBuffUse,REC_FILE_DESLIST*ptFileListRTMSGREC2FILEKE*ptRFMsg=NULL;if(NULL==(ptRFMsgmalloc(sizeof(T_MSGREC2FILE)returnnUllptRFMsg->ptBuffUseptBuffUseptRFMsg->ptFilelistptFilelist;returnptRFMsg-IMDN开发者社群-imdn.cn"> pvMsglq>greadg->gread=((q->gread+1)%TIMEWEIGHT_TASKQUEUE_SIZE)/*解锁return(pmSg);PushapmSgpacketontoapvMsgpacketqueue@parampisthepmsgtopushontothepacketqueue@paramgisthepacketqueue.W@return0ifsuccessful,-1ifqisfullntTWTMsgSend(TtWTMSg*p,TTWTMSGQUEUE*qintret/if(!TWT_QUEUEFULL(al)iThequeueisntfullsoweaddthenewframeatthecurrentwwritepositionandmovethewritepointer.g->pvMsgla->write]=pg->write=((q->qwrite+1)%TIMEWEIGHTTASKQUEUESIZE;ret=oThestackisfullsowearethrowingawaythisvalue.Keeptrackofthenumberoftimesthishappensg->overflow++ret=-1://*解锁return(ret)**米**米来米***来米*半米*米*半米*米求***半*米米求半**米求半**半求半和*米*//消息分发机制//*算法是//*正常返回0,出错返回-1水米米******水*米*水**米*半*水米米冰半**水水*水米米半米冰水*米水水*水*米水水externintRecToFileMsgProc(T_MSG_REC2FILE*ptMsg);intDispatchMsg(TTWTMSG*ptMsgitif(NULl=ptMsggotoErrRet/*dispatchmsg*/switch(ptMsg->enMsgType)caseTWTPINgPoNgBuffrecRecToFileMsgProcl(TMSG_REC2FE)(pmSg->pMsg);/*处理消息*/destroyMsg(pmSg;/*消毁消息breakdefault.printf("DispatchMsgMsgtypeError!n")break.return0ErrRetprintf("DispatchMsgFail!";return-1./*buffsize*/#defineP|NGPONG_BUFFBSIZE0X20000//10*1024*1024/*10M*/*pingpongbuff*///chargacPINGBUFF[PINGPONGBUFFBSIZE];/*PingBuff*///chargacPONGBUFF[PINGPONG_BUFFBSIZE]*PongBuff*/水米米*********米*水**米*半*水米米水**冰水*水米米半半水半米冰水*米水水*水*米米//*释放pingpongbuff/必然成功//*无返回木***木*水****本**水*水水*水****本水**水水****水水***本***米*水voidDestroyPingPongButt(TPINGPONGBUFFUSEDESptPingPongButt)nLoopif(NULL=ptPingpongbuffreturnfor(nLoop=0;nLooptIngBuffUse[nLoop].pcHeadAddr)free(ptPingPong->tPingBuffUse[nLoop].pcHeadAddr)free(ptPingpongBuff)/初始化pingpongbuff返回pignpongbuff的描述指针//*正常返回0,出错返回-1水水水水水水水水水木水木水水水水水木水木水水水水水水水水本水水水水水水水水水本水水水水水水水水水水水水水TPINGPONGBUFFUSEDESInitPingPongBuff(unsignedintnBuffSizeTPINGPONGBUFFUSEDES*ptBuffDes=NULLintnLoop/*获取buf描述*ifNULL==(ptBuffDes=malloc(sizeof(TPINGPONGBUFFUSEDES))))gotoErrRetmemset(ptBuffDes,0,sizeof(T_PINGPONGBUFFUSE_DES));/*分别初始化ping和pong*/for(nLoop=0;nLooptIngBuffUsenLooppcHeadAddr=mallocnBuffSize))gotoErrRet;ptBuffDes->tIngBuffUselnLoop]nBuffSizenBuffsizeptBuffDes->tPingBuffUse[nLoop].oFfsetptBuffDes->tPingBuffUsenLoop)eUseStatus-=BUFFWRITEABLE;ptBuffDes->eCurUseIDBUFFPINGreturnptBuffDesErrretprintf("lnitPingPongBuffFail!");DestroyIngPongBuff(ptBuffDes)turnnull平**米**米*米***来米米*米*米*半米*米米米来*半米平**米米求*来*半求半来*米求*和*米*/*Resetpingpongbuff//*正常返回0,出错返回-1米米米米水冰米*米米水**米米冰*米水米米米米水米水*水米米来米米x米来米米水冰来来宋来水米来米来冰#defineResetBuffUse(ptBuffuse)ptBuffUse->oFfset0ptBuffUse->eUseStatusBUFFWRITEABLEgenerateafilerecmsg*正常返回消息体的指针,异常返回NULLT_MSG_REC2lGKSenFRMSB(T_BUFF_USE_DES*ptBuffUse,REC_FILE_DESLIST*ptFileListRTMSGREC2FILEKE*ptRFMsg=NULL;if(NULL==(ptRFMsgmalloc(sizeof(T_MSGREC2FILE)returnnUllptRFMsg->ptBuffUseptBuffUseptRFMsg->ptFilelistptFilelist;returnptRFMsg - IMDN开发者社群-imdn.cn">
登录
首页 » Others » 内存乒乓缓存机制和消息分发机制的C代码实现

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

于 2020-12-03 发布
0 243
下载积分: 1 下载次数: 0

代码说明:

用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

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • morlet小波分析matlab
    morlet小波分析matlab程序,用于气象数据统计与分析
    2021-05-07下载
    积分:1
  • ieee 33节点
    IEEE 33节点的潮流计算图 是配电网的 可用于课程设计
    2020-11-03下载
    积分:1
  • mimo 优化功率分配
    mimo 系统 等功率分配与优化功率分配算法比较。代码可以执行。
    2020-11-30下载
    积分:1
  • Essential C++源代码
    Essential C++源代码,包括书内每章节案例源码及练习题答案源码。目录结构清晰,便于学习。附pdf版《Essential C++》
    2020-12-10下载
    积分:1
  • 空间插值算法在GIS专业中的应用
    本文档为GIS原理与算法结课论文,文中介绍了几种常用的空间插值模型的原理与方法,并通过规则格网DEM的建立,说明空间插值算法在GIS中的应用。
    2020-11-30下载
    积分:1
  • fluent网格划分的关键.pdf
    fluent 中自适应网格完善特征允许我们根据数据计算结果来修改网格梳密布置或网格走向。另外,fluent 还提供了创建和查看特定应用的自适应方法。下面各部分详细描述了自适应的过程。23.1,自适应的使用23.2,自适应过程23.3,边界自适应23.4,倾斜度自适应23.5,等值线的自适应23.6,区域自适应23.7,体积自适应23.8, y 和-y 的自适应23.9,管理自适应注册23.10,自适应控制23.11,用修匀和交换方法来改善网格
    2020-12-10下载
    积分:1
  • 完整的IOCP实例
    应该是网上最完成的IOCP实例吧。NT6线程池,VS2010。
    2021-05-06下载
    积分:1
  • 数字全息图再现
    用菲涅尔方法实现数字全息图的再现,简单易懂,有注释。
    2020-12-06下载
    积分:1
  • MQTT服务器搭建教
    本教程为本地MQTT服务器搭建,搭建此本地MQTT服务器必须先为电脑配置java 环境,然后用CMD命令按步骤运行apache-apollo-1.7.1下相关exe搭建服务器管烨员:C: Windowssystem32 cmd.exe215/1/291:55example#215/1/2916:55ed hD: vapache-apo1lo-1.7.1 in >ddr卷是软件卷的序列号是FAD4418D: Mapache- apollo-1.7.1hin的目录E17/81421:4 d1r卷是软件卷的序列号是FAD4418D: pache-apo11o-1.71 bin roker in的目录E17/g81421:41dir驱动器卷是软件卷的序列号是FD4-418D:< apache-apo1lo-1.71 bin Broker in的目录217/981421:41apoIlo-broker run红框内第一行是监听,监听端口是,客服端链接时输入电脑的地址即可,本机可使用命令查询。绿框内为控制台地址,在浏览器输入可现实控制台界面。c管埠员: CAWindows system32 cmd.exe- apollo-broker run一巴回[9Gv[37ml∈[32mc[37 mApache Apo11a←[2mINFO i AyulINFo Starting store: leveldb store at D: apac he-apo1lo-1-7.1 in Nmybraker vdatMARN Using the pure java Leve lDB implementation which is still experimentalIf the NI version is not available for your platform, please switch to the BDBstoreinsteadhttp://activemg.apache.org/appllo/docunentation/user-manual.htmlNro I Accepting connections at: tcp://6.5.6.G:G1G1JNFO白 ccepting connect1 ons at:t1s:/.回,.日=6⊥614NFo Accepting connections at: us://0.0.4.2=61623/Fo Accepting connections at: uss://.0.6-G:61624INFOvirtual host startup is wait ing on store startupINFO i virtual host startup is no longer waiting- It waited a total of 1 seconNfoAdministrationinterfaceavailableat:https://127.0.0.1:61681/INFOAdministrat iun interface dudildlle at: htLp://1270-0-1:616808.用户名默认: admin,密码: password←C▲不安全p127.0.0.161681/ onsale/in dex. hlIIIApollo ConsoleogInUsernamePassward5控制台界面Apache Apollo -AntCA承我全琳低!1n(1h1 ronnie/nrf=hm匈☆Apollo ConsoleadminVirtual Hosts Connectors operat ng Ervironment ConfiguratioVirtual Host: mybrcke-ETAIL5ueues Topics Durable SubsSince:2017//5下午10:A0Name: nomcCrcaHostNo Queues have been created yet.STORE STATUS
    2021-05-06下载
    积分:1
  • 北京昆仑通态组态软件MCGS高级教
    经过培训,您可以熟练应用MCGS组态软件中的各项功能;能根据工艺现场要求熟练制作动画流程;能处理相对复杂的曲线、报表、数据后处理等需求;能熟练使用MCGS脚本程序进行复杂的工业流程控制。
    2020-07-03下载
    积分:1
  • 696518资源总数
  • 106155会员总数
  • 8今日下载