A few fixes for clang support
[folly.git] / folly / FBString.h
index 1c8ec1d308a5d9b39233d9411e435461d5d167b9..a0a86f466e5bf1f31b6d16a79e23735895ae996f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2013 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 <cassert>
 
 #include "folly/Traits.h"
-#include "folly/Likely.h"
 #include "folly/Malloc.h"
 #include "folly/Hash.h"
 
 #endif
 
+// We defined these here rather than including Likely.h to avoid
+// redefinition errors when fbstring is imported into libstdc++.
+#define FBSTRING_LIKELY(x)   (__builtin_expect((x), 1))
+#define FBSTRING_UNLIKELY(x) (__builtin_expect((x), 0))
+
 #include <atomic>
 #include <limits>
 #include <type_traits>
@@ -274,7 +278,7 @@ public:
   fbstring_core() {
     // Only initialize the tag, will set the MSBs (i.e. the small
     // string size) to zero too
-    ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - 1));
+    ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char)));
     // or: setSmallSize(0);
     writeTerminator();
     assert(category() == isSmall && size() == 0);
@@ -641,7 +645,7 @@ public:
 
   void push_back(Char c) {
     assert(capacity() >= size());
-    size_t sz, cp;
+    size_t sz;
     if (category() == isSmall) {
       sz = smallSize();
       if (sz < maxSmallSize) {
@@ -653,14 +657,16 @@ public:
       reserve(maxSmallSize * 2);
     } else {
       sz = ml_.size_;
-      cp = capacity();  // != ml_.capacity() for isShared()
-      if (sz == cp) reserve(cp * 3 / 2);
+      if (sz == capacity()) {  // always true for isShared()
+        reserve(sz * 3 / 2);  // ensures not shared
+      }
     }
+    assert(!isShared());
     assert(capacity() >= sz + 1);
     // Category can't be small - we took care of that above
     assert(category() == isMedium || category() == isLarge);
     ml_.size_ = sz + 1;
-    mutable_data()[sz] = c;
+    ml_.data_[sz] = c;
     writeTerminator();
   }
 
@@ -727,7 +733,7 @@ private:
       return static_cast<RefCounted*>(
         static_cast<void*>(
           static_cast<unsigned char*>(static_cast<void*>(p))
-          - offsetof(RefCounted, data_)));
+          - sizeof(refCount_)));
     }
 
     static size_t refs(Char * p) {
@@ -1131,14 +1137,24 @@ public:
     return const_reverse_iterator(begin());
   }
 
-  // Non-standard functions. They intentionally return by value to
-  // reduce pressure on the reference counting mechanism.
-  value_type front() const { return *begin(); }
-  value_type back() const {
+  // Added by C++11
+  // C++11 21.4.5, element access:
+  const value_type& front() const { return *begin(); }
+  const value_type& back() const {
+    assert(!empty());
+    // Should be begin()[size() - 1], but that branches twice
+    return *(end() - 1);
+  }
+  value_type& front() { return *begin(); }
+  value_type& back() {
     assert(!empty());
-    return begin()[size() - 1];
+    // Should be begin()[size() - 1], but that branches twice
+    return *(end() - 1);
+  }
+  void pop_back() {
+    assert(!empty());
+    store_.shrink(1);
   }
-  void pop_back() { assert(!empty()); store_.shrink(1); }
 
   // 21.3.3 capacity:
   size_type size() const { return store_.size(); }
@@ -1247,7 +1263,7 @@ public:
     Invariant checker(*this);
     (void) checker;
 #endif
-    if (UNLIKELY(!n)) {
+    if (FBSTRING_UNLIKELY(!n)) {
       // Unlikely but must be done
       return *this;
     }
@@ -1259,8 +1275,8 @@ public:
     // std::less_equal, which is guaranteed to offer a total order
     // over pointers. See discussion at http://goo.gl/Cy2ya for more
     // info.
-    static const std::less_equal<const value_type*> le;
-    if (UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
+    std::less_equal<const value_type*> le;
+    if (FBSTRING_UNLIKELY(le(oldData, s) && !le(oldData + oldSize, s))) {
       assert(le(s + n, oldData + oldSize));
       const size_type offset = s - oldData;
       store_.reserve(oldSize + n);
@@ -2311,4 +2327,7 @@ struct hash< ::folly::fbstring> {
 
 #endif // _LIBSTDCXX_FBSTRING
 
+#undef FBSTRING_LIKELY
+#undef FBSTRING_UNLIKELY
+
 #endif // FOLLY_BASE_FBSTRING_H_