本文整理汇总了C++中MemoryPool::allocBack方法的典型用法代码示例。如果您正苦于以下问题:C++ MemoryPool::allocBack方法的具体用法?C++ MemoryPool::allocBack怎么用?C++ MemoryPool::allocBack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryPool
的用法示例。
在下文中一共展示了MemoryPool::allocBack方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
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 );
}
}
//.........这里部分代码省略.........