source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/zoneheap.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.6 KB
Line 
1/* -*- C++ -*- */
2
3/**
4 * @class ZoneHeap
5 * @brief A zone (or arena, or region) based allocator.
6 * @author Emery Berger
7 * @date June 2000
8 *
9 * Uses the superclass to obtain large chunks of memory that are only
10 * returned when the heap itself is destroyed.
11 *
12*/
13
14#ifndef _ZONEHEAP_H_
15#define _ZONEHEAP_H_
16
17#include <assert.h>
18
19namespace HL {
20
21  template <class Super, size_t ChunkSize>
22  class ZoneHeap : public Super {
23  public:
24
25    ZoneHeap (void)
26      : sizeRemaining (-1),
27        currentArena (NULL),
28        pastArenas (NULL)
29    {}
30
31    ~ZoneHeap (void)
32    {
33      // printf ("deleting arenas!\n");
34      // Delete all of our arenas.
35      Arena * ptr = pastArenas;
36      while (ptr != NULL) {
37        void * oldPtr = (void *) ptr;
38        ptr = ptr->nextArena;
39        //printf ("deleting %x\n", ptr);
40        Super::free (oldPtr);
41      }
42      if (currentArena != NULL)
43        //printf ("deleting %x\n", currentArena);
44        Super::free (currentArena);
45    }
46
47    inline void * malloc (size_t sz) {
48      void * ptr = zoneMalloc (sz);
49      return ptr;
50    }
51
52    /// Free in a zone allocator is a no-op.
53    inline void free (void *) {}
54
55    /// Remove in a zone allocator is a no-op.
56    inline int remove (void *) { return 0; }
57
58
59  private:
60
61    inline static size_t align (int sz) {
62      return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1);
63    }
64
65    inline void * zoneMalloc (size_t sz) {
66      void * ptr;
67      // Round up size to an aligned value.
68      sz = align (sz);
69      // Get more space in our arena if there's not enough room in this one.
70      if ((currentArena == NULL) || (sizeRemaining < (int) sz)) {
71        // First, add this arena to our past arena list.
72        if (currentArena != NULL) {
73          currentArena->nextArena = pastArenas;
74          pastArenas = currentArena;
75        }
76        // Now get more memory.
77        size_t allocSize = ChunkSize;
78        if (allocSize < sz) {
79          allocSize = sz;
80        }
81        currentArena =
82          (Arena *) Super::malloc (allocSize + sizeof(Arena));
83        if (currentArena == NULL) {
84          return NULL;
85        }
86        currentArena->arenaSpace = (char *) (currentArena + 1);
87        currentArena->nextArena = NULL;
88        sizeRemaining = ChunkSize;
89      }
90      // Bump the pointer and update the amount of memory remaining.
91      sizeRemaining -= sz;
92      ptr = currentArena->arenaSpace;
93      currentArena->arenaSpace += sz;
94      assert (ptr != NULL);
95      return ptr;
96    }
97 
98    class Arena {
99    public:
100      Arena * nextArena;
101      char * arenaSpace;
102      double _dummy; // For alignment.
103    };
104   
105    /// Space left in the current arena.
106    long sizeRemaining;
107
108    /// The current arena.
109    Arena * currentArena;
110
111    /// A linked list of past arenas.
112    Arena * pastArenas;
113  };
114
115}
116
117#endif
Note: See TracBrowser for help on using the repository browser.