#include <iostream>
#include <time.h>
using namespace std;
#ifdef _WIN32
#define random rand
#define srandom srand
#endif
struct LittleStruct
{
void *pointer1, *pointer2;
int val1, val2;
LittleStruct *ls1;
};
static const int SIZE_ASSIGNED = 100000, NUM_RUN = 1000000, SIZE_POOL = 20,
DRAIN = SIZE_POOL - SIZE_POOL / 4, LOG_EVERY = NUM_RUN / 1000;
#ifdef _WIN32
static const long long RAND_TOP = RAND_MAX;
#else
static const long long RAND_TOP = (1 << 31) - 1;
#endif
static LittleStruct *assigned_ptrs[SIZE_ASSIGNED], *pool[SIZE_POOL];
static int numAssigned = 0, numAllocs = 0, numFrees = 0;
static inline int getPoolIX()
{
long long bigrand = (long long)random() * 20;
int rand = bigrand / RAND_TOP;
if (rand == 20) rand = 19;
// clunky, but it'll do for this
return (rand);
}
static void freeSlot()
{
int slot = getPoolIX();
if (pool[slot] != NULL)
{
numFrees++;
delete pool[slot];
pool[slot] = NULL;
}
}
static LittleStruct *allocSlot()
{
int ix;
for (;;)
{
for (ix = 0; ix < SIZE_POOL; ix++)
{
int slot = getPoolIX();
if (pool[slot] == NULL)
{
numAllocs++;
return (pool[slot] = new LittleStruct());
}
else
freeSlot();
}
for (ix = 0; ix < DRAIN; ix++)
freeSlot();
}
return NULL;
}
static void tallyAlloced(LittleStruct *ls)
{
for (int ix = 0; ix < numAssigned; ix++)
if (assigned_ptrs[ix] == ls) return;
assigned_ptrs[numAssigned++] = ls;
}
int main(int argc, char **argv)
{
bool dolog = false;
if (argc > 1)
{
switch(argv[1][0])
{
case 't': // check some values and test the random number generator
{
char buf[32];
cout << "SIZE_POOL: " << SIZE_POOL << endl;
cout << "RAND_TOP: 0x" << hex << RAND_TOP << dec << endl;
for (int ix = 0; ix < 1000; ix++)
cout << ix << ": " << getPoolIX() << endl;
}
exit(0);
case 'l': // do logs
dolog = true;
break;
default:
cout << "alloctest { t | l }" << endl;
cout << "where:" << endl;
cout << " t: test random number generator, show values." << endl;
cout << " l: log output (slows things up, of course...)" << endl;
exit(0);
}
}
else
{
srandom(time(NULL));
memset(pool, SIZE_POOL * sizeof(LittleStruct *), 0);
// make sure this is cleared, no matter what
while (numAllocs < NUM_RUN)
{
tallyAlloced(allocSlot());
if (dolog && (numAllocs % LOG_EVERY) == 0)
cout << "NumAllocs: " << numAllocs <<
", NumFrees: " << numFrees <<
", NumAssigned: " << numAssigned << endl;
if (numAssigned >= SIZE_ASSIGNED)
{
cout << "Overflowed assigned pointer vector" << endl;
exit(1);
}
}
cout << endl << "Run ended: " << endl;
cout << "Used:\t\t" << numAllocs << " allocations, " << numFrees << " frees." <<endl;
cout << " \t\t" << numAssigned << " pointers, total." << endl << endl;
}
}
#include <iostream>
#include <time.h>
using namespace std;
#ifdef _WIN32
#define random rand
#define srandom srand
#endif
struct LittleStruct
{
void *pointer1, *pointer2;
int val1, val2;
LittleStruct *ls1;
};
static const int SIZE_ASSIGNED = 100000, NUM_RUN = 1000000, SIZE_POOL = 20,
DRAIN = SIZE_POOL - SIZE_POOL / 4, LOG_EVERY = NUM_RUN / 1000;
#ifdef _WIN32
static const long long RAND_TOP = RAND_MAX;
#else
static const long long RAND_TOP = (1 << 31) - 1;
#endif
static LittleStruct *assigned_ptrs[SIZE_ASSIGNED], *pool[SIZE_POOL];
static int numAssigned = 0, numAllocs = 0, numFrees = 0;
static inline int getPoolIX()
{
long long bigrand = (long long)random() * 20;
int rand = bigrand / RAND_TOP;
if (rand == 20) rand = 19;
// clunky, but it'll do for this
return (rand);
}
static void freeSlot()
{
int slot = getPoolIX();
if (pool[slot] != NULL)
{
numFrees++;
delete pool[slot];
pool[slot] = NULL;
}
}
static LittleStruct *allocSlot()
{
int ix;
for (;;)
{
for (ix = 0; ix < SIZE_POOL; ix++)
{
int slot = getPoolIX();
if (pool[slot] == NULL)
{
numAllocs++;
return (pool[slot] = new LittleStruct());
}
else
freeSlot();
}
for (ix = 0; ix < DRAIN; ix++)
freeSlot();
}
return NULL;
}
static void tallyAlloced(LittleStruct *ls)
{
for (int ix = 0; ix < numAssigned; ix++)
if (assigned_ptrs[ix] == ls) return;
assigned_ptrs[numAssigned++] = ls;
}
int main(int argc, char **argv)
{
bool dolog = false;
if (argc > 1)
{
switch(argv[1][0])
{
case 't': // check some values and test the random number generator
{
char buf[32];
cout << "SIZE_POOL: " << SIZE_POOL << endl;
cout << "RAND_TOP: 0x" << hex << RAND_TOP << dec << endl;
for (int ix = 0; ix < 1000; ix++)
cout << ix << ": " << getPoolIX() << endl;
}
exit(0);
case 'l': // do logs
dolog = true;
break;
default:
cout << "alloctest { t | l }" << endl;
cout << "where:" << endl;
cout << " t: test random number generator, show values." << endl;
cout << " l: log output (slows things up, of course...)" << endl;
exit(0);
}
}
else
{
srandom(time(NULL));
memset(pool, SIZE_POOL * sizeof(LittleStruct *), 0);
// make sure this is cleared, no matter what
while (numAllocs < NUM_RUN)
{
tallyAlloced(allocSlot());
if (dolog && (numAllocs % LOG_EVERY) == 0)
cout << "NumAllocs: " << numAllocs <<
", NumFrees: " << numFrees <<
", NumAssigned: " << numAssigned << endl;
if (numAssigned >= SIZE_ASSIGNED)
{
cout << "Overflowed assigned pointer vector" << endl;
exit(1);
}
}
cout << endl << "Run ended: " << endl;
cout << "Used:\t\t" << numAllocs << " allocations, " << numFrees << " frees." <<endl;
cout << " \t\t" << numAssigned << " pointers, total." << endl << endl;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks