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
36 #pragma implementation
40 #include "base/fast_alloc.hh"
42 void *FastAlloc::freeLists
[Num_Buckets
];
44 #ifdef FAST_ALLOC_STATS
45 unsigned FastAlloc::newCount
[Num_Buckets
];
46 unsigned FastAlloc::deleteCount
[Num_Buckets
];
47 unsigned FastAlloc::allocCount
[Num_Buckets
];
50 void *FastAlloc::moreStructs(int bucket
)
52 assert(bucket
> 0 && bucket
< Num_Buckets
);
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
];
58 #ifdef FAST_ALLOC_STATS
62 freeLists
[bucket
] = p
;
63 for (int i
= 0; i
< (nstructs
-2); ++i
, p
+= sz
)
71 #ifdef FAST_ALLOC_DEBUG
81 // count of in-use FastAlloc objects
82 int FastAlloc::numInUse
;
84 // dummy head & tail object for doubly linked list of in-use FastAlloc
86 FastAlloc
FastAlloc::inUseHead(&FastAlloc::inUseHead
, &FastAlloc::inUseHead
);
88 // special constructor for dummy head: make inUsePrev & inUseNext
90 FastAlloc::FastAlloc(FastAlloc
*prev
, FastAlloc
*next
)
97 // constructor: marks as in use, add to in-use list
98 FastAlloc::FastAlloc()
100 // mark this object in use
106 // add to tail of list of in-use objects ("before" dummy head)
107 FastAlloc
*myNext
= &inUseHead
;
108 FastAlloc
*myPrev
= inUseHead
.inUsePrev
;
112 myPrev
->inUseNext
= this;
113 myNext
->inUsePrev
= this;
116 // destructor: mark not in use, remove from in-use list
117 FastAlloc::~FastAlloc()
123 assert(numInUse
>= 0);
125 // remove me from in-use list
126 inUsePrev
->inUseNext
= inUseNext
;
127 inUseNext
->inUsePrev
= inUsePrev
;
131 // summarize in-use list
133 FastAlloc::dump_summary()
135 map
<string
, int> typemap
;
137 for (FastAlloc
*p
= inUseHead
.inUseNext
; p
!= &inUseHead
; p
= p
->inUseNext
)
139 ++typemap
[typeid(*p
).name()];
142 map
<string
, int>::const_iterator mapiter
;
144 cout
<< " count type\n"
146 for (mapiter
= typemap
.begin(); mapiter
!= typemap
.end(); ++mapiter
)
148 cout
<< setw(6) << mapiter
->second
<< " " << mapiter
->first
<< endl
;
153 // show oldest n items on in-use list
155 FastAlloc::dump_oldest(int n
)
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
)
161 cout
<< "FastAlloc::dump_oldest: bad arg " << n
162 << " (" << numInUse
<< " objects in use" << endl
;
166 for (FastAlloc
*p
= inUseHead
.inUsePrev
;
167 p
!= &inUseHead
&& n
> 0;
168 p
= p
->inUsePrev
, --n
)
170 cout
<< p
<< " " << typeid(*p
).name() << endl
;
176 // C interfaces to FastAlloc::dump_summary() and FastAlloc::dump_oldest().
177 // gdb seems to have trouble with calling C++ functions directly.
182 FastAlloc::dump_summary();
186 fast_alloc_oldest(int n
)
188 FastAlloc::dump_oldest(n
);