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