source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/experimental/strictslotheap.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.6 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 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
20template <int chunkSize, int slotSize, class Super>
21class LazySlotHeap : public Super {
22public:
23
24  LazySlotHeap (void)
25    : myChunk (new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>())
26  {}
27
28  virtual ~LazySlotHeap (void)
29  {
30    // Give up our chunk.
31    Super::free (myChunk);
32  }
33
34  inline void * malloc (size_t sz) {
35    assert (sz <= chunkSize);
36    void * ptr = myChunk->getSlot();
37    if (ptr == NULL) {
38      myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>();
39      ptr = myChunk->getSlot();
40      assert (ptr != NULL);
41    }
42    return ptr;
43  }
44
45  inline void free (void * ptr) {
46    /// Return a slot to its chunk.
47    Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr);
48    ch->putSlot (ptr);
49    // Once the chunk is completely empty, free it.
50    if (ch->getNumSlotsAvailable() == ch->getNumSlots()) {
51      if (ch == myChunk) {
52        // If this was 'our' chunk, get another one.
53        myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>();
54      }
55      Super::free (ch);
56    }
57  }
58
59protected:
60
61  inline static size_t size (void * ptr)
62  {
63          return slotSize;
64  }
65
66
67private:
68
69  Chunk<chunkSize, slotSize> * myChunk;
70
71};
72
73
74#endif
Note: See TracBrowser for help on using the repository browser.