source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/experimental/lazyslotheap.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.2 KB
Line 
1/* -*- C++ -*- */
2
3#ifndef _LAZYSLOTHEAP_H_
4#define _LAZYSLOTHEAP_H_
5
6/*
7  This heap manages memory in units of Chunks.
8  malloc returns a slot within a chunk,
9  while free returns slots back to a chunk.
10
11  We completely exhaust the first chunk before we ever get another one.
12  Once a chunk (except for our current one) is COMPLETELY empty, it is returned to the superheap.
13*/
14
15#include <assert.h>
16#include <new.h>
17
18#include "chunk.h"
19
20
21template <int chunkSize, int slotSize, class Super>
22class LazySlotHeap : public Super {
23public:
24
25  LazySlotHeap (void)
26    : myChunk (new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>())
27  {}
28
29  ~LazySlotHeap (void)
30  {
31    // Give up our chunk.
32    Super::free (myChunk);
33  }
34
35  inline void * malloc (size_t sz) {
36    assert (sz <= slotSize);
37    void * ptr = myChunk->getSlot();
38    if (ptr == NULL) {
39      myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>();
40      ptr = myChunk->getSlot();
41      assert (ptr != NULL);
42    }
43    return ptr;
44  }
45
46  inline void free (void * ptr) {
47    /// Return a slot to its chunk.
48    Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr);
49    ch->putSlot (ptr);
50                // check if it's completely empty. If so, free it.
51          if (ch != myChunk) {
52      // Once the chunk is completely empty, free it.
53      if (ch->getNumSlotsAvailable() == ch->getNumSlots()) {
54        Super::free (ch);
55      }
56    }
57
58#if 0
59        // If this chunk isn't the one we're currently holding,
60                // free it. NB: It is NOT guaranteed to be empty!
61                if (ch != myChunk) {
62      Super::free (ch);
63    }
64#endif
65#if 0
66                template <int chunkSize, int slotSize, class Super>
67class StrictSlotHeap : public LazySlotHeap {
68public:
69        inline void free (void * ptr) {
70    /// Return a slot to its chunk.
71    Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr);
72    ch->putSlot (ptr);
73        // check if it's completely empty. If so, free it.
74          if (ch != myChunk) {
75      // Once the chunk is completely empty, free it.
76      if (ch->getNumSlotsAvailable() == ch->getNumSlots()) {
77        Super::free (ch);
78      }
79    }
80  }
81
82};
83#endif
84
85  }
86
87  inline static size_t size (void * ptr)
88  {
89          return slotSize;
90  }
91
92
93protected:
94
95  Chunk<chunkSize, slotSize> * myChunk;
96
97};
98
99
100
101#endif
Note: See TracBrowser for help on using the repository browser.