/* Send Thread */ void send_thread (void const *argument) { static uint32_t i = 10; while (true) { i++; // Fake data update message_t *message = mpool.alloc(); message->voltage = CREATE_VOLTAGE(i); message->current = CREATE_CURRENT(i); message->counter = i; queue.put(message); Thread::wait(QUEUE_PUT_DELAY); } }
static int test_MemoryPool (){ cout << "Go Testing" << __FILE__ << endl; MemoryPool pool; auto ptr1 = pool.alloc (5); auto ptr2 = pool.alloc (10); auto ptr3 = pool.alloc (11); pool.dump (); pool.free (ptr1); pool.free (ptr2); pool.dump (); int* ptr4 = (int*)ptr3; // ptr4++; // error !! pool.free ( (void*&)ptr4 ); // clear cout << "clear" << endl; pool.dump (); pool.clear (); pool.dump (); cout << "clear done." << endl; // for c++ A* a = pool.cnew<A>(); pool.dump (); pool.cdelete<A>(a); pool.dump (); cout << "a=>" << a << endl; return 0; }
int main(int argc, char* argv[]) { MemoryPool* pPool = MemoryPool::createInPlace(buffer, sizeof(buffer)); /* allocates from the back of the pool to help prevent fragmentation */ MemoryPool::free(pPool->allocBack(112)); MemoryPool::free(pPool->allocBack(1024)); MemoryPool::free(pPool->allocBack(500)); MemoryPool::free(pPool->allocBack(2000)); const int iterations = 1000; srand( time(NULL) ); for( int counter = 0; counter<iterations; ++counter ) { MemoryPool* pPool = MemoryPool::createInPlace(buffer, sizeof(buffer)); { std::vector<void*> a;a.reserve(10000); for( int i = 0;i<10000; ++i ) { a.push_back(pPool->alloc(8)); } for( int i = 0;i<10000; ++i ) { MemoryPool::free(a[i]); } } std::vector<int> alloc; std::vector<int> frees; int nAllocations = 0; int nFree = 0; int nBytes = 0; { alloc.reserve(30000); frees.reserve(30000); for( int i = 0;i<10000; ++i ) { frees.push_back( rand() ); if( rand() % 7 || ( nAllocations <= (nFree*2) ) ) { int size = 8 + (rand() % (2048)); alloc.push_back(size); nBytes += size; ++nAllocations; } else { alloc.push_back(0); ++nFree; } } } std::vector<void*> pp; const int sz = alloc.size(); pp.reserve(sz); { for(int i =0 ;i<sz; ++i ) { const int size = alloc[i]; int* p; if( size > 0 ) { p = (int*)pPool->alloc(size); assert( 0 == ( (ptrdiff_t)p & 7) ); // 8 byte alignment assert(p); *p = i; pp.push_back(p); } else { const int idx = frees[i]%pp.size(); p = (int*)pp[idx]; MemoryPool::free(p); pp[idx] = pp.back(); pp.pop_back(); } } MemoryPool::Stats stats; pPool->calcStats(stats); printf( "Pool Status: %d %d %d %d\n", stats.nBytesFree, stats.nBytesInUse, stats.nFree, stats.nInUse ); for( int i = 0; !pp.empty(); ++i ) { const int idx = frees[i]%pp.size(); void* p = pp[idx]; pp[idx] = pp.back(); pp.pop_back(); MemoryPool::free(p); } pPool->forceConsolidate(); pPool->calcStats(stats); printf( "Pool Status: %d %d %d %d\n\n", stats.nBytesFree, stats.nBytesInUse, stats.nFree, stats.nInUse ); } } return 0; }