#include <sys/types.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>


class NextOnFreeList {
public:
    NextOnFreeList *next;
};

static std::stack<void *> qq;

class Event
{
	public:
		enum Event_Types {PROCESS,DATA};
		Event_Types Type;
		virtual ~Event() {}
		uint64_t timeIdx;
		virtual void process(void) {}
		
		inline void *operator new(size_t size);
		inline void operator delete(void *doomed, size_t size);
};

inline void * Event::operator new(size_t size)
{
	if(qq.empty()) {
		return (void *)malloc(64);
	}
	void *retval=qq.top();
	qq.pop();
	return retval;
}

inline void Event::operator delete(void *doomed, size_t size)
{
	qq.push(doomed);
}


class Event_InsertFlit: public Event
{
	public:
		Event_InsertFlit(uint64_t time, void *_from,
			void *_to, void *linkp);
		void process(void);
	private:
		void *from;
		void *to;
		void *lp_to_send;
};

Event_InsertFlit::Event_InsertFlit(uint64_t time, void *_from,
	void *_to, void *linkp)
{
	Type = DATA;
	timeIdx = time;
	to = _to;
	from = _from;
	lp_to_send = linkp;
}
void Event_InsertFlit::process(void)
{
	std::cout << "nothing\n";
}


int main(int argc, char argv[])
{
	Event *ev[1001];

	std::cout << "size " << sizeof(Event_InsertFlit)<< "\n";

	for(uint32_t i=0; i<1000;i++) {
		for(uint32_t j=0; j<1000;j++) {
			ev[j] = new Event_InsertFlit(1,NULL,NULL,NULL);
		}
		
		ev[1000] = new Event_InsertFlit(1,NULL,NULL,NULL);
		for(uint32_t j=0; j<10000;j++) {
			Event *temp=ev[1000];
			ev[1000] = new Event_InsertFlit(1,NULL,NULL,NULL);
			delete temp;
		}
		delete ev[1000];
		
		for(uint32_t j=0; j<1000;j++) {
			delete ev[j];
		}
	}
}

