DWORD WINAPI WahSetContext( LPCONTEXT_TABLE Table, SOCKET Socket, LPVOID Context ) /*++ Routine Description: Associates the given context with the given key. Arguments: Table - The table to contain the new context. Socket - The key to index the new context. Context - The new context. Return Value: DWORD - NO_ERROR if successful, a Win32 error code if not. --*/ { DWORD newLookupArraySize; LPVOID * newLookupArray; DWORD key = SOCKET_TO_KEY( Socket ); // // Acquire the lock protecting the context table. // LOCK_TABLE( Table ); // // Determine if we can do this without growing the lookup array. // if( Table->LookupArraySize > key ) { Table->LookupArray[key] = Context; UNLOCK_TABLE( Table ); return NO_ERROR; } // // We'll need to grow the lookup array first. // newLookupArraySize = Table->LookupArraySize + ARRAY_GROWTH_DELTA * ( ( ( key - Table->LookupArraySize ) / ARRAY_GROWTH_DELTA ) + 1 ); newLookupArray = REALLOC_MEM( Table->LookupArray, newLookupArraySize * sizeof(LPVOID) ); if( newLookupArray != NULL ) { Table->LookupArray = newLookupArray; Table->LookupArraySize = newLookupArraySize; Table->LookupArray[key] = Context; UNLOCK_TABLE( Table ); return NO_ERROR; } // // Error growing the lookup array. // UNLOCK_TABLE( Table ); return ERROR_NOT_ENOUGH_MEMORY; } // WahSetContext
void realloc_test() { arHeap_t *heap = NULL; #if !defined(USE_C_MALLOC) heap = AR_CreateHeap(); #endif std::vector<size_t> mem_size_set; std::vector<void*> mem_ptr_set; #if(OS_TYPE != OS_WINDOWS_CE) srand(time(NULL)); #else srand(_time64 (NULL)); #endif for(size_t i = 0; i < 10000; ++i) { size_t n = 0; while(n == 0) { n = AR_rand64() % 102767; if(n < 102767)n = 102767 + AR_rand64() % 32768; } mem_size_set.push_back(n); /* while(n == 0) { n = AR_rand64() % 32768; if(n < 256) { n += 256; } } mem_size_set.push_back(n); while(n == 0) { n = AR_rand64() % 256; } mem_size_set.push_back(n); */ } void *ptr = NULL; for(size_t i = 0; i < mem_size_set.size(); ++i) { size_t bytes = mem_size_set[i]; ptr = REALLOC_MEM(heap, ptr, bytes); memset(ptr, 'a', bytes); } FREE_MEM(heap, ptr); if(heap) { AR_DestroyHeap(heap); } }