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