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

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 2.9 KB
Line 
1#ifndef _SCALABLELEAHEAP_H_
2#define _SCALABLELEAHEAP_H_
3
4#include "heaplayers.h"
5
6namespace ScalableHeapNS {
7
8        template <int NumHeaps, class SuperHeap>
9        class GlobalHeapWrapper : public SuperHeap {
10        public:
11                inline void * malloc (size_t sz) {
12                        void * ptr = SuperHeap::malloc (sz);
13                        if (ptr != NULL) {
14                                assert (!isFree(ptr));
15                        }
16                        return ptr;
17                }
18                inline void free (void * ptr) {
19                        // Set this object's heap to an unassigned heap (NumHeaps),
20                        // then free it.
21                        setHeap (ptr, NumHeaps); // This should be an unassigned heap number.
22                        assert (getHeap(ptr) == NumHeaps);
23                        setPrevHeap(getNext(ptr), NumHeaps);
24                        assert (getPrevHeap(getNext(ptr)) == NumHeaps);
25                        SuperHeap::free (ptr);
26                }
27        private:
28                inline int remove (void *);
29        };
30
31
32  template <int Threshold, class Heap1, class Heap2>
33  class TryHeap : public Heap2 {
34  public:
35          TryHeap (void)
36                  : reserved (0)
37          {}
38
39    inline void * malloc (size_t sz) {
40      void * ptr = heap1.malloc (sz);
41      if (ptr == NULL) {
42#if 1
43                // Get a big chunk.
44                size_t chunkSize = (Threshold / 2) > sz ? (Threshold / 2) : sz;
45                ptr = Heap2::malloc (chunkSize);
46                if (ptr == NULL) {
47                        return NULL;
48                }
49                // Split it.
50                void * splitPiece = CoalesceHeap<Heap2>::split (ptr, sz);
51                assert (splitPiece != ptr);
52                assert (!isFree(ptr));
53                // Put the split piece on heap 1.
54                if (splitPiece != NULL) {
55                        reserved += getSize(splitPiece);
56                        heap1.free (splitPiece);
57                }
58#else
59                ptr = Heap2::malloc (sz);
60#endif
61          } else {
62                  reserved -= getSize(ptr);
63          }
64//        assert (getHeap(ptr) == tid);
65      return ptr;
66    }
67    inline void free (void * ptr) {
68          reserved += getSize(ptr);
69      heap1.free (ptr);
70      if (reserved > Threshold) {
71                // We've crossed the threshold.
72                // Free objects from heap 1 and give them to heap 2.
73                // Start big.
74                size_t sz = Threshold / 2;
75                while ((sz > sizeof(double)) && (reserved > Threshold / 2)) {
76                  void * p = NULL;
77                  while ((p == NULL) && (sz >= sizeof(double))) {
78                        p = heap1.malloc (sz);
79                        if (p == NULL) {
80                          sz >>= 1;
81                        }
82                  }
83                  if (p != NULL) {
84                          reserved -= getSize(p);
85                          Heap2::free (p);
86                  }
87                }
88      }
89    }
90
91       
92  private:
93        inline int remove (void * ptr);
94#if 0
95        {
96                assert (0);
97                abort();
98        }
99#endif
100
101    Heap1 heap1;
102        int reserved;
103  };
104
105
106  template <int NumHeaps, int MmapThreshold, class BaseNullHeap, class BaseHeap>
107  class SmallHeap : public
108          ScalableHeapNS::TryHeap<MmapThreshold,
109        MarkThreadHeap<NumHeaps, BaseNullHeap>,
110        MarkThreadHeap<NumHeaps, GlobalHeapWrapper<NumHeaps, BaseHeap> > > {};
111
112  template <int NumHeaps, int MmapThreshold, class BaseNullHeap, class BaseHeap>
113  class MTHeap :
114    public PHOThreadHeap<NumHeaps,
115                     LockedHeap<SmallHeap<NumHeaps, MmapThreshold, BaseNullHeap, BaseHeap> > > {};
116
117};
118
119 
120template <int NumHeaps, class BaseNullHeap, class BaseHeap, class Mmap>
121class ScalableHeap :
122public SelectMmapHeap<128 * 1024,
123         ScalableHeapNS::MTHeap<NumHeaps, 128 * 1024, BaseNullHeap, BaseHeap>,
124                    LockedHeap<Mmap> > {};
125#endif
Note: See TracBrowser for help on using the repository browser.