/*
- * 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.
*/
/*
- * For high-level documentation and usage examples see folly/doc/small_vector.md
+ * For high-level documentation and usage examples see
+ * folly/docs/small_vector.md
*
* @author Jordan DeLong <delong.j@fb.com>
*/
#include "folly/Malloc.h"
-#if defined(__GNUC__) && defined(__x86_64__)
+#if defined(__GNUC__) && FOLLY_X64
# include "folly/SmallLocks.h"
# define FB_PACKED __attribute__((packed))
#else
# define FB_PACKED
#endif
-#ifdef FOLLY_HAVE_MALLOC_SIZE
+#if FOLLY_HAVE_MALLOC_SIZE
extern "C" std::size_t malloc_size(const void*);
-# ifndef FOLLY_HAVE_MALLOC_USABLE_SIZE
+# if !FOLLY_HAVE_MALLOC_USABLE_SIZE
# define malloc_usable_size malloc_size
# endif
# ifndef malloc_usable_size
# endif
#endif
+// Ignore shadowing warnings within this file, so includers can use -Wshadow.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+
namespace folly {
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
template<class T, std::size_t M, class A, class B, class C>
-struct small_vector;
+class small_vector;
//////////////////////////////////////////////////////////////////////
*/
template<class T>
typename std::enable_if<
- !boost::has_trivial_copy<T>::value
+ !FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveToUninitialized(T* first, T* last, T* out) {
auto const count = last - first;
}
}
- // Specialization for trivially copyable types. (TODO: change to
- // std::is_trivially_copyable when that works.)
+ // Specialization for trivially copyable types.
template<class T>
typename std::enable_if<
- boost::has_trivial_copy<T>::value
+ FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveToUninitialized(T* first, T* last, T* out) {
std::memmove(out, first, (last - first) * sizeof *first);
*/
template<class T>
typename std::enable_if<
- !boost::has_trivial_copy<T>::value
+ !FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
if (lastConstructed == realLast) {
// change to std::is_trivially_copyable when that works.)
template<class T>
typename std::enable_if<
- boost::has_trivial_copy<T>::value
+ FOLLY_IS_TRIVIALLY_COPYABLE(T)
>::type
moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
std::move_backward(first, lastConstructed, realLast);
void populateMemForward(T* mem, std::size_t n, Function const& op) {
std::size_t idx = 0;
try {
- for (int i = 0; i < n; ++i) {
+ for (size_t i = 0; i < n; ++i) {
op(&mem[idx]);
++idx;
}
IntegralSizePolicy() : size_(0) {}
protected:
- std::size_t policyMaxSize() const {
+ static constexpr std::size_t policyMaxSize() {
return SizeType(~kExternMask);
}
SizeType size_;
};
-#ifdef __x86_64__
+#if FOLLY_X64
template<class SizeType, bool ShouldUseHeap>
struct OneBitMutexImpl {
typedef SizeType InternalSizeType;
protected:
static bool const kShouldUseHeap = ShouldUseHeap;
- std::size_t policyMaxSize() const {
+ static constexpr std::size_t policyMaxSize() {
return SizeType(~(SizeType(1) << kLockBit | kExternMask));
}
}
template <class T>
T* pointerFlagClear(T* p) {
- return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) & ~1);
+ return reinterpret_cast<T*>(
+ reinterpret_cast<uintptr_t>(p) & ~uintptr_t(1));
}
inline void* shiftPointer(void* p, size_t sizeBytes) {
return static_cast<char*>(p) + sizeBytes;
return std::lexicographical_compare(begin(), end(), o.begin(), o.end());
}
- size_type max_size() const {
+ static constexpr size_type max_size() {
return !BaseType::kShouldUseHeap ? MaxInline
- : this->policyMaxSize();
+ : BaseType::policyMaxSize();
}
size_type size() const { return this->doSize(); }
doConstruct(n, val);
}
- void makeSize(size_type size, value_type* v = NULL) {
+ void makeSize(size_type size, value_type* v = nullptr) {
makeSize(size, v, size - 1);
}
detail::shiftPointer(newh, kHeapifyCapacitySize) :
newh);
- if (v != NULL) {
+ if (v != nullptr) {
// move new element
try {
new (&newp[pos]) value_type(std::move(*v));
} catch (...) {
- std::free(newh);
+ free(newh);
throw;
}
detail::moveToUninitialized(begin(), begin() + pos, newp);
} catch (...) {
newp[pos].~value_type();
- std::free(newh);
+ free(newh);
throw;
}
for (size_type i = 0; i <= pos; ++i) {
newp[i].~value_type();
}
- std::free(newh);
+ free(newh);
throw;
}
} else {
try {
detail::moveToUninitialized(begin(), end(), newp);
} catch (...) {
- std::free(newh);
+ free(newh);
throw;
}
}
}
} FB_PACKED;
-#if defined(__x86_64_)
+#if FOLLY_X64
typedef unsigned char InlineStorageType[sizeof(value_type) * MaxInline];
#else
typedef typename std::aligned_storage<
void freeHeap() {
auto vp = detail::pointerFlagClear(pdata_.heap_);
- std::free(vp);
+ free(vp);
}
} FB_PACKED u;
} FB_PACKED;
}
+#pragma GCC diagnostic pop
+
#ifdef FB_PACKED
# undef FB_PACKED
#endif