/*Orion Lawlor's Simple C++ Examples, olawlor@acm.org
Shows how to define a custom operator new
to allocate all classes.
*/
#include <stdio.h>
#include <stdlib.h>

class myFunkyAllocatorClass { 
public:
	void *someData;
};

void *operator new (size_t sz, myFunkyAllocatorClass &b) {
	printf("Custom operator new called for %d bytes\n",sz);
	return b.someData;
}

class foo {
public:
	double a;
	int x;
	foo() {printf("foo constructor called (this=%p)\n",this);}
};

int main() {
	myFunkyAllocatorClass b;
	b.someData=malloc(sizeof(foo));
	printf("Allocator data is at %p\n",b.someData);
	foo *f=new (b) foo;
	printf("Foo allocated at %p\n",f);
}
/*<@>
<@> ******** Program output: ********
<@> Allocator data is at 0x804a008
<@> Custom operator new called for 12 bytes
<@> foo constructor called (this=0x804a008)
<@> Foo allocated at 0x804a008
<@> */
