* by permission.
*/
-#include <assert.h>
+#include <cassert>
+
#include "base/fast_alloc.hh"
#if !NO_FAST_ALLOC
unsigned FastAlloc::allocCount[Num_Buckets];
#endif
-void *FastAlloc::moreStructs(int bucket)
+void *
+FastAlloc::moreStructs(int bucket)
{
assert(bucket > 0 && bucket < Num_Buckets);
return (p + sz);
}
-
#if FAST_ALLOC_DEBUG
-#include <typeinfo>
-#include <iostream>
#include <iomanip>
+#include <iostream>
#include <map>
#include <string>
+#include <typeinfo>
using namespace std;
inUseNext = next;
}
-
// constructor: marks as in use, add to in-use list
FastAlloc::FastAlloc()
{
inUseNext->inUsePrev = inUsePrev;
}
-
// summarize in-use list
void
FastAlloc::dump_summary()
cout << " count type\n"
<< " ----- ----\n";
for (mapiter = typemap.begin(); mapiter != typemap.end(); ++mapiter)
- {
cout << setw(6) << mapiter->second << " " << mapiter->first << endl;
- }
}
-
// show oldest n items on in-use list
void
FastAlloc::dump_oldest(int n)
{
// sanity check: don't want to crash the debugger if you forget to
// pass in a parameter
- if (n < 0 || n > numInUse)
- {
+ if (n < 0 || n > numInUse) {
cout << "FastAlloc::dump_oldest: bad arg " << n
<< " (" << numInUse << " objects in use" << endl;
return;
for (FastAlloc *p = inUseHead.inUseNext;
p != &inUseHead && n > 0;
p = p->inUseNext, --n)
- {
cout << p << " " << typeid(*p).name() << endl;
- }
}
-
//
// C interfaces to FastAlloc::dump_summary() and FastAlloc::dump_oldest().
// gdb seems to have trouble with calling C++ functions directly.
FastAlloc::dump_oldest(n);
}
-#endif
+#endif // FAST_ALLOC_DEBUG
#endif // NO_FAST_ALLOC
* by permission.
*/
-#ifndef __FAST_ALLOC_H__
-#define __FAST_ALLOC_H__
+#ifndef __BASE_FAST_ALLOC_HH__
+#define __BASE_FAST_ALLOC_HH__
-#include <stddef.h>
+#include <cstddef>
// Fast structure allocator. Designed for small objects that are
// frequently allocated and deallocated. This code is derived from the
#if NO_FAST_ALLOC
-class FastAlloc {
+class FastAlloc
+{
};
#else
-class FastAlloc {
+class FastAlloc
+{
public:
-
static void *allocate(size_t);
static void deallocate(void *, size_t);
#if FAST_ALLOC_DEBUG
FastAlloc();
- FastAlloc(FastAlloc*,FastAlloc*); // for inUseHead, see below
+ FastAlloc(FastAlloc *, FastAlloc *); // for inUseHead, see below
virtual ~FastAlloc();
#else
virtual ~FastAlloc() {}
#endif
};
-
-inline
-int FastAlloc::bucketFor(size_t sz)
+inline int
+FastAlloc::bucketFor(size_t sz)
{
return (sz + Alloc_Quantum - 1) >> Log2_Alloc_Quantum;
}
-
-inline
-void *FastAlloc::allocate(size_t sz)
+inline void *
+FastAlloc::allocate(size_t sz)
{
int b;
void *p;
return p;
}
-
-inline
-void FastAlloc::deallocate(void *p, size_t sz)
+inline void
+FastAlloc::deallocate(void *p, size_t sz)
{
int b;
- if (sz > Max_Alloc_Size)
- {
+ if (sz > Max_Alloc_Size) {
::delete [] (char *)p;
return;
}
#endif
}
-
-inline
-void *FastAlloc::operator new(size_t sz)
+inline void *
+FastAlloc::operator new(size_t sz)
{
return allocate(sz);
}
-
-inline
-void FastAlloc::operator delete(void *p, size_t sz)
+inline void
+FastAlloc::operator delete(void *p, size_t sz)
{
deallocate(p, sz);
}
#endif // NO_FAST_ALLOC
-#endif // __FAST_ALLOC_H__
+#endif // __BASE_FAST_ALLOC_HH__