X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFBString.h;h=d26b961d2a2bc325334885a7044ecb4fb28b98a7;hb=b015c8c3a85860ce011a6e497ba5076ed8be4232;hp=3e7e292b1fdfe0da1bc31f6c7fcd430c1b39833b;hpb=19b982dc8f1e2a81b34c9bd0b7c2bce92ba9413c;p=folly.git diff --git a/folly/FBString.h b/folly/FBString.h index 3e7e292b..d26b961d 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -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. @@ -32,6 +32,12 @@ #pragma GCC system_header +// When used as std::string replacement always disable assertions. +#ifndef NDEBUG +#define NDEBUG +#define FOLLY_DEFINED_NDEBUG_FOR_FBSTRING +#endif // NDEBUG + // Handle the cases where the fbcode version (folly/Malloc.h) is included // either before or after this inclusion. #ifdef FOLLY_MALLOC_H_ @@ -86,6 +92,7 @@ // FBString cannot use throw when replacing std::string, though it may still // use std::__throw_* +// nolint #define throw FOLLY_FBSTRING_MAY_NOT_USE_THROW #ifdef _LIBSTDCXX_FBSTRING @@ -100,7 +107,10 @@ namespace folly { // has issues when inlining is used, so disable that as well. #if defined(__clang__) # if __has_feature(address_sanitizer) -# if __has_attribute(__no_address_safety_analysis__) +# if __has_attribute(__no_sanitize__) +# define FBSTRING_DISABLE_ADDRESS_SANITIZER \ + __attribute__((__no_sanitize__("address"), __noinline__)) +# elif __has_attribute(__no_address_safety_analysis__) # define FBSTRING_DISABLE_ADDRESS_SANITIZER \ __attribute__((__no_address_safety_analysis__, __noinline__)) # elif __has_attribute(__no_sanitize_address__) @@ -109,8 +119,6 @@ namespace folly { # endif # endif #elif defined (__GNUC__) && \ - (__GNUC__ == 4) && \ - (__GNUC_MINOR__ >= 8) && \ __SANITIZE_ADDRESS__ # define FBSTRING_DISABLE_ADDRESS_SANITIZER \ __attribute__((__no_address_safety_analysis__, __noinline__)) @@ -254,19 +262,10 @@ private: }; */ -/** - * gcc-4.7 throws what appears to be some false positive uninitialized - * warnings for the members of the MediumLarge struct. So, mute them here. - */ -#if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wuninitialized" -#endif - /** * This is the core of the string. The code should work on 32- and - * 64-bit architectures and with any Char size. Porting to big endian - * architectures would require some changes. + * 64-bit and both big- and little-endianan architectures with any + * Char size. * * The storage is selected as follows (assuming we store one-byte * characters on a 64-bit machine): (a) "small" strings between 0 and @@ -278,23 +277,27 @@ private: * reference-counted and copied lazily. the reference count is * allocated right before the character array. * - * The discriminator between these three strategies sits in the two - * most significant bits of the rightmost char of the storage. If - * neither is set, then the string is small (and its length sits in - * the lower-order bits of that rightmost character). If the MSb is - * set, the string is medium width. If the second MSb is set, then the - * string is large. + * The discriminator between these three strategies sits in two + * bits of the rightmost char of the storage. If neither is set, then the + * string is small (and its length sits in the lower-order bits on + * little-endian or the high-order bits on big-endian of that + * rightmost character). If the MSb is set, the string is medium width. + * If the second MSb is set, then the string is large. On little-endian, + * these 2 bits are the 2 MSbs of MediumLarge::capacity_, while on + * big-endian, these 2 bits are the 2 LSbs. This keeps both little-endian + * and big-endian fbstring_core equivalent with merely different ops used + * to extract capacity/category. */ template class fbstring_core { +protected: + static constexpr bool kIsLittleEndian = + __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; + static constexpr bool kIsBigEndian = + __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; + static_assert( + kIsLittleEndian || kIsBigEndian, "unable to identify endianness"); public: - fbstring_core() noexcept { - // Only initialize the tag, will set the MSBs (i.e. the small - // string size) to zero too - ml_.capacity_ = maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char))); - // or: setSmallSize(0); - writeTerminator(); - assert(category() == Category::isSmall && size() == 0); - } + fbstring_core() noexcept { reset(); } fbstring_core(const fbstring_core & rhs) { assert(&rhs != this); @@ -306,18 +309,12 @@ public: "fbstring layout failure"); static_assert(offsetof(MediumLarge, capacity_) == 2 * sizeof(ml_.data_), "fbstring layout failure"); - const size_t size = rhs.smallSize(); - if (size == 0) { - ml_.capacity_ = rhs.ml_.capacity_; - writeTerminator(); - } else { - // Just write the whole thing, don't look at details. In - // particular we need to copy capacity anyway because we want - // to set the size (don't forget that the last character, - // which stores a short string's length, is shared with the - // ml_.capacity field). - ml_ = rhs.ml_; - } + // Just write the whole thing, don't look at details. In + // particular we need to copy capacity anyway because we want + // to set the size (don't forget that the last character, + // which stores a short string's length, is shared with the + // ml_.capacity field). + ml_ = rhs.ml_; assert(category() == Category::isSmall && this->size() == rhs.size()); } else if (rhs.category() == Category::isLarge) { // Large strings are just refcounted @@ -337,8 +334,7 @@ public: // No need for writeTerminator() here, we copied one extra // element just above. ml_.size_ = rhs.ml_.size_; - ml_.capacity_ = (allocSize / sizeof(Char) - 1) - | static_cast(Category::isMedium); + ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium); assert(category() == Category::isMedium); } assert(size() == rhs.size()); @@ -346,14 +342,11 @@ public: } fbstring_core(fbstring_core&& goner) noexcept { - if (goner.category() == Category::isSmall) { - // Just copy, leave the goner in peace - new(this) fbstring_core(goner.small_, goner.smallSize()); - } else { - // Take goner's guts - ml_ = goner.ml_; + // Take goner's guts + ml_ = goner.ml_; + if (goner.category() != Category::isSmall) { // Clean goner's carcass - goner.setSmallSize(0); + goner.reset(); } } @@ -413,16 +406,14 @@ public: ml_.data_ = static_cast(checkedMalloc(allocSize)); fbstring_detail::pod_copy(data, data + size, ml_.data_); ml_.size_ = size; - ml_.capacity_ = (allocSize / sizeof(Char) - 1) - | static_cast(Category::isMedium); + ml_.setCapacity(allocSize / sizeof(Char) - 1, Category::isMedium); } else { // Large strings are allocated differently size_t effectiveCapacity = size; auto const newRC = RefCounted::create(data, & effectiveCapacity); ml_.data_ = newRC->data_; ml_.size_ = size; - ml_.capacity_ = effectiveCapacity - | static_cast(Category::isLarge); + ml_.setCapacity(effectiveCapacity, Category::isLarge); } writeTerminator(); } @@ -457,12 +448,11 @@ public: ml_.data_ = data; ml_.size_ = size; // Don't forget about null terminator - ml_.capacity_ = (allocatedSize - 1) - | static_cast(Category::isMedium); + ml_.setCapacity(allocatedSize - 1, Category::isMedium); } else { // No need for the memory free(data); - setSmallSize(0); + reset(); } } @@ -555,8 +545,7 @@ public: // we have + 1 above. RefCounted::decrementRefs(ml_.data_); ml_.data_ = newRC->data_; - ml_.capacity_ = minCapacity - | static_cast(Category::isLarge); + ml_.setCapacity(minCapacity, Category::isLarge); // size remains unchanged } else { // String is not shared, so let's try to realloc (if needed) @@ -566,8 +555,7 @@ public: RefCounted::reallocate(ml_.data_, ml_.size_, ml_.capacity(), minCapacity); ml_.data_ = newRC->data_; - ml_.capacity_ = minCapacity - | static_cast(Category::isLarge); + ml_.setCapacity(minCapacity, Category::isLarge); writeTerminator(); } assert(capacity() >= minCapacity); @@ -588,8 +576,7 @@ public: (ml_.capacity() + 1) * sizeof(Char), capacityBytes)); writeTerminator(); - ml_.capacity_ = (capacityBytes / sizeof(Char) - 1) - | static_cast(Category::isMedium); + ml_.setCapacity(capacityBytes / sizeof(Char) - 1, Category::isMedium); } else { // Conversion from medium to large string fbstring_core nascent; @@ -612,8 +599,7 @@ public: // No need for writeTerminator(), we wrote it above with + 1. ml_.data_ = newRC->data_; ml_.size_ = size; - ml_.capacity_ = minCapacity - | static_cast(Category::isLarge); + ml_.setCapacity(minCapacity, Category::isLarge); assert(capacity() >= minCapacity); } else if (minCapacity > maxSmallSize) { // medium @@ -626,8 +612,7 @@ public: // No need for writeTerminator(), we wrote it above with + 1. ml_.data_ = data; ml_.size_ = size; - ml_.capacity_ = (allocSizeBytes / sizeof(Char) - 1) - | static_cast(Category::isMedium); + ml_.setCapacity(allocSizeBytes / sizeof(Char) - 1, Category::isMedium); } else { // small // Nothing to do, everything stays put @@ -727,15 +712,18 @@ private: // Disabled fbstring_core & operator=(const fbstring_core & rhs); - struct MediumLarge { - Char * data_; - size_t size_; - size_t capacity_; - - size_t capacity() const { - return capacity_ & capacityExtractMask; - } - }; + // Equivalent to setSmallSize(0), but with specialized + // writeTerminator which doesn't re-check the category after + // capacity_ is overwritten. + void reset() { + // Only initialize the tag, will set the MSBs (i.e. the small + // string size) to zero too. + ml_.capacity_ = kIsLittleEndian + ? maxSmallSize << (8 * (sizeof(size_t) - sizeof(Char))) + : maxSmallSize << 2; + small_[0] = '\0'; + assert(category() == Category::isSmall && size() == 0); + } struct RefCounted { std::atomic refCount_; @@ -804,42 +792,68 @@ private: } }; + typedef std::conditional::type + category_type; + + enum class Category : category_type { + isSmall = 0, + isMedium = kIsLittleEndian + ? sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000 + : 0x2, + isLarge = kIsLittleEndian + ? sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000 + : 0x1, + }; + + Category category() const { + // works for both big-endian and little-endian + return static_cast(ml_.capacity_ & categoryExtractMask); + } + + struct MediumLarge { + Char * data_; + size_t size_; + size_t capacity_; + + size_t capacity() const { + return kIsLittleEndian + ? capacity_ & capacityExtractMask + : capacity_ >> 2; + } + + void setCapacity(size_t cap, Category cat) { + capacity_ = kIsLittleEndian + ? cap | static_cast(cat) + : (cap << 2) | static_cast(cat); + } + }; + union { Char small_[sizeof(MediumLarge) / sizeof(Char)]; MediumLarge ml_; }; - enum { + enum : size_t { lastChar = sizeof(MediumLarge) - 1, maxSmallSize = lastChar / sizeof(Char), maxMediumSize = 254 / sizeof(Char), // coincides with the small // bin size in dlmalloc - categoryExtractMask = sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000, - capacityExtractMask = ~categoryExtractMask, + categoryExtractMask = kIsLittleEndian + ? sizeof(size_t) == 4 ? 0xC0000000 : 0xC000000000000000 + : 0x3, + capacityExtractMask = kIsLittleEndian + ? ~categoryExtractMask + : 0x0 /*unused*/, }; static_assert(!(sizeof(MediumLarge) % sizeof(Char)), "Corrupt memory layout for fbstring."); - typedef std::conditional::type - category_type; - - enum class Category : category_type { - isSmall = 0, - isMedium = sizeof(size_t) == 4 ? 0x80000000 : 0x8000000000000000, - isLarge = sizeof(size_t) == 4 ? 0x40000000 : 0x4000000000000000, - }; - - Category category() const { - // Assumes little endian - return static_cast(ml_.capacity_ & categoryExtractMask); - } - size_t smallSize() const { - assert(category() == Category::isSmall && - static_cast(small_[maxSmallSize]) - <= static_cast(maxSmallSize)); - return static_cast(maxSmallSize) - - static_cast(small_[maxSmallSize]); + assert(category() == Category::isSmall); + auto shift = kIsLittleEndian ? 0 : 2; + auto smallShifted = static_cast(small_[maxSmallSize]) >> shift; + assert(static_cast(maxSmallSize) >= smallShifted); + return static_cast(maxSmallSize) - smallShifted; } void setSmallSize(size_t s) { @@ -847,15 +861,13 @@ private: // so don't assume anything about the previous value of // small_[maxSmallSize]. assert(s <= maxSmallSize); - small_[maxSmallSize] = maxSmallSize - s; + small_[maxSmallSize] = kIsLittleEndian + ? maxSmallSize - s + : (maxSmallSize - s) << 2; writeTerminator(); } }; -#if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic pop -#endif - #ifndef _LIBSTDCXX_FBSTRING /** * Dummy fbstring core that uses an actual std::string. This doesn't @@ -998,7 +1010,23 @@ private: public: // C++11 21.4.2 construct/copy/destroy - explicit basic_fbstring(const A& a = A()) noexcept { + + // Note: while the following two constructors can be (and previously were) + // collapsed into one constructor written this way: + // + // explicit basic_fbstring(const A& a = A()) noexcept { } + // + // This can cause Clang (at least version 3.7) to fail with the error: + // "chosen constructor is explicit in copy-initialization ... + // in implicit initialization of field '(x)' with omitted initializer" + // + // if used in a struct which is default-initialized. Hence the split into + // these two separate constructors. + + basic_fbstring() noexcept : basic_fbstring(A()) { + } + + explicit basic_fbstring(const A&) noexcept { } basic_fbstring(const basic_fbstring& str) @@ -1022,7 +1050,7 @@ public: assign(str, pos, n); } - /* implicit */ basic_fbstring(const value_type* s, const A& a = A()) + /* implicit */ basic_fbstring(const value_type* s, const A& /*a*/ = A()) : store_(s, s ? traits_type::length(s) : (std::__throw_logic_error( @@ -1030,11 +1058,11 @@ public: 0)) { } - basic_fbstring(const value_type* s, size_type n, const A& a = A()) + basic_fbstring(const value_type* s, size_type n, const A& /*a*/ = A()) : store_(s, n) { } - basic_fbstring(size_type n, value_type c, const A& a = A()) { + basic_fbstring(size_type n, value_type c, const A& /*a*/ = A()) { auto const data = store_.expand_noinit(n); fbstring_detail::pod_fill(data, data + n, c); store_.writeTerminator(); @@ -1044,7 +1072,7 @@ public: basic_fbstring(InIt begin, InIt end, typename std::enable_if< !std::is_same::type, - value_type*>::value, const A>::type & a = A()) { + value_type*>::value, const A>::type & /*a*/ = A()) { assign(begin, end); } @@ -1968,11 +1996,18 @@ public: return find_last_not_of(&c, pos, 1); } - basic_fbstring substr(size_type pos = 0, size_type n = npos) const { + basic_fbstring substr(size_type pos = 0, size_type n = npos) const& { enforce(pos <= size(), std::__throw_out_of_range, ""); return basic_fbstring(data() + pos, std::min(n, size() - pos)); } + basic_fbstring substr(size_type pos = 0, size_type n = npos) && { + enforce(pos <= size(), std::__throw_out_of_range, ""); + erase(0, pos); + if (n < size()) resize(n); + return std::move(*this); + } + int compare(const basic_fbstring& str) const { // FIX due to Goncalo N M de Carvalho July 18, 2005 return compare(0, size(), str); @@ -2020,7 +2055,7 @@ private: }; // non-member functions -// C++11 21.4.8.1/2 +// C++11 21.4.8.1/1 template inline basic_fbstring operator+(const basic_fbstring& lhs, @@ -2062,24 +2097,45 @@ basic_fbstring operator+(basic_fbstring&& lhs, return std::move(lhs.append(rhs)); } +// C++11 21.4.8.1/5 template inline basic_fbstring operator+( - const typename basic_fbstring::value_type* lhs, + const E* lhs, const basic_fbstring& rhs) { // basic_fbstring result; - const typename basic_fbstring::size_type len = - basic_fbstring::traits_type::length(lhs); + const auto len = basic_fbstring::traits_type::length(lhs); result.reserve(len + rhs.size()); result.append(lhs, len).append(rhs); return result; } +// C++11 21.4.8.1/6 template inline basic_fbstring operator+( - typename basic_fbstring::value_type lhs, + const E* lhs, + basic_fbstring&& rhs) { + // + const auto len = basic_fbstring::traits_type::length(lhs); + if (rhs.capacity() >= len + rhs.size()) { + // Good, at least we don't need to reallocate + rhs.insert(rhs.begin(), lhs, lhs + len); + return rhs; + } + // Meh, no go. Do it by hand since we have len already. + basic_fbstring result; + result.reserve(len + rhs.size()); + result.append(lhs, len).append(rhs); + return result; +} + +// C++11 21.4.8.1/7 +template +inline +basic_fbstring operator+( + E lhs, const basic_fbstring& rhs) { basic_fbstring result; @@ -2089,11 +2145,29 @@ basic_fbstring operator+( return result; } +// C++11 21.4.8.1/8 +template +inline +basic_fbstring operator+( + E lhs, + basic_fbstring&& rhs) { + // + if (rhs.capacity() > rhs.size()) { + // Good, at least we don't need to reallocate + rhs.insert(rhs.begin(), lhs); + return rhs; + } + // Meh, no go. Forward to operator+(E, const&). + auto const& rhsC = rhs; + return lhs + rhsC; +} + +// C++11 21.4.8.1/9 template inline basic_fbstring operator+( const basic_fbstring& lhs, - const typename basic_fbstring::value_type* rhs) { + const E* rhs) { typedef typename basic_fbstring::size_type size_type; typedef typename basic_fbstring::traits_type traits_type; @@ -2105,11 +2179,22 @@ basic_fbstring operator+( return result; } +// C++11 21.4.8.1/10 +template +inline +basic_fbstring operator+( + basic_fbstring&& lhs, + const E* rhs) { + // + return std::move(lhs += rhs); +} + +// C++11 21.4.8.1/11 template inline basic_fbstring operator+( const basic_fbstring& lhs, - typename basic_fbstring::value_type rhs) { + E rhs) { basic_fbstring result; result.reserve(lhs.size() + 1); @@ -2118,6 +2203,16 @@ basic_fbstring operator+( return result; } +// C++11 21.4.8.1/12 +template +inline +basic_fbstring operator+( + basic_fbstring&& lhs, + E rhs) { + // + return std::move(lhs += rhs); +} + template inline bool operator==(const basic_fbstring& lhs, @@ -2252,20 +2347,20 @@ std::basic_istream< auto err = __ios_base::goodbit; if (sentry) { auto n = is.width(); - if (n == 0) { + if (n <= 0) { n = str.max_size(); } str.erase(); - auto got = is.rdbuf()->sgetc(); - for (; extracted != n && got != T::eof() && !isspace(got); ++extracted) { - // Whew. We get to store this guy + for (auto got = is.rdbuf()->sgetc(); extracted != size_t(n); ++extracted) { + if (got == T::eof()) { + err |= __ios_base::eofbit; + is.width(0); + break; + } + if (isspace(got)) break; str.push_back(got); got = is.rdbuf()->snextc(); } - if (got == T::eof()) { - err |= __ios_base::eofbit; - is.width(0); - } } if (!extracted) { err |= __ios_base::failbit; @@ -2304,6 +2399,9 @@ operator<<( os.setstate(std::ios_base::badbit | std::ios_base::failbit); } } +#elif defined(_MSC_VER) + // MSVC doesn't define __ostream_insert + os.write(str.data(), str.size()); #else std::__ostream_insert(os, str.data(), str.size()); #endif @@ -2461,4 +2559,9 @@ FOLLY_FBSTRING_HASH #undef FBSTRING_LIKELY #undef FBSTRING_UNLIKELY +#ifdef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING +#undef NDEBUG +#undef FOLLY_DEFINED_NDEBUG_FOR_FBSTRING +#endif // FOLLY_DEFINED_NDEBUG_FOR_FBSTRING + #endif // FOLLY_BASE_FBSTRING_H_