Update SSLContext to use folly::Random and discrete_distribution
[folly.git] / folly / Padded.h
index f7a283c69ee8e893b54cad48ff4b84ebeed36fe4..50fb8d78a5c4583acb78ff504c9249a233a21eb3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
 #ifndef FOLLY_PADDED_H_
 #define FOLLY_PADDED_H_
 
+#include <algorithm>
 #include <cassert>
 #include <cstdint>
 #include <cstring>
@@ -28,6 +29,7 @@
 #include <boost/iterator/iterator_adaptor.hpp>
 
 #include <folly/Portability.h>
+#include <folly/ContainerTraits.h>
 
 /**
  * Code that aids in storing data aligned on block (possibly cache-line)
@@ -350,7 +352,7 @@ class Adaptor {
 
   Adaptor(const Adaptor&) = default;
   Adaptor& operator=(const Adaptor&) = default;
-  Adaptor(Adaptor&& other)
+  Adaptor(Adaptor&& other) noexcept
     : c_(std::move(other.c_)),
       lastCount_(other.lastCount_) {
     other.lastCount_ = Node::kElementCount;
@@ -424,12 +426,13 @@ class Adaptor {
     return c_.back().data()[lastCount_ - 1];
   }
 
+  template <typename... Args>
+  void emplace_back(Args&&... args) {
+    new (allocate_back()) value_type(std::forward<Args>(args)...);
+  }
+
   void push_back(value_type x) {
-    if (lastCount_ == Node::kElementCount) {
-      c_.push_back(Node());
-      lastCount_ = 0;
-    }
-    c_.back().data()[lastCount_++] = std::move(x);
+    emplace_back(std::move(x));
   }
 
   void pop_back() {
@@ -490,6 +493,14 @@ class Adaptor {
   }
 
  private:
+  value_type* allocate_back() {
+    if (lastCount_ == Node::kElementCount) {
+      container_emplace_back_or_push_back(c_);
+      lastCount_ = 0;
+    }
+    return &c_.back().data()[lastCount_++];
+  }
+
   static Node fullNode(const value_type& value) {
     Node n;
     std::fill(n.data(), n.data() + kElementsPerNode, value);