Fix bug in reserve() and shrink_to_fit().
authorChristian Kamm <christian.kamm@celeraone.com>
Mon, 25 Mar 2013 08:54:26 +0000 (09:54 +0100)
committerJordan DeLong <jdelong@fb.com>
Sun, 21 Apr 2013 20:21:27 +0000 (13:21 -0700)
Summary:
impl_.e_ += newB - impl_.b_; fails when the difference
between newB and impl_.b_ isn't a multiple of sizeof(T).

Test Plan: .

Reviewed By: oyamauchi@fb.com

FB internal diff: D774754

folly/FBVector.h
folly/test/FBVectorTest.cpp

index fbae7e2c86dc59643fc5738d82eba1979ea1cc95..9e593c608a56dabf8e556b77aa6ef1ca77fd5a33 100644 (file)
@@ -1095,7 +1095,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;
   }
 
@@ -1128,7 +1128,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;
     }
   }
index 68380279916cd0b76f542a181d75357141ec81c7..a8a1b2ac4632c25a2f71885d045b94887a93f0b4 100644 (file)
@@ -245,6 +245,17 @@ TEST(FBVector, move_iterator) {
   EXPECT_EQ(fbvi3, base);
 }
 
+TEST(FBVector, reserve_consistency) {
+  struct S { int64_t a, b, c, d; };
+
+  fbvector<S> fb1;
+  for (size_t i = 0; i < 1000; ++i) {
+    fb1.reserve(1);
+    EXPECT_EQ(fb1.size(), 0);
+    fb1.shrink_to_fit();
+  }
+}
+
 int main(int argc, char** argv) {
   testing::InitGoogleTest(&argc, argv);
   google::ParseCommandLineFlags(&argc, &argv, true);