/
main.cpp
executable file
·135 lines (119 loc) · 2.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "memory_pool.h"
#include <stdio.h>
#include <vector>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
int inUse = 0;
int inFree = 0;
struct Data
{
int inUse;
int inFree;
};
void WalkPoolCallback( void* pMem, size_t size, bool inUse, void* pData )
{
Data& d = *((Data*)pData);
printf( "chunk %p %s, size - %lu\n", pMem, inUse?"in use":"free", size);
if( inUse )
{
++d.inUse;
}
else
{
++d.inFree;
}
}
char buffer[1024*1024*8];
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;
}