source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/perclassheap.h @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 1.3 KB
Line 
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
20namespace HL {
21
22template <class SuperHeap>
23class PerClassHeap {
24public:
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
43private:
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.