source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/experimental/theap.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 _THEAP_H_
2#define _THEAP_H_
3
4// A general threshold-based heap.
5
6// Threshold layer.
7// Once we have more than Threshold bytes on our freelist,
8// return memory to the superheap.
9
10// The class argument FH should be some freelist heap that subclasses NullHeap.
11
12template <class SuperHeap, class FH, int Threshold>
13class THeap : public SuperHeap {
14public:
15  THeap (void)
16    : total (0)
17  {}
18
19  ~THeap (void)
20  {}
21
22  inline void * malloc (size_t sz) {
23    void * ptr;
24    ptr = fHeap.malloc (sz);
25    if (ptr == NULL) {
26      // We have no memory on our freelist.
27      // Get it from the superheap.
28      ptr = SuperHeap::malloc (sz);
29    } else {
30      total -= size(ptr);
31      // printf ("total = %d\n", total);
32    }
33        assert (size(ptr) >= sz);
34    return ptr;
35  }
36
37  inline void free (void * ptr) {
38    if (total < Threshold) {
39      // printf ("FREE TO FREELIST.\n");
40      total += size(ptr);
41      fHeap.free (ptr);
42      //printf ("total = %d\n", total);
43    } else {
44      // Dump the freelist heap.
45      void * p = fHeap.malloc (1);
46      while (p != NULL) {
47                SuperHeap::free (p);
48                p = fHeap.malloc (1);
49      }
50      SuperHeap::free (ptr);
51          total = 0;
52    }
53  }
54
55private:
56  // Provide a free list that will return NULL if it is out of memory.
57  FH fHeap;
58  int total;
59};
60
61
62#endif
Note: See TracBrowser for help on using the repository browser.