/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* optimizations for use with relocatable types and jemalloc.
*/
-#ifndef FOLLY_FBVECTOR_H
-#define FOLLY_FBVECTOR_H
+#pragma once
//=============================================================================
// headers
#include <folly/Likely.h>
#include <folly/Malloc.h>
#include <folly/Traits.h>
+#include <folly/portability/BitsFunctexcept.h>
#include <boost/operators.hpp>
S_destroy_range_a(*this, b_, e_);
}
- D_deallocate(b_, z_ - b_);
+ D_deallocate(b_, size_type(z_ - b_));
}
}
}
}
- void
- set(pointer newB, size_type newSize, size_type newCap) {
+ void set(pointer newB, size_type newSize, size_type newCap) {
z_ = newB + newCap;
e_ = newB + newSize;
b_ = newB;
// optimized
static void S_uninitialized_fill_n(T* dest, size_type n) {
if (folly::IsZeroInitializable<T>::value) {
- std::memset(dest, 0, sizeof(T) * n);
+ if (LIKELY(n != 0)) {
+ std::memset(dest, 0, sizeof(T) * n);
+ }
} else {
auto b = dest;
auto e = dest + n;
static void
S_uninitialized_copy_bits(T* dest, const T* first, const T* last) {
- std::memcpy((void*)dest, (void*)first, (last - first) * sizeof(T));
+ if (last != first) {
+ std::memcpy((void*)dest, (void*)first, (last - first) * sizeof(T));
+ }
}
static void
std::move_iterator<T*> last) {
T* bFirst = first.base();
T* bLast = last.base();
- std::memcpy((void*)dest, (void*)bFirst, (bLast - bFirst) * sizeof(T));
+ if (bLast != bFirst) {
+ std::memcpy((void*)dest, (void*)bFirst, (bLast - bFirst) * sizeof(T));
+ }
}
template <typename It>
}
void relocate_move_or_memcpy(T* dest, T* first, T* last, std::true_type) {
- std::memcpy((void*)dest, (void*)first, (last - first) * sizeof(T));
+ if (first != nullptr) {
+ std::memcpy((void*)dest, (void*)first, (last - first) * sizeof(T));
+ }
}
void relocate_move_or_memcpy(T* dest, T* first, T* last, std::false_type) {
public:
size_type size() const noexcept {
- return impl_.e_ - impl_.b_;
+ return size_type(impl_.e_ - impl_.b_);
}
size_type max_size() const noexcept {
}
size_type capacity() const noexcept {
- return impl_.z_ - impl_.b_;
+ return size_type(impl_.z_ - impl_.b_);
}
bool empty() const noexcept {
throw;
}
if (impl_.b_)
- M_deallocate(impl_.b_, impl_.z_ - impl_.b_);
+ M_deallocate(impl_.b_, size_type(impl_.z_ - impl_.b_));
impl_.z_ = newB + newCap;
impl_.e_ = newB + (impl_.e_ - impl_.b_);
impl_.b_ = newB;
}
void shrink_to_fit() noexcept {
+ if (empty()) {
+ impl_.reset();
+ return;
+ }
+
auto const newCapacityBytes = folly::goodMallocSize(size() * sizeof(T));
auto const newCap = newCapacityBytes / sizeof(T);
auto const oldCap = capacity();
}
const_reference at(size_type n) const {
if (UNLIKELY(n >= size())) {
- throw std::out_of_range("fbvector: index is greater than size.");
+ std::__throw_out_of_range("fbvector: index is greater than size.");
}
return (*this)[n];
}
// fbvector grows differently on two counts:
//
// (1) initial size
- // Instead of grwoing to size 1 from empty, and fbvector allocates at
- // least 64 bytes. You may still use reserve to reserve a lesser amount
- // of memory.
+ // Instead of growing to size 1 from empty, fbvector allocates at least
+ // 64 bytes. You may still use reserve to reserve a lesser amount of
+ // memory.
// (2) 1.5x
// For medium-sized vectors, the growth strategy is 1.5x. See the docs
// for details.
// These three functions, make_window, wrap_frame, and
// insert_use_fresh_memory, can be combined into a uniform interface.
// Since that interface involves a lot of case-work, it is built into
- // some macros: FOLLY_FBVECTOR_INSERT_(START|TRY|END)
+ // some macros: FOLLY_FBVECTOR_INSERT_(PRE|START|TRY|END)
// Macros are used in an attempt to let GCC perform better optimizations,
// especially control flow optimization.
//
// window
void make_window(iterator position, size_type n) {
- assert(isValid(position));
- assert(size() + n <= capacity());
- assert(n != 0);
-
// The result is guaranteed to be non-negative, so use an unsigned type:
size_type tail = std::distance(position, impl_.e_);
//---------------------------------------------------------------------------
// use fresh?
- bool insert_use_fresh(const_iterator cposition, size_type n) {
- if (cposition == cend()) {
+ bool insert_use_fresh(bool at_end, size_type n) {
+ if (at_end) {
if (size() + n <= capacity()) return false;
if (reserve_in_place(size() + n)) return false;
return true;
//---------------------------------------------------------------------------
// interface
+ #define FOLLY_FBVECTOR_INSERT_PRE(cpos, n) \
+ if (n == 0) return (iterator)cpos; \
+ bool at_end = cpos == cend(); \
+ bool fresh = insert_use_fresh(at_end, n); \
+ if (!at_end) { \
+ if (!fresh) {
+
+ // check for internal data (technically not required by the standard)
+
#define FOLLY_FBVECTOR_INSERT_START(cpos, n) \
- assert(isValid(cpos)); \
+ } \
+ assert(isValid(cpos)); \
+ } \
T* position = const_cast<T*>(cpos); \
size_type idx = std::distance(impl_.b_, position); \
- bool fresh = insert_use_fresh(position, n); \
T* b; \
- size_type newCap = 0; \
+ size_type newCap; /* intentionally uninitialized */ \
\
if (fresh) { \
newCap = computeInsertCapacity(n); \
b = M_allocate(newCap); \
} else { \
- make_window(position, n); \
+ if (!at_end) { \
+ make_window(position, n); \
+ } else { \
+ impl_.e_ += n; \
+ } \
b = impl_.b_; \
} \
\
if (fresh) { \
M_deallocate(b, newCap); \
} else { \
- undo_window(position, n); \
+ if (!at_end) { \
+ undo_window(position, n); \
+ } else { \
+ impl_.e_ -= n; \
+ } \
} \
throw; \
} \
template <class... Args>
iterator emplace(const_iterator cpos, Args&&... args) {
+ FOLLY_FBVECTOR_INSERT_PRE(cpos, 1)
FOLLY_FBVECTOR_INSERT_START(cpos, 1)
M_construct(start, std::forward<Args>(args)...);
FOLLY_FBVECTOR_INSERT_TRY(cpos, 1)
}
iterator insert(const_iterator cpos, const T& value) {
- if (dataIsInternal(value)) return insert(cpos, T(value));
-
+ FOLLY_FBVECTOR_INSERT_PRE(cpos, 1)
+ if (dataIsInternal(value)) return insert(cpos, T(value));
FOLLY_FBVECTOR_INSERT_START(cpos, 1)
M_construct(start, value);
FOLLY_FBVECTOR_INSERT_TRY(cpos, 1)
}
iterator insert(const_iterator cpos, T&& value) {
- if (dataIsInternal(value)) return insert(cpos, T(std::move(value)));
-
+ FOLLY_FBVECTOR_INSERT_PRE(cpos, 1)
+ if (dataIsInternal(value)) return insert(cpos, T(std::move(value)));
FOLLY_FBVECTOR_INSERT_START(cpos, 1)
M_construct(start, std::move(value));
FOLLY_FBVECTOR_INSERT_TRY(cpos, 1)
}
iterator insert(const_iterator cpos, size_type n, VT value) {
- if (n == 0) return (iterator)cpos;
- if (dataIsInternalAndNotVT(value)) return insert(cpos, n, T(value));
-
+ FOLLY_FBVECTOR_INSERT_PRE(cpos, n)
+ if (dataIsInternalAndNotVT(value)) return insert(cpos, n, T(value));
FOLLY_FBVECTOR_INSERT_START(cpos, n)
D_uninitialized_fill_n_a(start, n, value);
FOLLY_FBVECTOR_INSERT_TRY(cpos, n)
iterator insert(const_iterator cpos, FIt first, FIt last,
std::forward_iterator_tag) {
size_type n = std::distance(first, last);
- if (n == 0) return (iterator)cpos;
-
+ FOLLY_FBVECTOR_INSERT_PRE(cpos, n)
FOLLY_FBVECTOR_INSERT_START(cpos, n)
D_uninitialized_copy_a(start, first, last);
FOLLY_FBVECTOR_INSERT_TRY(cpos, n)
}
} // namespace folly
-
-#endif // FOLLY_FBVECTOR_H