1 | // -*- C++ -*- |
---|
2 | |
---|
3 | /* |
---|
4 | |
---|
5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
6 | |
---|
7 | Copyright (C) 2000-2003 by Emery Berger |
---|
8 | http://www.cs.umass.edu/~emery |
---|
9 | emery@cs.umass.edu |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify |
---|
12 | it under the terms of the GNU General Public License as published by |
---|
13 | the Free Software Foundation; either version 2 of the License, or |
---|
14 | (at your option) any later version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | GNU General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
24 | |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #ifndef _SLOPHEAP_H_ |
---|
29 | #define _SLOPHEAP_H_ |
---|
30 | |
---|
31 | /** |
---|
32 | * @class SlopHeap |
---|
33 | * |
---|
34 | * SlopHeap is designed to guarantee that you always have an extra N bytes |
---|
35 | * available after the most recent malloc. This is necessary for the current |
---|
36 | * coalescing support, which can look past the last allocated object. |
---|
37 | * |
---|
38 | * @param SuperHeap The parent heap. |
---|
39 | * @param SLOP The amount of extra memory required, in bytes. |
---|
40 | */ |
---|
41 | |
---|
42 | namespace HL { |
---|
43 | |
---|
44 | template <class SuperHeap, int SLOP = 16> |
---|
45 | class SlopHeap : public SuperHeap { |
---|
46 | public: |
---|
47 | SlopHeap (void) |
---|
48 | : remaining (0), |
---|
49 | ptr (NULL) |
---|
50 | {} |
---|
51 | |
---|
52 | inline void * malloc (const size_t nbytes) { |
---|
53 | |
---|
54 | // Put the usual case up front. |
---|
55 | if (nbytes <= remaining) { |
---|
56 | remaining -= nbytes; |
---|
57 | char * p = ptr; |
---|
58 | ptr += nbytes; |
---|
59 | return (void *) p; |
---|
60 | } |
---|
61 | |
---|
62 | // |
---|
63 | // We don't have enough space to satisfy the current |
---|
64 | // request, so get more memory. |
---|
65 | // |
---|
66 | |
---|
67 | return getMoreMemory(nbytes); |
---|
68 | } |
---|
69 | |
---|
70 | inline void clear (void) { |
---|
71 | ptr = NULL; |
---|
72 | remaining = 0; |
---|
73 | SuperHeap::clear (); |
---|
74 | } |
---|
75 | |
---|
76 | inline void free (void *) {} |
---|
77 | |
---|
78 | private: |
---|
79 | |
---|
80 | // Disabled. |
---|
81 | inline int remove (void *); |
---|
82 | |
---|
83 | void * getMoreMemory (size_t nbytes) { |
---|
84 | char * newptr = (char *) SuperHeap::malloc (nbytes + SLOP); |
---|
85 | |
---|
86 | if (newptr == NULL) { |
---|
87 | return NULL; |
---|
88 | } |
---|
89 | |
---|
90 | // |
---|
91 | // If this new memory is contiguous with the previous one, |
---|
92 | // reclaim the "slop". |
---|
93 | // |
---|
94 | |
---|
95 | if ((ptr != NULL) && (ptr + remaining + SLOP == newptr)) { |
---|
96 | remaining += SLOP; |
---|
97 | } else { |
---|
98 | ptr = newptr; |
---|
99 | remaining = 0; |
---|
100 | } |
---|
101 | char * p = ptr; |
---|
102 | ptr += nbytes; |
---|
103 | |
---|
104 | return (void *) p; |
---|
105 | } |
---|
106 | |
---|
107 | char * ptr; |
---|
108 | size_t remaining; |
---|
109 | |
---|
110 | }; |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | #endif |
---|