9 #define MDEBUG(...) printf(__VA_ARGS__)
11 #define MDEBUG(...) do {} while (0)
14 #include "StreamOutput.h"
17 * with MUCH thanks to http://www.parashift.com/c++-faq-lite/memory-pools.html
19 * test framework at https://gist.github.com/triffid/5563987
25 MemoryPool(void* base
, uint16_t size
);
29 void dealloc(void* p
);
31 void debug(StreamOutput
*);
39 static MemoryPool
* first
;
46 // this overloads "placement new"
47 inline void* operator new(size_t nbytes
, MemoryPool
& pool
)
49 return pool
.alloc(nbytes
);
52 // this allows placement new to free memory if the constructor fails
53 inline void operator delete(void* p
, MemoryPool
& pool
)
58 // this catches all usages of delete blah. The object's destructor is called before we get here
59 // it first checks if the deleted object is part of a pool, and uses free otherwise.
60 inline void operator delete(void* p
)
62 MemoryPool
* m
= MemoryPool::first
;
67 MDEBUG("Pool %p has %p, using dealloc()\n", m
, p
);
74 MDEBUG("no pool has %p, using free()\n", p
);
78 #endif /* _MEMORYPOOL_H */