void List_Pop_Front(LIST *list,void *elem) { LIST_NODE *ptr = NULL; if( NULL == list || NULL == elem){ return; } ptr = list->head; if( NULL == ptr){ return; } if( NULL == ptr->next ){ xMemCpy(elem,ptr->element,list->elem_size); list->head = NULL; xFree(ptr->element); xFree(ptr); } else{ xMemCpy(elem,ptr->element,list->elem_size); list->head = ptr->next; ptr->next->prev = NULL; xFree(ptr->element); xFree(ptr); } list->size -= 1; }
int main() { xRegion region1 = xMalloc(sizeof(xRegionType)); __XMALLOC_ASSERT(region1->next == NULL); __XMALLOC_ASSERT(region1->prev == NULL); xRegion region2 = xMalloc(sizeof(xRegionType)); __XMALLOC_ASSERT(region2->next == NULL); __XMALLOC_ASSERT(region2->prev == NULL); xRegion region3 = xMalloc(sizeof(xRegionType)); __XMALLOC_ASSERT(region3->next == NULL); __XMALLOC_ASSERT(region3->prev == NULL); // inserts region1 after region2 xInsertRegionAfter(region1, region2); __XMALLOC_ASSERT(region2->next == region1); __XMALLOC_ASSERT(region1->prev == region2); // inserts region3 after region2 xInsertRegionAfter(region3, region2); __XMALLOC_ASSERT(region2->next == region3); __XMALLOC_ASSERT(region3->prev == region2); __XMALLOC_ASSERT(region1->prev == region3); __XMALLOC_ASSERT(region3->next == region1); xFree(region1); xFree(region2); xFree(region3); return 0; }
int List_Remove(LIST* list, void *data) { LIST_NODE *ptr = NULL; LIST_NODE *node = NULL; unsigned int found = FALSE; if( NULL == list || !list->size){ return -1; } ptr = list->head; while( ptr != NULL){ node = ptr; if (!xMemCmp(node->element, data, list->elem_size)){ node->prev->next = node->next; node->next->prev = node->prev; xFree(node->element); xFree(node); found = TRUE; break; } ptr = ptr->next; } if (!found){ return -1; } list->size -= 1; return 0; }
Void TComCUIcField::destroy() { if( m_pcIc ) { xFree( m_pcIc ); m_pcIc = NULL; } if( m_pcIcd ) { xFree( m_pcIcd ); m_pcIcd = NULL; } }
static gotnom(gk_word *Gkword) { int is_ending = 0; int rval = 0; char *endkeys; gk_string *curstem; char *curend; char *stemkeys; endkeys = stemkeys = curend = NULL; curstem = NULL; endkeys = (char *)malloc((size_t)LONGSTRING); stemkeys = (char *)malloc((size_t)LONGSTRING); curstem = CreatGkString(1); curend = (char *)malloc((size_t)MAXWORDSIZE); endkeys[0] = stemkeys[0] = 0; stripacc(endstring_of(Gkword)); Xstrncpy(curend,endstring_of(Gkword),MAXWORDSIZE); /* * note that with nouns we ignore the accents at this point. * this is so that we don't have to worry about varying accents such * as a)gaqo/s vs. a)/ristos. */ is_ending = chcknend(curend,endkeys); /*printf("rval %d on curend [%s]\n", is_ending , curend );*/ if( is_ending ) { Xstrncpy(gkstring_of(curstem),stem_of(Gkword),(int)sizeof gkstring_of(curstem)); stripacc(gkstring_of(curstem)); /*printf("trying stem [%s] with ending [%s] endkeys [%s]\n", gkstring_of(curstem) , curend , endkeys );*/ *stemkeys = 0; if( stemexists(gkstring_of(curstem),endkeys,stemkeys,1) ) { rval += StemWorks(Gkword,stemkeys, curstem ); } } if(! rval && 0 ) { /*rval=strippreverb(Gkword,endkeys,0);*/ /*printf("rval %d after stripp\n", rval );*/ if(!rval) checkforcompnoun(gkstring_of(curstem),endkeys,stemkeys); } xFree(endkeys,"endkeys"); xFree(stemkeys,"stemkeys"); FreeGkString(curstem); xFree(curend,"curend"); endkeys = stemkeys = curend = NULL; curstem = NULL; return(rval); }
void List_Push_Front(LIST *list, void *elem) { LIST_NODE *ptr = NULL; LIST_NODE *tmp = NULL; if( NULL == list || NULL == elem){ return; } ptr = list->head; tmp = xMalloc( sizeof(LIST_NODE )); if( NULL == tmp ){ return ; } tmp->element = xMalloc( list->elem_size ); if( NULL == tmp->element ){ xFree(tmp); return; } xMemCpy(tmp->element,elem,list->elem_size); if( NULL == ptr ){ list->head = tmp; tmp->next = NULL; tmp->prev = NULL; } else{ ptr->prev = tmp; tmp->next = ptr; list->head = tmp; tmp->prev = NULL; } list->size += 1; }
void IOCP_FreeContext(IOCP* iocp, IOCP_CONTEXT* context) { if (NULL == context){ return; } xFree(context); }
void List_Clear(LIST *list) { LIST_NODE *ptr = NULL; LIST_NODE *tmp = NULL; if( NULL == list ){ return; } ptr = list->head; while( ptr != NULL ){ tmp = ptr; ptr = ptr->next; xFree(tmp->element); xFree(tmp); } }
void Lock_Destory(Lock* lock, unsigned int flag) { if (NULL == lock){ return; } DeleteCriticalSection(&lock->cs); CloseHandle(lock->event); if (flag){ xFree(lock); } }
int List_Free(LIST **list) { LIST_NODE *tmp = NULL; LIST_NODE *ptr = NULL; if( NULL == (*list)){ return -1; } ptr = (*list)->head; while( ptr != NULL ){ tmp = ptr; ptr = ptr->next; xFree( tmp->element ); xFree( tmp ); } xFree( *list ); return 0; }
int main() { // alloc small memory block int i, j; for (i = 1; i <= 10 * __XMALLOC_MAX_SMALL_BLOCK_SIZE ; i++) { void *p = xMalloc0(i); __XMALLOC_ASSERT(NULL != p); for (j = 0; j < i; j++) __XMALLOC_ASSERT(0 == *((char*)(p + j))); xFree(p); } return 0; }
int main() { int i = __XMALLOC_MAX_SMALL_BLOCK_SIZE; for (i; i < 10 * __XMALLOC_MAX_SMALL_BLOCK_SIZE; i++) { void *p = xMalloc(i); __XMALLOC_ASSERT(NULL != p && "xMalloc should have allocated addr != NULL."); xFree(p); } return 0; }
int IOCP_Destroy(IOCP* iocp) { HANDLE curThread = NULL; int i = 0; if(iocp->hThreads) { curThread = GetCurrentThread(); for(i = 0; i < iocp->threads; ++i) { if(curThread == iocp->hThreads[i]) return IOCP_DESTROYFAILED; } } iocp->inited = FALSE; if(iocp->hThreads) { if(iocp->iocpHandle) { for(i = 0; i < iocp->threads; ++i){ PostQueuedCompletionStatus(iocp->iocpHandle, 0, (ULONG_PTR)NULL, NULL); } } for(i = 0; i < iocp->threads; ++i) { if( iocp->hThreads[i] ) { WaitForSingleObject(iocp->hThreads[i], INFINITE); CloseHandle(iocp->hThreads[i]); } } xFree(iocp->hThreads); } iocp->hThreads = NULL; iocp->threads = 0; if(iocp->iocpHandle) { if(!CloseHandle(iocp->iocpHandle)){ } iocp->iocpHandle = NULL; } TimerQueue_Destory(&iocp->timerQueue); Map_Foreach(iocp->contextMap, IOCP_ContextHandler); Map_Delete(iocp->contextMap); LockPool_Destroy(&iocp->lockPool); DeleteCriticalSection(&iocp->cs); return IOCP_SUCESS; }
void List_Pop_Back(LIST *list, void *elem) { LIST_NODE* ptr = NULL; LIST_NODE* ptr2 = NULL; if( NULL == list || NULL == elem ){ return; } ptr = list->head; while( ptr != NULL ){ ptr2 = ptr; ptr = ptr->next; } ptr = ptr2; xMemCpy(elem,ptr->element,list->elem_size); xFree(ptr->element); xFree(ptr); list->size -= 1; }
int main() { // alloc small memory block void *p = xMalloc(1); int i; // reallocate the memory as long as the reallocated block size fits in // xmallocs bins for (i = 2; i < __XMALLOC_MAX_SMALL_BLOCK_SIZE; i++) { p = xrealloc(p,i); __XMALLOC_ASSERT(NULL != p && "xRealloc should have allocated addr != NULL."); } xFree(p); return 0; }
int main() { int i; // alloc small memory blocks from static bins for (i = 1; i < __XMALLOC_MAX_SMALL_BLOCK_SIZE; i++) { void *p = xMalloc(i); xBin bin = xGetBinOfAddr(p); __XMALLOC_ASSERT(xIsStaticBin(bin) == TRUE); xFree(p); } // alloc new bin, which is not from the static ones xBin newBin = xMalloc(sizeof(xBinType)); __XMALLOC_ASSERT(xIsStaticBin(newBin) == FALSE); return 0; }
int IOCP_Init(IOCP* iocp, int threads) { SYSTEM_INFO sysInfo; int i = 0; if(threads <= 0) { GetSystemInfo(&sysInfo); threads = sysInfo.dwNumberOfProcessors; } if(threads <= 0 || threads > 64) threads = 5; if( NULL == (iocp->iocpHandle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, threads)) ) { iocp->lastErrorCode = GetLastError(); //iocp->destory(iocp); IOCP_Destroy(iocp); return IOCP_UNDEFINED; } TimerQueue_Init(&iocp->timerQueue); LockPool_Init(&iocp->lockPool, 0); iocp->threads = threads; if(iocp->hThreads) xFree(iocp->hThreads); iocp->hThreads = xMalloc(sizeof(HANDLE)* threads); xMemSet(iocp->hThreads, 0, sizeof(HANDLE) * threads); for(i = 0; i < iocp->threads; ++i) { //if(0 == (iocp->hThreads[i] = _beginthreadex(NULL, 0, IOCP_Service, iocp, 0, NULL))) if(NULL == (iocp->hThreads[i] = CreateThread(NULL, 0, IOCP_Service, iocp, 0, NULL))) { //iocp->lastErrorCode = errno; iocp->lastErrorCode = GetLastError(); //iocp->destroy(iocp); IOCP_Destroy(iocp); return IOCP_UNDEFINED; } } iocp->contextMap = Map_New(); InitializeCriticalSection(&iocp->cs); iocp->inited = TRUE; return IOCP_SUCESS; }
LIST* List_New(unsigned int elem_size) { LIST* list = NULL; list = xMalloc(sizeof(LIST)); if( NULL == list){ return NULL; } list->size = 0; list->elem_size = elem_size; list->head = xMalloc(sizeof(LIST_NODE*)); if( NULL == list->head ){ xFree((list)); return NULL; } return list; }
int List_Create(LIST **list,unsigned int elem_size) { if( NULL == list){ return -1; } (*list) = xMalloc( sizeof(LIST) ); if( NULL == (*list)){ return -1; } (*list)->size = 0; (*list)->elem_size = elem_size; (*list)->head = xMalloc(sizeof(LIST_NODE*) ); if( NULL == (*list)->head ){ xFree((*list)); return -1; } return 0; }
int main() { // alloc large memory block void *p = xMalloc0(2 * __XMALLOC_SIZEOF_PAGE); // realloc large p = xRealloc0Large(p, __XMALLOC_SIZEOF_PAGE); __XMALLOC_ASSERT(NULL != p); __XMALLOC_ASSERT(0 == *(char *)p); __XMALLOC_ASSERT(0 == *((char *)p + __XMALLOC_SIZEOF_PAGE -1)); // realloc large again p = xRealloc0Large(p, 10 * __XMALLOC_SIZEOF_PAGE); __XMALLOC_ASSERT(NULL != p); __XMALLOC_ASSERT(0 == *(char *)p); __XMALLOC_ASSERT(0 == *((char *)p + __XMALLOC_SIZEOF_PAGE -1)); __XMALLOC_ASSERT(0 == *((char *)p + (10 * __XMALLOC_SIZEOF_PAGE) -1)); xFree(p); return 0; }
void List_Erase(LIST *list,unsigned int index) { unsigned int counter = 0; LIST_NODE *ptr = NULL; LIST_NODE *ptr2 = NULL; if( NULL == list ){ return; } ptr = list->head; while( ptr != NULL && counter < index){ ptr2 = ptr; ptr = ptr->next; counter += 1; } ptr = ptr2; if( ptr->next == ptr ){ ptr->next = ptr->next; ptr->next->prev = NULL; xFree( ptr->element ); xFree( ptr ); } else if( NULL == ptr->next ){ ptr->prev->next = NULL; xFree( ptr->element ); xFree( ptr); } else{ ptr->prev->next = ptr->next; ptr->next->prev = ptr->prev; xFree(ptr->element); xFree(ptr); } list->size -= 1; }