source: proiecte/swift/trunk/lib/hoard-371/src/heaplayers/chunkheap.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.5 KB
Line 
1/* -*- C++ -*- */
2
3#ifndef _CHUNKHEAP_H_
4#define _CHUNKHEAP_H_
5
6/*
7
8  Heap Layers: An Extensible Memory Allocation Infrastructure
9 
10  Copyright (C) 2000-2003 by Emery Berger
11  http://www.cs.umass.edu/~emery
12  emery@cs.umass.edu
13 
14  This program is free software; you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation; either version 2 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
28*/
29
30#include <assert.h>
31
32/**
33 * @class ChunkHeap
34 * @brief Allocates memory from the superheap in chunks.
35 * @param ChunkSize The minimum size for allocating memory from the superheap.
36 */
37
38namespace HL {
39
40  template <int ChunkSize, class SuperHeap>
41  class ChunkHeap : public SuperHeap {
42  public:
43
44    inline ChunkHeap (void)
45      : buffer (NULL),
46        eob (NULL)
47    {}
48
49    inline void * malloc (const size_t sz) {
50      void * ptr = buffer;
51      buffer += sz;
52      if (buffer <= eob) {
53        assert (eob != NULL);
54        assert ((size_t) (eob - (char *) ptr + 1) >= sz);
55        return ptr;
56      }
57          buffer -= sz;         // we didn't succeed, back up
58      return getMoreMemory(sz);
59    }
60
61    inline void clear (void) {
62      buffer = NULL;
63      eob = NULL;
64      SuperHeap::clear ();
65    }
66
67  private:
68
69    // Disabled.
70    inline int remove (void *);
71
72    void * getMoreMemory (size_t sz) {
73      assert (sz > 0);
74      // Round sz to the next chunk size.
75      size_t reqSize = (((sz-1) / ChunkSize) + 1) * ChunkSize;
76      char * buf = (char *) SuperHeap::malloc (reqSize);
77      if (buf == NULL) {
78        return NULL;
79      }
80      // If the current end of buffer is not the same as the new buffer,
81      // reset the buffer pointer.
82      if (eob != buf) {
83        buffer = buf;
84      }
85          else {
86                  // we still have a bit leftover at the end of previous buffer
87                  reqSize += eob - buffer;
88          }
89      eob = buffer + reqSize;
90   
91      void * ptr = buffer;
92      buffer += sz;
93      return ptr;
94    }
95
96    /// The current allocation buffer.
97    char * buffer;
98
99    /// The end of the buffer.
100    char * eob;
101  };
102
103}
104
105#endif
Note: See TracBrowser for help on using the repository browser.