Rev | Line | |
---|
[176] | 1 | #ifndef _PERCLASSHEAP_H |
---|
| 2 | #define _PERCLASSHEAP_H |
---|
| 3 | |
---|
| 4 | #include <new> |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @class PerClassHeap |
---|
| 8 | * @brief Enable the use of one heap for all class memory allocation. |
---|
| 9 | * |
---|
| 10 | * This class contains one instance of the SuperHeap argument. The |
---|
| 11 | * example below shows how to make a subclass of Foo that uses a |
---|
| 12 | * FreelistHeap to manage its memory, overloading operators new and |
---|
| 13 | * delete. |
---|
| 14 | * |
---|
| 15 | * <TT> |
---|
| 16 | * class NewFoo : public Foo, PerClassHeap<FreelistHeap<mallocHeap> > {}; |
---|
| 17 | * </TT> |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | namespace HL { |
---|
| 21 | |
---|
| 22 | template <class SuperHeap> |
---|
| 23 | class PerClassHeap { |
---|
| 24 | public: |
---|
| 25 | inline void * operator new (size_t sz) { |
---|
| 26 | return getHeap()->malloc (sz); |
---|
| 27 | } |
---|
| 28 | inline void operator delete (void * ptr) { |
---|
| 29 | getHeap()->free (ptr); |
---|
| 30 | } |
---|
| 31 | inline void * operator new[] (size_t sz) { |
---|
| 32 | return getHeap()->malloc (sz); |
---|
| 33 | } |
---|
| 34 | inline void operator delete[] (void * ptr) { |
---|
| 35 | getHeap()->free (ptr); |
---|
| 36 | } |
---|
| 37 | // For some reason, g++ needs placement new to be overridden |
---|
| 38 | // as well, at least in conjunction with use of the STL. |
---|
| 39 | // Otherwise, this should be superfluous. |
---|
| 40 | inline void * operator new (size_t sz, void * p) { return p; } |
---|
| 41 | inline void * operator new[] (size_t sz, void * p) { return p; } |
---|
| 42 | |
---|
| 43 | private: |
---|
| 44 | inline static SuperHeap * getHeap (void) { |
---|
| 45 | static SuperHeap theHeap; |
---|
| 46 | return &theHeap; |
---|
| 47 | } |
---|
| 48 | }; |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.