[libcxx-commits] [libcxx] r367606 - Teach malloc_allocator how to count bytes

Eric Fiselier via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 1 12:52:46 PDT 2019


Author: ericwf
Date: Thu Aug  1 12:52:46 2019
New Revision: 367606

URL: http://llvm.org/viewvc/llvm-project?rev=367606&view=rev
Log:
Teach malloc_allocator how to count bytes

Modified:
    libcxx/trunk/test/support/min_allocator.h

Modified: libcxx/trunk/test/support/min_allocator.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/min_allocator.h?rev=367606&r1=367605&r2=367606&view=diff
==============================================================================
--- libcxx/trunk/test/support/min_allocator.h (original)
+++ libcxx/trunk/test/support/min_allocator.h Thu Aug  1 12:52:46 2019
@@ -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));
     }
 




More information about the libcxx-commits mailing list