void FreeMemory ( void * ptr, size_t capacity ) { if( !m_memory_init ) InitPool (); else if (IsPoolFull()) { _mm_free(ptr); } // find the best place (insertion sort) FreeMemoryHolder* it(m_free_memory); FreeMemoryHolder * const it_end(&(m_free_memory[HMM_MAX_FREE_OBJECTS])); do { if (it->m_ptr == nullptr || it->m_capacity > capacity) { break; } } while (++it != it_end); // move other containers up by 1 index FreeMemoryHolder* it2(it_end); FreeMemoryHolder* it3(it2 - 2); while (--it2 != it) { it2->Copy(it3); it3->Zero(); --it3; } it->m_ptr = ptr; it->m_capacity = capacity; }
LW_NTSTATUS LwRtlSvcmLoadEmbedded( LW_IN LW_PCWSTR pServiceName, LW_IN LW_SVCM_MODULE_ENTRY_FUNCTION Entry, LW_OUT PLW_SVCM_INSTANCE* ppInstance ) { NTSTATUS status = STATUS_SUCCESS; PLW_SVCM_INSTANCE pInstance = NULL; status = InitPool(); GCOS(status); status = LW_RTL_ALLOCATE_AUTO(&pInstance); GCOS(status); LW_RTL_LOG_DEBUG("Loading embedded service: %s", pServiceName); status = LwRtlSvcmInitializeInstance(pInstance, pServiceName, "<embedded>", Entry); GCOS(status); cleanup: if (!NT_SUCCESS(status)) { LwRtlSvcmUnload(pInstance); pInstance = NULL; } *ppInstance = pInstance; return status; }
//-------------------------------------------------------------------------------------------------- le_mem_PoolRef_t _le_mem_CreatePool ( const char* componentName, ///< [IN] Name of the component. const char* name, ///< [IN] Name of the pool inside the component. size_t objSize ///< [IN] The size of the individual objects to be allocated from this pool /// (in bytes). E.g., sizeof(MyObject_t). ) { le_mem_PoolRef_t newPool = malloc(sizeof(MemPool_t)); // Crash if we can't create the memory pool. LE_ASSERT(newPool); // Initialize the memory pool. InitPool(newPool, componentName, name, objSize); Lock(); // Generate an error if there are multiple pools with the same name. VerifyUniquenessOfName(newPool); // Add the new pool to the list of pools. ListOfPoolsChgCnt++; le_dls_Queue(&ListOfPools, &(newPool->poolLink)); Unlock(); return newPool; }
int main(int argc, char **argv) { int arg_ins=1; float arg_exit=7; if (0 != InitPool()) return 1; AddTask(Task1, &arg_ins, NULL, &arg_exit); AddTask(Task2, &arg_ins, NULL, &arg_exit); while(1) { SLEEP(10); } return 0; }
void CMemoryPool::newNode(size_t nSize) { if (QTail == NULL || pMProxy == NULL) { std::cerr << "LinkList should be inited.." << std::endl; InitPool(nSize); return; } std::cout << "allocate " << nSize << "B memory to new linknode\n" << std::endl; LinkNode* pnode = new LinkNode(nSize); if (pnode == NULL) throw std::bad_alloc(); QTail->_next = pnode; QTail = pnode; pMProxy->setMemoryAddr(QTail->_pMemory); mListNodeNums++; }
GOrgueMemoryPool::GOrgueMemoryPool() : GOrgueThread(), m_PoolStart(0), m_PoolPtr(0), m_PoolEnd(0), m_CacheStart(0), m_PoolSize(0), m_PoolLimit(0), m_PageSize(4096), m_CacheSize(0), m_MallocSize(0), m_MemoryLimit(0), m_AllocError(0) { InitPool(); }
void* AllocateMemory ( size_t bytes, size_t & new_capacity, size_t align_of /* = 4U */ ) { if( !m_memory_init ) InitPool (); FreeMemoryHolder* it ( m_free_memory ); FreeMemoryHolder * const it_end ( &( m_free_memory[ HMM_MAX_FREE_OBJECTS ] ) ); do { if( it->m_ptr == nullptr ) // If this pointer is not valid, then we quit and call standard alloc, because array is sorted. { break; } else { if( it->m_capacity >= bytes ) // Is there enough capacity here ? { if( size_t ( it->m_ptr ) % align_of == 0 && it->m_capacity % align_of == 0 ) // Try to see if the pointer fits in the alignement requirements { new_capacity = it->m_capacity; void * ret ( it->m_ptr ); // move other containers down by 1 index FreeMemoryHolder* it2(it); while (++it2 != it_end) { it->Copy(it2); it2->Zero(); ++it; } return ret; } } } } while( ++it != it_end ); new_capacity = align_of; while( new_capacity < bytes /*|| ( new_capacity % align_of != 0) */ ) new_capacity <<= 1; void * nptr ( _mm_malloc ( new_capacity, align_of ) ); return nptr; }
//-------------------------------------------------------------------------------------------------- le_mem_PoolRef_t _le_mem_CreateSubPool ( le_mem_PoolRef_t superPool, ///< [IN] The super-pool. const char* componentName, ///< [IN] Name of the component. const char* name, ///< [IN] Name of the pool inside the component. size_t numObjects ///< [IN] The number of objects to take from the super-pool. ) { LE_ASSERT(superPool != NULL); // Make sure the parent pool is not itself a sub-pool. LE_ASSERT(superPool->superPoolPtr == NULL); // Get a sub-pool from the pool of sub-pools. le_mem_PoolRef_t subPool = le_mem_ForceAlloc(SubPoolsPool); // Initialize the pool. InitPool(subPool, componentName, name, superPool->userDataSize); subPool->superPoolPtr = superPool; Lock(); // Log an error if the pool name is not unique. VerifyUniquenessOfName(subPool); // Add the sub-pool to the list of pools. ListOfPoolsChgCnt++; le_dls_Queue(&ListOfPools, &(subPool->poolLink)); Unlock(); // Expand the pool to its initial size. // Note: This moves blocks from the parent pool to the sub pool, expanding the parent pool, // if necessary. le_mem_ExpandPool(subPool, numObjects); // Inherit the parent pool's destructor. subPool->destructor = superPool->destructor; return subPool; }
LW_NTSTATUS LwRtlSvcmLoadModule( LW_IN LW_PCWSTR pServiceName, LW_IN LW_PCWSTR pModulePath, LW_OUT PLW_SVCM_INSTANCE* ppInstance ) { NTSTATUS status = STATUS_SUCCESS; PLW_SVCM_INSTANCE pInstance = NULL; PSTR pModulePathA = NULL; LW_SVCM_MODULE_ENTRY_FUNCTION Entry = NULL; PSTR pEntryName = NULL; PSTR pBareName = NULL; PSTR pFinalSlash = NULL; PSTR pFinalDot = NULL; status = InitPool(); GCOS(status); status = LwRtlCStringAllocateFromWC16String(&pModulePathA, pModulePath); GCOS(status); pFinalSlash = strrchr(pModulePathA, '/'); pFinalDot = strrchr(pModulePathA, '.'); if (!pFinalSlash) { pFinalSlash = pModulePathA; } else { pFinalSlash++; } if (!pFinalDot) { pFinalDot = pModulePathA + strlen(pModulePathA); } status = LW_RTL_ALLOCATE(&pBareName, CHAR, (pFinalDot - pFinalSlash) + 1); GCOS(status); memcpy(pBareName, pFinalSlash, pFinalDot - pFinalSlash); status = LwRtlCStringAllocatePrintf(&pEntryName, "_LwSvcmEntry_%s", pBareName); GCOS(status); status = LW_RTL_ALLOCATE_AUTO(&pInstance); GCOS(status); LW_RTL_LOG_DEBUG("Loading service module: %s", pModulePathA); (void) dlerror(); pInstance->pDlHandle = dlopen(pModulePathA, RTLD_LOCAL | RTLD_NOW); if (!pInstance->pDlHandle) { LW_RTL_LOG_ERROR( "Could not load service module '%s': %s", pModulePathA, dlerror()); status = LwErrnoToNtStatus(errno); GCOS(status); } (void) dlerror(); Entry = dlsym(pInstance->pDlHandle, pEntryName); if (!Entry) { LW_RTL_LOG_ERROR( "Could not load entry point from service module '%s': %s", pModulePathA, dlerror()); status = LwErrnoToNtStatus(errno); if (!status) { status = STATUS_BAD_DLL_ENTRYPOINT; } GCOS(status); } status = LwRtlSvcmInitializeInstance(pInstance, pServiceName, pModulePathA, Entry); GCOS(status); cleanup: RTL_FREE(&pModulePathA); RTL_FREE(&pBareName); RTL_FREE(&pEntryName); if (!NT_SUCCESS(status)) { LwRtlSvcmUnload(pInstance); pInstance = NULL; } *ppInstance = pInstance; return status; }
int main() { struct epoll_event ev, events[MAX_EVENTS]; struct sockaddr_in client; struct sockaddr_in server; // const char* localhost = "127.0.0.1"; const char* localhost = "0.0.0.0"; int tcpfd, udpfd, nfds, confd; int servPort = 8888; socklen_t addrLen = sizeof(struct sockaddr); char line[MAX_LINE]; ssize_t n; MYSQL connection; pthread_t thread; pthread_attr_t attr; //create socket tcpfd = CreateSocket(TCP); //translate ip & port into sockaddr_in format Ip2SocketAddr(&server, localhost, servPort); //bind ip & port to socket BindSocketAddr(&server, tcpfd); //begin to listen listen(tcpfd, LISTEN_NUM); //create epoll epfd = CreateEpoll(MAX_EVENTS); //set fd nonblocking mode SetNonBlock(tcpfd); //register fd event to epoll EpollAdd(epfd, tcpfd, EPOLLIN|EPOLLET); //create thread pool InitPool(MAX_POOL_SIZE); //init a connection to MySql conn = mysql_init(NULL); if(mysql_real_connect(conn, "localhost", "root", "", "test_server", 0, NULL, 0) == NULL){ perror("no database connection...\n"); exit(1); } while(1){ if((nfds = EpollWait(epfd, events, 20, 500)) == -1){ perror("epoll_wait\n"); exit(-1); } for(int i = 0; i < nfds; i++) { if(events[i].data.fd == tcpfd) { confd = accept(tcpfd, (struct sockaddr *)&client, &addrLen); if(confd < 0) { perror("accept error \n"); exit(1); } else printf("connect %s:%d socket :%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port), confd); // SetNonBlock(confd); EpollAdd(epfd, confd, EPOLLIN|EPOLLET); if(IsRecvIn(&events[i])) AddTask2Pool(MessageHandlerThread, (void *)&confd); } }//for(0 to nfds) } }
CMemoryPool::CMemoryPool() { /*初始化内存池*/ InitPool(); }