#include <stddef.h>
#include <fstream.h>
ofstream out("framis.out");
class Framis
{
char c[10];
static unsigned char pool[];
static unsigned char alloc_map[];
public:
enum { PSIZE = 100 };
Framis() { out << "framis()\n"; };
~Framis() { out << "~framis()...\n"; };
void * operator new (size_t);
void operator delete(void *);
};
unsigned char Framis::pool[PSIZE * sizeof(Framis)];
unsigned char Framis::alloc_map[PSIZE] = { 0} ;
void * Framis::operator new(size_t sz)
{
for(int i = 0; i < PSIZE; i++)
if(!alloc_map[i])
{
out << "using block " << i << "...";
alloc_map[i] = 1;
return pool + (i * sizeof(Framis));
}
out << "out of memory" << endl;
return 0;
}
void Framis::operator delete(void * m)
{
if(!m)
return;
unsigned long block = (unsigned long)m
- (unsigned long)pool;
block /= sizeof(Framis);
out << "freeing block " << endl;
alloc_map[block] = 0;
}
int main()
{
Framis * f[Framis::PSIZE];
for (int i = 0; i < Framis::PSIZE; i++)
{
f[i] = new Framis;
}
new Framis;
delete f[10];
Framis * x = new Framis;
delete x;
for (int j = 0; j < Framis::PSIZE; j++)
{
delete f[j];
}
return 0;
}