Merge zeep.pool:/z/saidi/work/m5.newmem
[gem5.git] / src / base / fast_alloc.hh
1 /*
2 * Copyright (c) 2000-2001, 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * This code was originally written by Steve Reinhardt as part of
31 * the Wisconsin Wind Tunnel simulator. Relicensed as part of M5
32 * by permission.
33 */
34
35 #ifndef __FAST_ALLOC_H__
36 #define __FAST_ALLOC_H__
37
38 #include <stddef.h>
39
40 // Fast structure allocator. Designed for small objects that are
41 // frequently allocated and deallocated. This code is derived from the
42 // 'alloc_struct' package used in WWT and Blizzard. C++ provides a
43 // much nicer framework for the same optimization. The package is
44 // implemented as a class, FastAlloc. Allocation and deletion are
45 // performed using FastAlloc's new and delete operators. Any object
46 // that derives from the FastAlloc class will transparently use this
47 // allocation package.
48
49 // The static allocate() and deallocate() methods can also be called
50 // directly if desired.
51
52 // In order for derived classes to call delete with the correct
53 // structure size even when they are deallocated via a base-type
54 // pointer, they must have a virtual destructor. It is sufficient for
55 // FastAlloc to declare a virtual destructor (as it does); it is not
56 // required for derived classes to declare their own destructor. The
57 // compiler will automatically generate a virtual destructor for each
58 // derived class. However, it is more efficient if each derived class
59 // defines an inline destructor, so that the compiler can statically
60 // collapse the destructor call chain back up the inheritance
61 // hierarchy.
62
63 // Uncomment this #define to track in-use objects
64 // (for debugging memory leaks).
65 //#define FAST_ALLOC_DEBUG
66
67 // Uncomment this #define to count news, deletes, and chunk allocations
68 // (by bucket).
69 // #define FAST_ALLOC_STATS
70
71 #include "config/no_fast_alloc.hh"
72
73 #if NO_FAST_ALLOC
74
75 class FastAlloc {
76 };
77
78 #else
79
80 class FastAlloc {
81 public:
82
83 static void *allocate(size_t);
84 static void deallocate(void *, size_t);
85
86 void *operator new(size_t);
87 void operator delete(void *, size_t);
88
89 #ifdef FAST_ALLOC_DEBUG
90 FastAlloc();
91 FastAlloc(FastAlloc*,FastAlloc*); // for inUseHead, see below
92 virtual ~FastAlloc();
93 #else
94 virtual ~FastAlloc() {}
95 #endif
96
97 private:
98
99 // Max_Alloc_Size is the largest object that can be allocated with
100 // this class. There's no fundamental limit, but this limits the
101 // size of the freeLists array. Let's not make this really huge
102 // like in Blizzard.
103 static const int Max_Alloc_Size = 512;
104
105 // Alloc_Quantum is the difference in size between adjacent
106 // buckets in the free list array.
107 static const int Log2_Alloc_Quantum = 3;
108 static const int Alloc_Quantum = (1 << Log2_Alloc_Quantum);
109
110 // Num_Buckets = bucketFor(Max_Alloc_Size) + 1
111 static const int Num_Buckets =
112 ((Max_Alloc_Size + Alloc_Quantum - 1) >> Log2_Alloc_Quantum) + 1;
113
114 // when we call new() for more structures, how many should we get?
115 static const int Num_Structs_Per_New = 20;
116
117 static int bucketFor(size_t);
118 static void *moreStructs(int bucket);
119
120 static void *freeLists[Num_Buckets];
121
122 #ifdef FAST_ALLOC_STATS
123 static unsigned newCount[Num_Buckets];
124 static unsigned deleteCount[Num_Buckets];
125 static unsigned allocCount[Num_Buckets];
126 #endif
127
128 #ifdef FAST_ALLOC_DEBUG
129 // per-object debugging fields
130 bool inUse; // in-use flag
131 FastAlloc *inUsePrev; // ptrs to build list of in-use objects
132 FastAlloc *inUseNext;
133
134 // static (global) debugging vars
135 static int numInUse; // count in-use objects
136 static FastAlloc inUseHead; // dummy head for list of in-use objects
137
138 public:
139 // functions to dump debugging info (see fast_alloc.cc for C
140 // versions that might be more agreeable to call from gdb)
141 static void dump_summary();
142 static void dump_oldest(int n);
143 #endif
144 };
145
146
147 inline
148 int FastAlloc::bucketFor(size_t sz)
149 {
150 return (sz + Alloc_Quantum - 1) >> Log2_Alloc_Quantum;
151 }
152
153
154 inline
155 void *FastAlloc::allocate(size_t sz)
156 {
157 int b;
158 void *p;
159
160 if (sz > Max_Alloc_Size)
161 return (void *)::new char[sz];
162
163 b = bucketFor(sz);
164 p = freeLists[b];
165
166 if (p)
167 freeLists[b] = *(void **)p;
168 else
169 p = moreStructs(b);
170
171 #ifdef FAST_ALLOC_STATS
172 ++newCount[b];
173 #endif
174
175 return p;
176 }
177
178
179 inline
180 void FastAlloc::deallocate(void *p, size_t sz)
181 {
182 int b;
183
184 if (sz > Max_Alloc_Size)
185 {
186 ::delete [] (char *)p;
187 return;
188 }
189
190 b = bucketFor(sz);
191 *(void **)p = freeLists[b];
192 freeLists[b] = p;
193 #ifdef FAST_ALLOC_STATS
194 ++deleteCount[b];
195 #endif
196 }
197
198
199 inline
200 void *FastAlloc::operator new(size_t sz)
201 {
202 return allocate(sz);
203 }
204
205
206 inline
207 void FastAlloc::operator delete(void *p, size_t sz)
208 {
209 deallocate(p, sz);
210 }
211
212 #endif // NO_FAST_ALLOC
213
214 #endif // __FAST_ALLOC_H__