summaryrefslogtreecommitdiffstats
path: root/libcxx/test/support/min_allocator.h
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/test/support/min_allocator.h')
-rw-r--r--libcxx/test/support/min_allocator.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/libcxx/test/support/min_allocator.h b/libcxx/test/support/min_allocator.h
index 68cde7917a2..701159e0481 100644
--- a/libcxx/test/support/min_allocator.h
+++ b/libcxx/test/support/min_allocator.h
@@ -11,6 +11,9 @@
#define MIN_ALLOCATOR_H
#include <cstddef>
+#include <cstdlib>
+#include <cstddef>
+#include <cassert>
#include "test_macros.h"
@@ -39,6 +42,57 @@ public:
friend bool operator!=(bare_allocator x, bare_allocator y) {return !(x == y);}
};
+struct malloc_allocator_base {
+ static size_t alloc_count;
+ static size_t dealloc_count;
+ static bool disable_default_constructor;
+
+ static size_t outstanding_alloc() {
+ assert(alloc_count >= dealloc_count);
+ return (alloc_count - dealloc_count);
+ }
+
+ static void reset() {
+ assert(outstanding_alloc() == 0);
+ disable_default_constructor = false;
+ alloc_count = 0;
+ dealloc_count = 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;
+
+
+template <class T>
+class malloc_allocator : public malloc_allocator_base
+{
+public:
+ typedef T value_type;
+
+ malloc_allocator() TEST_NOEXCEPT { assert(!disable_default_constructor); }
+
+ template <class U>
+ malloc_allocator(malloc_allocator<U>) TEST_NOEXCEPT {}
+
+ T* allocate(std::size_t n)
+ {
+ ++alloc_count;
+ return static_cast<T*>(std::malloc(n*sizeof(T)));
+ }
+
+ void deallocate(T* p, std::size_t)
+ {
+ ++dealloc_count;
+ std::free(static_cast<void*>(p));
+ }
+
+ friend bool operator==(malloc_allocator, malloc_allocator) {return true;}
+ friend bool operator!=(malloc_allocator x, malloc_allocator y) {return !(x == y);}
+};
+
#if TEST_STD_VER >= 11
OpenPOWER on IntegriCloud