frutiger created this revision.
frutiger added reviewers: mclow.lists, EricWF.
frutiger added a subscriber: cfe-commits.

This commit adds the required constructor overloads and member functions to
make the supporting allocators actually valid allocators.  The implementation
of 'allocate' defers to 'std::malloc', and correspondingly that of 'deallocate'
defers to 'std::free'.

This commit also changes supporting allocators 'A1' to track the number of
elements requested and 'A2' to track the hint that was requested in order to
facilitate testing.

Two (seemingly unrelated) tests seem to fail:

Failing Tests (2):
    libc++ :: std/utilities/tuple/tuple.general/tuple.smartptr.pass.cpp
    libc++ :: std/utilities/tuple/tuple.tuple/tuple.creation/tuple_cat.pass.cpp

http://reviews.llvm.org/D16967

Files:
  
test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size.pass.cpp
  
test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size_hint.pass.cpp
  
test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp
  test/support/allocators.h

Index: test/support/allocators.h
===================================================================
--- test/support/allocators.h
+++ test/support/allocators.h
@@ -10,6 +10,7 @@
 #ifndef ALLOCATORS_H
 #define ALLOCATORS_H
 
+#include <cstdlib>
 #include <type_traits>
 #include <utility>
 
@@ -30,7 +31,7 @@
 
     static bool copy_called;
     static bool move_called;
-    static bool allocate_called;
+    static std::size_t allocate_called;
     static std::pair<T*, std::size_t> deallocate_called;
 
     A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
@@ -45,12 +46,13 @@
 
     T* allocate(std::size_t n)
     {
-        allocate_called = true;
-        return (T*)n;
+        allocate_called = n;
+        return static_cast<T*>(std::malloc(sizeof(T) * n));
     }
 
     void deallocate(T* p, std::size_t n)
     {
+        std::free(p);
         deallocate_called = std::pair<T*, std::size_t>(p, n);
     }
 
@@ -59,7 +61,7 @@
 
 template <class T> bool A1<T>::copy_called = false;
 template <class T> bool A1<T>::move_called = false;
-template <class T> bool A1<T>::allocate_called = false;
+template <class T> std::size_t A1<T>::allocate_called = 0;
 template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
 
 template <class T, class U>
@@ -94,23 +96,36 @@
 
     static bool copy_called;
     static bool move_called;
-    static bool allocate_called;
+    static const void *allocate_called;
 
     A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
     A2(A2&& a)      TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
     A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
     A2& operator=(A2&& a)      TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
 
+    template <class U>
+        A2(const A2<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
+
+    T* allocate(std::size_t n)
+    {
+        return static_cast<T*>(std::malloc(sizeof(T) * n));
+    }
+
     T* allocate(std::size_t n, const void* hint)
     {
-        allocate_called = true;
-        return (T*)hint;
+        allocate_called = hint;
+        return static_cast<T*>(std::malloc(sizeof(T) * n));
+    }
+
+    void deallocate(T* p, std::size_t n)
+    {
+        std::free(p);
     }
 };
 
 template <class T> bool A2<T>::copy_called = false;
 template <class T> bool A2<T>::move_called = false;
-template <class T> bool A2<T>::allocate_called = false;
+template <class T> const void *A2<T>::allocate_called = (const void*)0;
 
 template <class T, class U>
 inline
@@ -150,6 +165,19 @@
     A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
     A3& operator=(A3&& a)      TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
 
+    template <class U>
+        A3(const A3<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
+
+    T* allocate(std::size_t n)
+    {
+        return static_cast<T*>(std::malloc(sizeof(T) * n));
+    }
+
+    void deallocate(T* p, std::size_t n)
+    {
+        std::free(p);
+    }
+
     template <class U, class ...Args>
     void construct(U* p, Args&& ...args)
     {
Index: test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp
===================================================================
--- test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp
+++ test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp
@@ -26,20 +26,23 @@
     {
         typedef std::scoped_allocator_adaptor<A1<int>> A;
         A a;
-        a.deallocate((int*)10, 20);
-        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>((int*)10, 20)));
+        int* p = a.allocate(20);
+        a.deallocate(p, 20);
+        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>(p, 20)));
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>> A;
         A a;
-        a.deallocate((int*)10, 20);
-        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>((int*)10, 20)));
+        int* p = a.allocate(20);
+        a.deallocate(p, 20);
+        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>(p, 20)));
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>, A3<int>> A;
         A a;
-        a.deallocate((int*)10, 20);
-        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>((int*)10, 20)));
+        int* p = a.allocate(20);
+        a.deallocate(p, 20);
+        assert((A1<int>::deallocate_called == std::pair<int*, std::size_t>(p, 20)));
     }
 
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Index: test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size_hint.pass.cpp
===================================================================
--- test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size_hint.pass.cpp
+++ test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size_hint.pass.cpp
@@ -27,44 +27,44 @@
         typedef std::scoped_allocator_adaptor<A1<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)0) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10, (const void*)0);
+        assert(A1<int>::allocate_called == 10);
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)10) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10, (const void*)10);
+        assert(A1<int>::allocate_called == 10);
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>, A3<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)20) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10, (const void*)20);
+        assert(A1<int>::allocate_called == 10);
     }
 
     {
         typedef std::scoped_allocator_adaptor<A2<int>> A;
         A a;
-        A2<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)0) == (int*)0);
-        assert(A2<int>::allocate_called == true);
+        A2<int>::allocate_called = (const void*)50;
+        a.allocate(10, (const void*)0);
+        assert(A2<int>::allocate_called == (const void*)0);
     }
     {
         typedef std::scoped_allocator_adaptor<A2<int>, A2<int>> A;
         A a;
-        A2<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)10) == (int*)10);
-        assert(A2<int>::allocate_called == true);
+        A2<int>::allocate_called = (const void*)50;
+        a.allocate(10, (const void*)10);
+        assert(A2<int>::allocate_called == (const void*)10);
     }
     {
         typedef std::scoped_allocator_adaptor<A2<int>, A2<int>, A3<int>> A;
         A a;
-        A2<int>::allocate_called = false;
-        assert(a.allocate(10, (const void*)20) == (int*)20);
-        assert(A2<int>::allocate_called == true);
+        A2<int>::allocate_called = (const void*)50;
+        a.allocate(10, (const void*)20);
+        assert(A2<int>::allocate_called == (const void*)20);
     }
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 }
Index: test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size.pass.cpp
===================================================================
--- test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size.pass.cpp
+++ test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size.pass.cpp
@@ -27,22 +27,22 @@
         typedef std::scoped_allocator_adaptor<A1<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10);
+        assert(A1<int>::allocate_called == 10);
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10);
+        assert(A1<int>::allocate_called == 10);
     }
     {
         typedef std::scoped_allocator_adaptor<A1<int>, A2<int>, A3<int>> A;
         A a;
         A1<int>::allocate_called = false;
-        assert(a.allocate(10) == (int*)10);
-        assert(A1<int>::allocate_called == true);
+        a.allocate(10);
+        assert(A1<int>::allocate_called == 10);
     }
 
 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to