6504e07c26ab3876e1d3d6a559c551bfedf56adb
[gem5.git] / base / fast_alloc.cc
1 /*
2 * Copyright (c) 2000-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 NO_FAST_ALLOC
36
37 #ifdef __GNUC__
38 #pragma implementation
39 #endif
40
41 #include <assert.h>
42 #include "base/fast_alloc.hh"
43
44 void *FastAlloc::freeLists[Num_Buckets];
45
46 #ifdef FAST_ALLOC_STATS
47 unsigned FastAlloc::newCount[Num_Buckets];
48 unsigned FastAlloc::deleteCount[Num_Buckets];
49 unsigned FastAlloc::allocCount[Num_Buckets];
50 #endif
51
52 void *FastAlloc::moreStructs(int bucket)
53 {
54 assert(bucket > 0 && bucket < Num_Buckets);
55
56 int sz = bucket * Alloc_Quantum;
57 const int nstructs = Num_Structs_Per_New; // how many to allocate?
58 char *p = ::new char[nstructs * sz];
59
60 #ifdef FAST_ALLOC_STATS
61 ++allocCount[bucket];
62 #endif
63
64 freeLists[bucket] = p;
65 for (int i = 0; i < (nstructs-2); ++i, p += sz)
66 *(void **)p = p + sz;
67 *(void **)p = 0;
68
69 return (p + sz);
70 }
71
72
73 #ifdef FAST_ALLOC_DEBUG
74
75 #include <typeinfo>
76 #include <iostream>
77 #include <iomanip>
78 #include <map>
79 #include <string>
80
81 using namespace std;
82
83 // count of in-use FastAlloc objects
84 int FastAlloc::numInUse;
85
86 // dummy head & tail object for doubly linked list of in-use FastAlloc
87 // objects
88 FastAlloc FastAlloc::inUseHead(&FastAlloc::inUseHead, &FastAlloc::inUseHead);
89
90 // special constructor for dummy head: make inUsePrev & inUseNext
91 // point to self
92 FastAlloc::FastAlloc(FastAlloc *prev, FastAlloc *next)
93 {
94 inUsePrev = prev;
95 inUseNext = next;
96 }
97
98
99 // constructor: marks as in use, add to in-use list
100 FastAlloc::FastAlloc()
101 {
102 // mark this object in use
103 inUse = true;
104
105 // update count
106 ++numInUse;
107
108 // add to tail of list of in-use objects ("before" dummy head)
109 FastAlloc *myNext = &inUseHead;
110 FastAlloc *myPrev = inUseHead.inUsePrev;
111
112 inUsePrev = myPrev;
113 inUseNext = myNext;
114 myPrev->inUseNext = this;
115 myNext->inUsePrev = this;
116 }
117
118 // destructor: mark not in use, remove from in-use list
119 FastAlloc::~FastAlloc()
120 {
121 assert(inUse);
122 inUse = false;
123
124 --numInUse;
125 assert(numInUse >= 0);
126
127 // remove me from in-use list
128 inUsePrev->inUseNext = inUseNext;
129 inUseNext->inUsePrev = inUsePrev;
130 }
131
132
133 // summarize in-use list
134 void
135 FastAlloc::dump_summary()
136 {
137 map<string, int> typemap;
138
139 for (FastAlloc *p = inUseHead.inUseNext; p != &inUseHead; p = p->inUseNext)
140 {
141 ++typemap[typeid(*p).name()];
142 }
143
144 map<string, int>::const_iterator mapiter;
145
146 cout << " count type\n"
147 << " ----- ----\n";
148 for (mapiter = typemap.begin(); mapiter != typemap.end(); ++mapiter)
149 {
150 cout << setw(6) << mapiter->second << " " << mapiter->first << endl;
151 }
152 }
153
154
155 // show oldest n items on in-use list
156 void
157 FastAlloc::dump_oldest(int n)
158 {
159 // sanity check: don't want to crash the debugger if you forget to
160 // pass in a parameter
161 if (n < 0 || n > numInUse)
162 {
163 cout << "FastAlloc::dump_oldest: bad arg " << n
164 << " (" << numInUse << " objects in use" << endl;
165 return;
166 }
167
168 for (FastAlloc *p = inUseHead.inUsePrev;
169 p != &inUseHead && n > 0;
170 p = p->inUsePrev, --n)
171 {
172 cout << p << " " << typeid(*p).name() << endl;
173 }
174 }
175
176
177 //
178 // C interfaces to FastAlloc::dump_summary() and FastAlloc::dump_oldest().
179 // gdb seems to have trouble with calling C++ functions directly.
180 //
181 extern "C" void
182 fast_alloc_summary()
183 {
184 FastAlloc::dump_summary();
185 }
186
187 extern "C" void
188 fast_alloc_oldest(int n)
189 {
190 FastAlloc::dump_oldest(n);
191 }
192
193 #endif
194
195 #endif // NO_FAST_ALLOC