Fix another race in Notification Queue
[folly.git] / folly / FBVector.h
index 18b1c8cc5f57ea50aaa685c9c7e09c055f691405..1a04eebc99e306c73918247f45b65dace7477193 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <type_traits>
 #include <utility>
 
-#include "folly/Likely.h"
-#include "folly/Malloc.h"
-#include "folly/Traits.h"
+#include <folly/Likely.h>
+#include <folly/Malloc.h>
+#include <folly/Traits.h>
 
 #include <boost/operators.hpp>
 
 // some files expected these from FBVector
 #include <limits>
-#include "folly/Foreach.h"
+#include <folly/Foreach.h>
 #include <boost/type_traits.hpp>
 #include <boost/utility/enable_if.hpp>
 
 //=============================================================================
 // forward declaration
 
-#ifdef FOLLY_BENCHMARK_USE_NS_IFOLLY
-namespace Ifolly {
-#else
 namespace folly {
-#endif
   template <class T, class Allocator = std::allocator<T>>
   class fbvector;
 }
 
-//=============================================================================
-// compatibility
-
-#if __GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7
-// PLEASE UPGRADE TO GCC 4.7 or above
-#define FOLLY_FBV_COMPATIBILITY_MODE
-#endif
-
-#ifndef FOLLY_FBV_COMPATIBILITY_MODE
-
-namespace folly {
-
-template <typename A>
-struct fbv_allocator_traits
-  : std::allocator_traits<A> {};
-
-template <typename T>
-struct fbv_is_nothrow_move_constructible
-  : std::is_nothrow_move_constructible<T> {};
-
-template <typename T, typename... Args>
-struct fbv_is_nothrow_constructible
-  : std::is_nothrow_constructible<T, Args...> {};
-
-template <typename T>
-struct fbv_is_copy_constructible
-  : std::is_copy_constructible<T> {};
-
-}
-
-#else
-
-namespace folly {
-
-template <typename A>
-struct fbv_allocator_traits {
-  static_assert(sizeof(A) == 0,
-    "If you want to use a custom allocator, then you must upgrade to gcc 4.7");
-  // for some old code that deals with this case, see D566719, diff number 10.
-};
-
-template <typename T>
-struct fbv_allocator_traits<std::allocator<T>> {
-  typedef std::allocator<T> A;
-
-  typedef T* pointer;
-  typedef const T* const_pointer;
-  typedef size_t size_type;
-
-  typedef std::false_type propagate_on_container_copy_assignment;
-  typedef std::false_type propagate_on_container_move_assignment;
-  typedef std::false_type propagate_on_container_swap;
-
-  static pointer allocate(A& a, size_type n) {
-    return static_cast<pointer>(::operator new(n * sizeof(T)));
-  }
-  static void deallocate(A& a, pointer p, size_type n) {
-    ::operator delete(p);
-  }
-
-  template <typename R, typename... Args>
-  static void construct(A& a, R* p, Args&&... args) {
-    new (p) R(std::forward<Args>(args)...);
-  }
-  template <typename R>
-  static void destroy(A& a, R* p) {
-    p->~R();
-  }
-
-  static A select_on_container_copy_construction(const A& a) {
-    return a;
-  }
-};
-
-template <typename T>
-struct fbv_is_nothrow_move_constructible
-  : std::false_type {};
-
-template <typename T, typename... Args>
-struct fbv_is_nothrow_constructible
-  : std::false_type {};
-
-template <typename T>
-struct fbv_is_copy_constructible
-  : std::true_type {};
-
-}
-
-#endif
-
 //=============================================================================
 // unrolling
 
@@ -170,11 +76,7 @@ struct fbv_is_copy_constructible
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
-#ifdef FOLLY_BENCHMARK_USE_NS_IFOLLY
-namespace Ifolly {
-#else
 namespace folly {
-#endif
 
 template <class T, class Allocator>
 class fbvector : private boost::totally_ordered<fbvector<T, Allocator>> {
@@ -184,7 +86,7 @@ class fbvector : private boost::totally_ordered<fbvector<T, Allocator>> {
   // implementation
 private:
 
-  typedef folly::fbv_allocator_traits<Allocator> A;
+  typedef std::allocator_traits<Allocator> A;
 
   struct Impl : public Allocator {
     // typedefs
@@ -221,7 +123,7 @@ private:
       if (usingStdAllocator::value) {
         return static_cast<T*>(malloc(n * sizeof(T)));
       } else {
-        return folly::fbv_allocator_traits<Allocator>::allocate(*this, n);
+        return std::allocator_traits<Allocator>::allocate(*this, n);
       }
     }
 
@@ -229,7 +131,7 @@ private:
       if (usingStdAllocator::value) {
         free(p);
       } else {
-        folly::fbv_allocator_traits<Allocator>::deallocate(*this, p, n);
+        std::allocator_traits<Allocator>::deallocate(*this, p, n);
       }
     }
 
@@ -316,7 +218,7 @@ public:
 private:
 
   typedef std::integral_constant<bool,
-      std::has_trivial_copy_constructor<T>::value &&
+      boost::has_trivial_copy_constructor<T>::value &&
       sizeof(T) <= 16 // don't force large structures to be passed by value
     > should_pass_by_value;
   typedef typename std::conditional<
@@ -360,7 +262,7 @@ private:
     if (usingStdAllocator::value) {
       new (p) U(std::forward<Args>(args)...);
     } else {
-      folly::fbv_allocator_traits<Allocator>::construct(
+      std::allocator_traits<Allocator>::construct(
         impl_, p, std::forward<Args>(args)...);
     }
   }
@@ -372,7 +274,7 @@ private:
 
   template <typename U, typename... Args>
   static void S_construct_a(Allocator& a, U* p, Args&&... args) {
-    folly::fbv_allocator_traits<Allocator>::construct(
+    std::allocator_traits<Allocator>::construct(
       a, p, std::forward<Args>(args)...);
   }
 
@@ -384,7 +286,7 @@ private:
     if (usingStdAllocator::value) {
       *p = arg;
     } else {
-      folly::fbv_allocator_traits<Allocator>::construct(impl_, p, arg);
+      std::allocator_traits<Allocator>::construct(impl_, p, arg);
     }
   }
 
@@ -397,7 +299,7 @@ private:
   template <typename U, typename Enable = typename
     std::enable_if<std::is_scalar<U>::value>::type>
   static void S_construct_a(Allocator& a, U* p, U arg) {
-    folly::fbv_allocator_traits<Allocator>::construct(a, p, arg);
+    std::allocator_traits<Allocator>::construct(a, p, arg);
   }
 
   // const& optimization
@@ -407,7 +309,7 @@ private:
     if (usingStdAllocator::value) {
       new (p) U(value);
     } else {
-      folly::fbv_allocator_traits<Allocator>::construct(impl_, p, value);
+      std::allocator_traits<Allocator>::construct(impl_, p, value);
     }
   }
 
@@ -420,7 +322,7 @@ private:
   template <typename U, typename Enable = typename
     std::enable_if<!std::is_scalar<U>::value>::type>
   static void S_construct_a(Allocator& a, U* p, const U& value) {
-    folly::fbv_allocator_traits<Allocator>::construct(a, p, value);
+    std::allocator_traits<Allocator>::construct(a, p, value);
   }
 
   //---------------------------------------------------------------------------
@@ -428,9 +330,9 @@ private:
 
   void M_destroy(T* p) noexcept {
     if (usingStdAllocator::value) {
-      if (!std::has_trivial_destructor<T>::value) p->~T();
+      if (!boost::has_trivial_destructor<T>::value) p->~T();
     } else {
-      folly::fbv_allocator_traits<Allocator>::destroy(impl_, p);
+      std::allocator_traits<Allocator>::destroy(impl_, p);
     }
   }
 
@@ -461,12 +363,12 @@ private:
   // allocator
   static void S_destroy_range_a(Allocator& a, T* first, T* last) noexcept {
     for (; first != last; ++first)
-      folly::fbv_allocator_traits<Allocator>::destroy(a, first);
+      std::allocator_traits<Allocator>::destroy(a, first);
   }
 
   // optimized
   static void S_destroy_range(T* first, T* last) noexcept {
-    if (!std::has_trivial_destructor<T>::value) {
+    if (!boost::has_trivial_destructor<T>::value) {
       // EXPERIMENTAL DATA on fbvector<vector<int>> (where each vector<int> has
       //  size 0).
       // The unrolled version seems to work faster for small to medium sized
@@ -523,7 +425,7 @@ private:
     auto e = dest + sz;
     try {
       for (; b != e; ++b)
-        folly::fbv_allocator_traits<Allocator>::construct(a, b,
+        std::allocator_traits<Allocator>::construct(a, b,
           std::forward<Args>(args)...);
     } catch (...) {
       S_destroy_range_a(a, dest, b);
@@ -605,7 +507,7 @@ private:
     auto b = dest;
     try {
       for (; first != last; ++first, ++b)
-        folly::fbv_allocator_traits<Allocator>::construct(a, b, *first);
+        std::allocator_traits<Allocator>::construct(a, b, *first);
     } catch (...) {
       S_destroy_range_a(a, dest, b);
       throw;
@@ -730,9 +632,9 @@ private:
     > relocate_use_memcpy;
 
   typedef std::integral_constant<bool,
-      (folly::fbv_is_nothrow_move_constructible<T>::value
+      (std::is_nothrow_move_constructible<T>::value
        && usingStdAllocator::value)
-      || !folly::fbv_is_copy_constructible<T>::value
+      || !std::is_copy_constructible<T>::value
     > relocate_use_move;
 
   // move
@@ -769,11 +671,11 @@ private:
   void relocate_undo(T* dest, T* first, T* last) noexcept {
     if (folly::IsRelocatable<T>::value && usingStdAllocator::value) {
       // used memcpy, old data is still valid, nothing to do
-    } else if (folly::fbv_is_nothrow_move_constructible<T>::value &&
+    } else if (std::is_nothrow_move_constructible<T>::value &&
                usingStdAllocator::value) {
       // noexcept move everything back, aka relocate_move
       relocate_move(first, dest, dest + (last - first));
-    } else if (!folly::fbv_is_copy_constructible<T>::value) {
+    } else if (!std::is_copy_constructible<T>::value) {
       // weak guarantee
       D_destroy_range_a(dest, dest + (last - first));
     } else {
@@ -803,12 +705,7 @@ public:
   template <class It, class Category = typename
             std::iterator_traits<It>::iterator_category>
   fbvector(It first, It last, const Allocator& a = Allocator())
-    #ifndef FOLLY_FBV_COMPATIBILITY_MODE
     : fbvector(first, last, a, Category()) {}
-    #else
-    : impl_(std::distance(first, last), a)
-    { fbvector_init(first, last, Category()); }
-    #endif
 
   fbvector(const fbvector& other)
     : impl_(other.size(), A::select_on_container_copy_construction(other.impl_))
@@ -817,12 +714,7 @@ public:
   fbvector(fbvector&& other) noexcept : impl_(std::move(other.impl_)) {}
 
   fbvector(const fbvector& other, const Allocator& a)
-    #ifndef FOLLY_FBV_COMPATIBILITY_MODE
     : fbvector(other.begin(), other.end(), a) {}
-    #else
-    : impl_(other.size(), a)
-    { fbvector_init(other.begin(), other.end(), std::forward_iterator_tag()); }
-    #endif
 
   fbvector(fbvector&& other, const Allocator& a) : impl_(a) {
     if (impl_ == other.impl_) {
@@ -834,12 +726,7 @@ public:
   }
 
   fbvector(std::initializer_list<T> il, const Allocator& a = Allocator())
-    #ifndef FOLLY_FBV_COMPATIBILITY_MODE
     : fbvector(il.begin(), il.end(), a) {}
-    #else
-    : impl_(std::distance(il.begin(), il.end()), a)
-    { fbvector_init(il.begin(), il.end(), std::forward_iterator_tag()); }
-    #endif
 
   ~fbvector() = default; // the cleanup occurs in impl_
 
@@ -908,7 +795,6 @@ public:
 
 private:
 
-  #ifndef FOLLY_FBV_COMPATIBILITY_MODE
   // contract dispatch for iterator types fbvector(It first, It last)
   template <class ForwardIterator>
   fbvector(ForwardIterator first, ForwardIterator last,
@@ -922,21 +808,6 @@ private:
     : impl_(a)
     { for (; first != last; ++first) emplace_back(*first); }
 
-  #else
-  // contract dispatch for iterator types without constructor forwarding
-  template <class ForwardIterator>
-  void
-  fbvector_init(ForwardIterator first, ForwardIterator last,
-                std::forward_iterator_tag)
-    { M_uninitialized_copy_e(first, last); }
-
-  template <class InputIterator>
-  void
-  fbvector_init(InputIterator first, InputIterator last,
-                std::input_iterator_tag)
-    { for (; first != last; ++first) emplace_back(*first); }
-  #endif
-
   // contract dispatch for allocator movement in operator=(fbvector&&)
   void
   moveFrom(fbvector&& other, std::true_type) {
@@ -1095,7 +966,7 @@ public:
     if (impl_.b_)
       M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
     impl_.z_ = newB + newCap;
-    impl_.e_ += newB - impl_.b_; // speed hax
+    impl_.e_ = newB + (impl_.e_ - impl_.b_);
     impl_.b_ = newB;
   }
 
@@ -1109,7 +980,7 @@ public:
     void* p = impl_.b_;
     if ((rallocm && usingStdAllocator::value) &&
         newCapacityBytes >= folly::jemallocMinInPlaceExpandable &&
-        rallocm(&p, NULL, newCapacityBytes, 0, ALLOCM_NO_MOVE)
+        rallocm(&p, nullptr, newCapacityBytes, 0, ALLOCM_NO_MOVE)
           == ALLOCM_SUCCESS) {
       impl_.z_ += newCap - oldCap;
     } else {
@@ -1128,7 +999,7 @@ public:
       if (impl_.b_)
         M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
       impl_.z_ = newB + newCap;
-      impl_.e_ += newB - impl_.b_; // speed hax
+      impl_.e_ = newB + (impl_.e_ - impl_.b_);
       impl_.b_ = newB;
     }
   }
@@ -1144,7 +1015,7 @@ private:
 
     auto const newCapacityBytes = folly::goodMallocSize(n * sizeof(T));
     void* p = impl_.b_;
-    if (rallocm(&p, NULL, newCapacityBytes, 0, ALLOCM_NO_MOVE)
+    if (rallocm(&p, nullptr, newCapacityBytes, 0, ALLOCM_NO_MOVE)
         == ALLOCM_SUCCESS) {
       impl_.z_ = impl_.b_ + newCapacityBytes / sizeof(T);
       return true;
@@ -1412,9 +1283,15 @@ private: // we have the private section first because it defines some macros
         impl_.e_ += n;
       } else {
         D_uninitialized_move_a(impl_.e_, impl_.e_ - n, impl_.e_);
+        try {
+          std::copy_backward(std::make_move_iterator(position),
+                             std::make_move_iterator(impl_.e_ - n), impl_.e_);
+        } catch (...) {
+          D_destroy_range_a(impl_.e_ - n, impl_.e_ + n);
+          impl_.e_ -= n;
+          throw;
+        }
         impl_.e_ += n;
-        std::copy_backward(std::make_move_iterator(position),
-                           std::make_move_iterator(impl_.e_ - n), impl_.e_);
         D_destroy_range_a(position, position + n);
       }
     }
@@ -1653,7 +1530,6 @@ void fbvector<T, Allocator>::emplace_back_aux(Args&&... args) {
     size_type lower = folly::goodMallocSize(sizeof(T) + size() * sizeof(T));
     size_type upper = byte_sz;
     size_type extra = upper - lower;
-    assert(extra >= 0);
 
     void* p = impl_.b_;
     size_t actual;
@@ -1761,4 +1637,3 @@ void attach(fbvector<T, A>& v, T* data, size_t sz, size_t cap) {
 } // namespace folly
 
 #endif // FOLLY_FBVECTOR_H
-