bool tSafeStrcpy( LibTest & tester ) { // SafeStrcpy( char *, const char *, size_t ) char b[ 15 ]; char tBuffer[ 128 ]; memset( tBuffer, '*', sizeof( tBuffer ) ); SafeStrcpy( tBuffer, "short", sizeof( b ) ); TEST( ! strcmp( tBuffer, "short" ) ); TEST( tBuffer[ 14 ] == 0 ); TEST( tBuffer[ 15 ] == '*' ); memset( tBuffer, '*', sizeof( tBuffer ) ); SafeStrcpy( tBuffer, "123456789 123456789", sizeof( b ) ); TEST( ! strcmp( tBuffer, "123456789 1234" ) ); TEST( tBuffer[ 14 ] == 0 ); TEST( tBuffer[ 15 ] == '*' ); memset( tBuffer, '*', sizeof( tBuffer ) ); SafeStrcpy( tBuffer, "short", min( strlen( "short" ) + 1, sizeof( b ) ) ); TEST( ! strcmp( tBuffer, "short" ) ); TEST( tBuffer[ 5 ] == 0 ); TEST( tBuffer[ 6 ] == '*' ); return( true ); }
void CSocketRegister::Add(WIN_LINUX_SOCKET s,char *szInfo) { int i=0; m_Lock.Lock(); { //统计最大的socket if(!SocketIsOK(m_nMaxSocket)) m_nMaxSocket=s; else if(s>m_nMaxSocket) m_nMaxSocket=s; //先试图修改 for(i=0;i<m_nUseMax;i++) { if(m_RegisterArray[i].m_nSocket==s) { if(szInfo) SafeStrcpy(m_RegisterArray[i].m_szInfo,szInfo,NEO_MEM_BLOCK_INFO_MAX_SIZE); //修改不增加 goto CSocketRegister_Add_End; } }//for //再试图插入,寻找其中空闲的socket for(i=0;i<m_nUseMax;i++) { if(!SocketIsOK(m_nMaxSocket)) { m_RegisterArray[i].m_nSocket=s; if(szInfo) SafeStrcpy(m_RegisterArray[i].m_szInfo,szInfo,NEO_MEM_BLOCK_INFO_MAX_SIZE); m_nSocketUseCount++; goto CSocketRegister_Add_End; } }//for //最后追加到末尾 if(NEO_MEM_REGISTER_MAX>m_nUseMax) { m_RegisterArray[i].m_nSocket=s; if(szInfo) SafeStrcpy(m_RegisterArray[i].m_szInfo,szInfo,NEO_MEM_BLOCK_INFO_MAX_SIZE); m_nUseMax++; m_nSocketUseCount++; } else//注册区满了,必须要扩大缓存 { m_pDebug->DebugToFile("CSocketRegister ::Add():Pool is full!\n"); } } CSocketRegister_Add_End: m_Lock.UnLock(); }
//内部工具,拷贝数据 void CMemRegister::RegisterCopy(SNEOMemRegister *pDest//目的拷贝指针 ,void *pPoint //待拷贝的指针 ,const char *szInfo) //待拷贝的描述 { pDest->m_pPoint=pPoint; if(szInfo) { //引入的函数 SafeStrcpy(pDest->m_szInfo,szInfo,NEO_MEM_BLOCK_INFO_MAX_SIZE); } else { NEO_CLEAN_CHAR_BUFFER(szInfo); } }
////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// //动态内存分配,队列采用列表的(依赖内存池) CNEOMemQueue::CNEOMemQueue(CNEOLowDebug *pDebug,CNEOMemPoolWithLock *pMemPool, const char *szAppName, // 应用程序名,代表队列名 int nMaxToken //最大token上限 ):m_nMaxToken(nMaxToken),m_nTokenCount(0), m_pHead(NULL),m_pLast(NULL), m_pDebug(pDebug),m_pMemPool(pMemPool) { //m_nMaxToken=nMaxToken; //m_pLast=NULL; //m_pDebug=pDebug; //m_pMemPool=pMemPool; SafeStrcpy(m_szAppName,szAppName,NEO_APPLICATION_NAME_SIZE); //m_pHead=NULL; //m_nTokenCount=0; }
//带安全锁的类 CNEOMemQueueWithLock::CNEOMemQueueWithLock(CNEOLowDebug *pDebug, CNEOMemPoolWithLock *pMemPool, const char *szAppName, // 应用程序名,代表队列名 int nMaxToken ) { SafeStrcpy(m_szAppName,szAppName,NEO_APPLICATION_NAME_SIZE); m_pMemPool=pMemPool; m_pQueue=new CNEOMemQueue(pDebug,pMemPool,m_szAppName,nMaxToken); if(m_pQueue) { char szNameBuffer[256]; SafePrintf(szNameBuffer,256,"%s::m_pQueue",m_szAppName); m_pMemPool->Register(m_pQueue,szNameBuffer); } }
/* Initializes widget instance * * Parameters: * request - requested widget * w - the widget * args - arguments * num_args - number of arguments * */ static void initialize(Widget request, Widget w, ArgList args, Cardinal *num_args) { CtrlWidget cw = (CtrlWidget)w; CtrlPart *c = & cw->ctrl; // Display *dpy = XtDisplay(w); int i; Dimension width,height; TF(); c->draw = 0; CtrlWidget cw1 = (CtrlWidget)request; CtrlPart *c1 = & cw1->ctrl; // foreach state for(i=0;i<3;i++) { c->gc[i] = get_gc( w, c->pixel[3+i] ); alloc_xft_color( w, c->pixel[i], c->col+i ); } // foreach label for(i=0;i<MAX_CLABEL;i++) { Change_Font(w, i, c->fontnames[i] ); } c->msg.state = STATE_NORMAL; c->msg.cmd = MSG_UPDATE; c->msg.value = 0; if( IsEmpty( c->str[TEXT] )) c->str[TEXT] = cw->core.name; for(i=0;i<MAX_CLABEL;i++) { SafeStrcpy( c->clabel[i].str, c->str[i], MAX_CLABEL_STR ); c->clabel[i].position=0; } c->clabel[TEXT].position=1; calc_widget_size(w, &width,&height ); if (cw->core.height == 0) cw->core.height = height; if (cw->core.width == 0) cw->core.width = width; // (*XtClass(w)->superclass->core_class.resize)(w); }
JCHAR* JEvent::Serialize(JUINT32 *uiLen) { JUINT32 uiAllocLen = 0; JUINT32 uiLeaveLen = 0; JCHAR* pBuf = JNULL; JCHAR* pTmpBuf = JNULL; JEVT_TYPE* pEvtType = JNULL; JUINT32* pLen = JNULL; JUINT32* pBodyLen = JNULL; JLogAutoPtr clsLogAutoPtr(JSingleton<JLog>::instance(), JLOG_MOD_EVENT, "JEvent::Serialize"); //calcualte the total event message length uiAllocLen += sizeof(JEVT_TYPE); //m_strFromProc size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strFromProc.GetLength(); //m_strFromThrd size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strFromThrd.GetLength(); //m_strFromMod size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strFromMod.GetLength(); //m_strToProc size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strToProc.GetLength(); //m_strToThrd size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strToThrd.GetLength(); //m_strToMod size uiAllocLen += sizeof(JUINT32); uiAllocLen += m_strToMod.GetLength(); //m_pBody size uiAllocLen += sizeof(JUINT32); if (m_pBody) { uiAllocLen += m_pBody->GetLength(); } pBuf = JSingleton<JStaticMemory>::instance()->Alloc(uiAllocLen+1); if (pBuf) { SafeMemset(pBuf, 0, uiAllocLen+1); pTmpBuf = pBuf; uiLeaveLen = uiAllocLen+1; //store the event type into the buffer pEvtType = reinterpret_cast<JEVT_TYPE*>(pTmpBuf); *pEvtType = m_eType; pTmpBuf += sizeof(JEVT_TYPE); uiLeaveLen -= sizeof(JEVT_TYPE); //first store the from process length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strFromProc.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the from process into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strFromProc.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the from thread length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strFromThrd.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the from thread into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strFromThrd.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the from module length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strFromMod.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the from module into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strFromMod.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the to process length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strToProc.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the to process into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strToProc.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the to thread length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strToThrd.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the to thread into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strToThrd.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the to module length into the buffer pLen = reinterpret_cast<JUINT32*>(pTmpBuf); *pLen = m_strToMod.GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the to module into the buffer if (*pLen) { SafeStrcpy(pTmpBuf, m_strToMod.c_str(), uiLeaveLen); pTmpBuf += *pLen; uiLeaveLen -= *pLen; } //first store the body length into the buffer pBodyLen = reinterpret_cast<JUINT32*>(pTmpBuf); if (m_pBody) { *pBodyLen = m_pBody->GetLength(); pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); //then store the body into the buffer if (*pBodyLen) { m_pBody->Serialize(pTmpBuf, uiLeaveLen); } } else { *pBodyLen = 0; pTmpBuf += sizeof(JUINT32); uiLeaveLen -= sizeof(JUINT32); } *uiLen = uiAllocLen; return pBuf; } return JNULL; }