2 * Copyright (c) 2003 The Regents of The University of Michigan
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.
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.
30 * This code was originally written by Steve Reinhardt as part of
31 * the Wisconsin Wind Tunnel simulator. Relicensed as part of M5
38 #pragma implementation
42 #include "base/fast_alloc.hh"
44 void *FastAlloc::freeLists
[Num_Buckets
];
46 #ifdef FAST_ALLOC_STATS
47 unsigned FastAlloc::newCount
[Num_Buckets
];
48 unsigned FastAlloc::deleteCount
[Num_Buckets
];
49 unsigned FastAlloc::allocCount
[Num_Buckets
];
52 void *FastAlloc::moreStructs(int bucket
)
54 assert(bucket
> 0 && bucket
< Num_Buckets
);
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
];
60 #ifdef FAST_ALLOC_STATS
64 freeLists
[bucket
] = p
;
65 for (int i
= 0; i
< (nstructs
-2); ++i
, p
+= sz
)
73 #ifdef FAST_ALLOC_DEBUG
83 // count of in-use FastAlloc objects
84 int FastAlloc::numInUse
;
86 // dummy head & tail object for doubly linked list of in-use FastAlloc
88 FastAlloc
FastAlloc::inUseHead(&FastAlloc::inUseHead
, &FastAlloc::inUseHead
);
90 // special constructor for dummy head: make inUsePrev & inUseNext
92 FastAlloc::FastAlloc(FastAlloc
*prev
, FastAlloc
*next
)
99 // constructor: marks as in use, add to in-use list
100 FastAlloc::FastAlloc()
102 // mark this object in use
108 // add to tail of list of in-use objects ("before" dummy head)
109 FastAlloc
*myNext
= &inUseHead
;
110 FastAlloc
*myPrev
= inUseHead
.inUsePrev
;
114 myPrev
->inUseNext
= this;
115 myNext
->inUsePrev
= this;
118 // destructor: mark not in use, remove from in-use list
119 FastAlloc::~FastAlloc()
125 assert(numInUse
>= 0);
127 // remove me from in-use list
128 inUsePrev
->inUseNext
= inUseNext
;
129 inUseNext
->inUsePrev
= inUsePrev
;
133 // summarize in-use list
135 FastAlloc::dump_summary()
137 map
<string
, int> typemap
;
139 for (FastAlloc
*p
= inUseHead
.inUseNext
; p
!= &inUseHead
; p
= p
->inUseNext
)
141 ++typemap
[typeid(*p
).name()];
144 map
<string
, int>::const_iterator mapiter
;
146 cout
<< " count type\n"
148 for (mapiter
= typemap
.begin(); mapiter
!= typemap
.end(); ++mapiter
)
150 cout
<< setw(6) << mapiter
->second
<< " " << mapiter
->first
<< endl
;
155 // show oldest n items on in-use list
157 FastAlloc::dump_oldest(int n
)
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
)
163 cout
<< "FastAlloc::dump_oldest: bad arg " << n
164 << " (" << numInUse
<< " objects in use" << endl
;
168 for (FastAlloc
*p
= inUseHead
.inUsePrev
;
169 p
!= &inUseHead
&& n
> 0;
170 p
= p
->inUsePrev
, --n
)
172 cout
<< p
<< " " << typeid(*p
).name() << endl
;
178 // C interfaces to FastAlloc::dump_summary() and FastAlloc::dump_oldest().
179 // gdb seems to have trouble with calling C++ functions directly.
184 FastAlloc::dump_summary();
188 fast_alloc_oldest(int n
)
190 FastAlloc::dump_oldest(n
);
195 #endif // NO_FAST_ALLOC