Teach malloc_allocator how to count bytes

llvm-svn: 367606
This commit is contained in:
Eric Fiselier 2019-08-01 19:52:46 +00:00
parent 3eb5aec61f
commit 8f2124b47a
1 changed files with 9 additions and 3 deletions

View File

@ -81,6 +81,7 @@ public:
};
struct malloc_allocator_base {
static size_t outstanding_bytes;
static size_t alloc_count;
static size_t dealloc_count;
static bool disable_default_constructor;
@ -93,12 +94,13 @@ struct malloc_allocator_base {
static void reset() {
assert(outstanding_alloc() == 0);
disable_default_constructor = false;
outstanding_bytes = 0;
alloc_count = 0;
dealloc_count = 0;
}
};
size_t malloc_allocator_base::outstanding_bytes = 0;
size_t malloc_allocator_base::alloc_count = 0;
size_t malloc_allocator_base::dealloc_count = 0;
bool malloc_allocator_base::disable_default_constructor = false;
@ -117,13 +119,17 @@ public:
T* allocate(std::size_t n)
{
const size_t nbytes = n*sizeof(T);
++alloc_count;
return static_cast<T*>(std::malloc(n*sizeof(T)));
outstanding_bytes += nbytes;
return static_cast<T*>(std::malloc(nbytes));
}
void deallocate(T* p, std::size_t)
void deallocate(T* p, std::size_t n)
{
const size_t nbytes = n*sizeof(T);
++dealloc_count;
outstanding_bytes -= nbytes;
std::free(static_cast<void*>(p));
}