small_vector.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * For high-level documentation and usage examples see
  18. * folly/docs/small_vector.md
  19. *
  20. * @author Jordan DeLong <delong.j@fb.com>
  21. */
  22. #pragma once
  23. #include <algorithm>
  24. #include <cassert>
  25. #include <cstdlib>
  26. #include <cstring>
  27. #include <iterator>
  28. #include <stdexcept>
  29. #include <type_traits>
  30. #include <utility>
  31. #include <boost/mpl/count.hpp>
  32. #include <boost/mpl/empty.hpp>
  33. #include <boost/mpl/eval_if.hpp>
  34. #include <boost/mpl/filter_view.hpp>
  35. #include <boost/mpl/front.hpp>
  36. #include <boost/mpl/identity.hpp>
  37. #include <boost/mpl/if.hpp>
  38. #include <boost/mpl/placeholders.hpp>
  39. #include <boost/mpl/size.hpp>
  40. #include <boost/mpl/vector.hpp>
  41. #include <boost/operators.hpp>
  42. #include <folly/ConstexprMath.h>
  43. #include <folly/FormatTraits.h>
  44. #include <folly/Likely.h>
  45. #include <folly/Portability.h>
  46. #include <folly/Traits.h>
  47. #include <folly/lang/Assume.h>
  48. #include <folly/lang/Exception.h>
  49. #include <folly/memory/Malloc.h>
  50. #include <folly/portability/Malloc.h>
  51. #if (FOLLY_X64 || FOLLY_PPC64)
  52. #define FOLLY_SV_PACK_ATTR FOLLY_PACK_ATTR
  53. #define FOLLY_SV_PACK_PUSH FOLLY_PACK_PUSH
  54. #define FOLLY_SV_PACK_POP FOLLY_PACK_POP
  55. #else
  56. #define FOLLY_SV_PACK_ATTR
  57. #define FOLLY_SV_PACK_PUSH
  58. #define FOLLY_SV_PACK_POP
  59. #endif
  60. // Ignore shadowing warnings within this file, so includers can use -Wshadow.
  61. FOLLY_PUSH_WARNING
  62. FOLLY_GNU_DISABLE_WARNING("-Wshadow")
  63. namespace folly {
  64. //////////////////////////////////////////////////////////////////////
  65. namespace small_vector_policy {
  66. //////////////////////////////////////////////////////////////////////
  67. /*
  68. * A flag which makes us refuse to use the heap at all. If we
  69. * overflow the in situ capacity we throw an exception.
  70. */
  71. struct NoHeap;
  72. //////////////////////////////////////////////////////////////////////
  73. } // namespace small_vector_policy
  74. //////////////////////////////////////////////////////////////////////
  75. template <class T, std::size_t M, class A, class B, class C>
  76. class small_vector;
  77. //////////////////////////////////////////////////////////////////////
  78. namespace detail {
  79. /*
  80. * Move objects in memory to the right into some uninitialized
  81. * memory, where the region overlaps. This doesn't just use
  82. * std::move_backward because move_backward only works if all the
  83. * memory is initialized to type T already.
  84. */
  85. template <class T>
  86. typename std::enable_if<
  87. std::is_default_constructible<T>::value &&
  88. !folly::is_trivially_copyable<T>::value>::type
  89. moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
  90. if (lastConstructed == realLast) {
  91. return;
  92. }
  93. T* end = first - 1; // Past the end going backwards.
  94. T* out = realLast - 1;
  95. T* in = lastConstructed - 1;
  96. try {
  97. for (; in != end && out >= lastConstructed; --in, --out) {
  98. new (out) T(std::move(*in));
  99. }
  100. for (; in != end; --in, --out) {
  101. *out = std::move(*in);
  102. }
  103. for (; out >= lastConstructed; --out) {
  104. new (out) T();
  105. }
  106. } catch (...) {
  107. // We want to make sure the same stuff is uninitialized memory
  108. // if we exit via an exception (this is to make sure we provide
  109. // the basic exception safety guarantee for insert functions).
  110. if (out < lastConstructed) {
  111. out = lastConstructed - 1;
  112. }
  113. for (auto it = out + 1; it != realLast; ++it) {
  114. it->~T();
  115. }
  116. throw;
  117. }
  118. }
  119. // Specialization for trivially copyable types. The call to
  120. // std::move_backward here will just turn into a memmove. (TODO:
  121. // change to std::is_trivially_copyable when that works.)
  122. template <class T>
  123. typename std::enable_if<
  124. !std::is_default_constructible<T>::value ||
  125. folly::is_trivially_copyable<T>::value>::type
  126. moveObjectsRight(T* first, T* lastConstructed, T* realLast) {
  127. std::move_backward(first, lastConstructed, realLast);
  128. }
  129. /*
  130. * Populate a region of memory using `op' to construct elements. If
  131. * anything throws, undo what we did.
  132. */
  133. template <class T, class Function>
  134. void populateMemForward(T* mem, std::size_t n, Function const& op) {
  135. std::size_t idx = 0;
  136. try {
  137. for (size_t i = 0; i < n; ++i) {
  138. op(&mem[idx]);
  139. ++idx;
  140. }
  141. } catch (...) {
  142. for (std::size_t i = 0; i < idx; ++i) {
  143. mem[i].~T();
  144. }
  145. throw;
  146. }
  147. }
  148. template <class SizeType, bool ShouldUseHeap>
  149. struct IntegralSizePolicyBase {
  150. typedef SizeType InternalSizeType;
  151. IntegralSizePolicyBase() : size_(0) {}
  152. protected:
  153. static constexpr std::size_t policyMaxSize() {
  154. return SizeType(~kExternMask);
  155. }
  156. std::size_t doSize() const {
  157. return size_ & ~kExternMask;
  158. }
  159. std::size_t isExtern() const {
  160. return kExternMask & size_;
  161. }
  162. void setExtern(bool b) {
  163. if (b) {
  164. size_ |= kExternMask;
  165. } else {
  166. size_ &= ~kExternMask;
  167. }
  168. }
  169. void setSize(std::size_t sz) {
  170. assert(sz <= policyMaxSize());
  171. size_ = (kExternMask & size_) | SizeType(sz);
  172. }
  173. void swapSizePolicy(IntegralSizePolicyBase& o) {
  174. std::swap(size_, o.size_);
  175. }
  176. protected:
  177. static bool constexpr kShouldUseHeap = ShouldUseHeap;
  178. private:
  179. static SizeType constexpr kExternMask =
  180. kShouldUseHeap ? SizeType(1) << (sizeof(SizeType) * 8 - 1) : 0;
  181. SizeType size_;
  182. };
  183. template <class SizeType, bool ShouldUseHeap>
  184. struct IntegralSizePolicy;
  185. template <class SizeType>
  186. struct IntegralSizePolicy<SizeType, true>
  187. : public IntegralSizePolicyBase<SizeType, true> {
  188. public:
  189. /*
  190. * Move a range to a range of uninitialized memory. Assumes the
  191. * ranges don't overlap.
  192. */
  193. template <class T>
  194. typename std::enable_if<!folly::is_trivially_copyable<T>::value>::type
  195. moveToUninitialized(T* first, T* last, T* out) {
  196. std::size_t idx = 0;
  197. try {
  198. for (; first != last; ++first, ++idx) {
  199. new (&out[idx]) T(std::move(*first));
  200. }
  201. } catch (...) {
  202. // Even for callers trying to give the strong guarantee
  203. // (e.g. push_back) it's ok to assume here that we don't have to
  204. // move things back and that it was a copy constructor that
  205. // threw: if someone throws from a move constructor the effects
  206. // are unspecified.
  207. for (std::size_t i = 0; i < idx; ++i) {
  208. out[i].~T();
  209. }
  210. throw;
  211. }
  212. }
  213. // Specialization for trivially copyable types.
  214. template <class T>
  215. typename std::enable_if<folly::is_trivially_copyable<T>::value>::type
  216. moveToUninitialized(T* first, T* last, T* out) {
  217. std::memmove(out, first, (last - first) * sizeof *first);
  218. }
  219. /*
  220. * Move a range to a range of uninitialized memory. Assumes the
  221. * ranges don't overlap. Inserts an element at out + pos using
  222. * emplaceFunc(). out will contain (end - begin) + 1 elements on success and
  223. * none on failure. If emplaceFunc() throws [begin, end) is unmodified.
  224. */
  225. template <class T, class EmplaceFunc>
  226. void moveToUninitializedEmplace(
  227. T* begin,
  228. T* end,
  229. T* out,
  230. SizeType pos,
  231. EmplaceFunc&& emplaceFunc) {
  232. // Must be called first so that if it throws [begin, end) is unmodified.
  233. // We have to support the strong exception guarantee for emplace_back().
  234. emplaceFunc(out + pos);
  235. // move old elements to the left of the new one
  236. try {
  237. this->moveToUninitialized(begin, begin + pos, out);
  238. } catch (...) {
  239. out[pos].~T();
  240. throw;
  241. }
  242. // move old elements to the right of the new one
  243. try {
  244. if (begin + pos < end) {
  245. this->moveToUninitialized(begin + pos, end, out + pos + 1);
  246. }
  247. } catch (...) {
  248. for (SizeType i = 0; i <= pos; ++i) {
  249. out[i].~T();
  250. }
  251. throw;
  252. }
  253. }
  254. };
  255. template <class SizeType>
  256. struct IntegralSizePolicy<SizeType, false>
  257. : public IntegralSizePolicyBase<SizeType, false> {
  258. public:
  259. template <class T>
  260. void moveToUninitialized(T* /*first*/, T* /*last*/, T* /*out*/) {
  261. assume_unreachable();
  262. }
  263. template <class T, class EmplaceFunc>
  264. void moveToUninitializedEmplace(
  265. T* /* begin */,
  266. T* /* end */,
  267. T* /* out */,
  268. SizeType /* pos */,
  269. EmplaceFunc&& /* emplaceFunc */) {
  270. assume_unreachable();
  271. }
  272. };
  273. /*
  274. * If you're just trying to use this class, ignore everything about
  275. * this next small_vector_base class thing.
  276. *
  277. * The purpose of this junk is to minimize sizeof(small_vector<>)
  278. * and allow specifying the template parameters in whatever order is
  279. * convenient for the user. There's a few extra steps here to try
  280. * to keep the error messages at least semi-reasonable.
  281. *
  282. * Apologies for all the black magic.
  283. */
  284. namespace mpl = boost::mpl;
  285. template <
  286. class Value,
  287. std::size_t RequestedMaxInline,
  288. class InPolicyA,
  289. class InPolicyB,
  290. class InPolicyC>
  291. struct small_vector_base {
  292. typedef mpl::vector<InPolicyA, InPolicyB, InPolicyC> PolicyList;
  293. /*
  294. * Determine the size type
  295. */
  296. typedef typename mpl::filter_view<
  297. PolicyList,
  298. std::is_integral<mpl::placeholders::_1>>::type Integrals;
  299. typedef typename mpl::eval_if<
  300. mpl::empty<Integrals>,
  301. mpl::identity<std::size_t>,
  302. mpl::front<Integrals>>::type SizeType;
  303. static_assert(
  304. std::is_unsigned<SizeType>::value,
  305. "Size type should be an unsigned integral type");
  306. static_assert(
  307. mpl::size<Integrals>::value == 0 || mpl::size<Integrals>::value == 1,
  308. "Multiple size types specified in small_vector<>");
  309. /*
  310. * Determine whether we should allow spilling to the heap or not.
  311. */
  312. typedef typename mpl::count<PolicyList, small_vector_policy::NoHeap>::type
  313. HasNoHeap;
  314. static_assert(
  315. HasNoHeap::value == 0 || HasNoHeap::value == 1,
  316. "Multiple copies of small_vector_policy::NoHeap "
  317. "supplied; this is probably a mistake");
  318. /*
  319. * Make the real policy base classes.
  320. */
  321. typedef IntegralSizePolicy<SizeType, !HasNoHeap::value> ActualSizePolicy;
  322. /*
  323. * Now inherit from them all. This is done in such a convoluted
  324. * way to make sure we get the empty base optimizaton on all these
  325. * types to keep sizeof(small_vector<>) minimal.
  326. */
  327. typedef boost::totally_ordered1<
  328. small_vector<Value, RequestedMaxInline, InPolicyA, InPolicyB, InPolicyC>,
  329. ActualSizePolicy>
  330. type;
  331. };
  332. template <class T>
  333. T* pointerFlagSet(T* p) {
  334. return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) | 1);
  335. }
  336. template <class T>
  337. bool pointerFlagGet(T* p) {
  338. return reinterpret_cast<uintptr_t>(p) & 1;
  339. }
  340. template <class T>
  341. T* pointerFlagClear(T* p) {
  342. return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p) & ~uintptr_t(1));
  343. }
  344. inline void* shiftPointer(void* p, size_t sizeBytes) {
  345. return static_cast<char*>(p) + sizeBytes;
  346. }
  347. } // namespace detail
  348. //////////////////////////////////////////////////////////////////////
  349. FOLLY_SV_PACK_PUSH
  350. template <
  351. class Value,
  352. std::size_t RequestedMaxInline = 1,
  353. class PolicyA = void,
  354. class PolicyB = void,
  355. class PolicyC = void>
  356. class small_vector : public detail::small_vector_base<
  357. Value,
  358. RequestedMaxInline,
  359. PolicyA,
  360. PolicyB,
  361. PolicyC>::type {
  362. typedef typename detail::
  363. small_vector_base<Value, RequestedMaxInline, PolicyA, PolicyB, PolicyC>::
  364. type BaseType;
  365. typedef typename BaseType::InternalSizeType InternalSizeType;
  366. /*
  367. * Figure out the max number of elements we should inline. (If
  368. * the user asks for less inlined elements than we can fit unioned
  369. * into our value_type*, we will inline more than they asked.)
  370. */
  371. static constexpr std::size_t MaxInline{
  372. constexpr_max(sizeof(Value*) / sizeof(Value), RequestedMaxInline)};
  373. public:
  374. typedef std::size_t size_type;
  375. typedef Value value_type;
  376. typedef value_type& reference;
  377. typedef value_type const& const_reference;
  378. typedef value_type* iterator;
  379. typedef value_type* pointer;
  380. typedef value_type const* const_iterator;
  381. typedef std::ptrdiff_t difference_type;
  382. typedef std::reverse_iterator<iterator> reverse_iterator;
  383. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  384. small_vector() = default;
  385. // Allocator is unused here. It is taken in for compatibility with std::vector
  386. // interface, but it will be ignored.
  387. small_vector(const std::allocator<Value>&) {}
  388. small_vector(small_vector const& o) {
  389. auto n = o.size();
  390. makeSize(n);
  391. try {
  392. std::uninitialized_copy(o.begin(), o.end(), begin());
  393. } catch (...) {
  394. if (this->isExtern()) {
  395. u.freeHeap();
  396. }
  397. throw;
  398. }
  399. this->setSize(n);
  400. }
  401. small_vector(small_vector&& o) noexcept(
  402. std::is_nothrow_move_constructible<Value>::value) {
  403. if (o.isExtern()) {
  404. swap(o);
  405. } else {
  406. std::uninitialized_copy(
  407. std::make_move_iterator(o.begin()),
  408. std::make_move_iterator(o.end()),
  409. begin());
  410. this->setSize(o.size());
  411. }
  412. }
  413. small_vector(std::initializer_list<value_type> il) {
  414. constructImpl(il.begin(), il.end(), std::false_type());
  415. }
  416. explicit small_vector(size_type n) {
  417. doConstruct(n, [&](void* p) { new (p) value_type(); });
  418. }
  419. small_vector(size_type n, value_type const& t) {
  420. doConstruct(n, [&](void* p) { new (p) value_type(t); });
  421. }
  422. template <class Arg>
  423. explicit small_vector(Arg arg1, Arg arg2) {
  424. // Forward using std::is_arithmetic to get to the proper
  425. // implementation; this disambiguates between the iterators and
  426. // (size_t, value_type) meaning for this constructor.
  427. constructImpl(arg1, arg2, std::is_arithmetic<Arg>());
  428. }
  429. ~small_vector() {
  430. for (auto& t : *this) {
  431. (&t)->~value_type();
  432. }
  433. if (this->isExtern()) {
  434. u.freeHeap();
  435. }
  436. }
  437. small_vector& operator=(small_vector const& o) {
  438. if (FOLLY_LIKELY(this != &o)) {
  439. assign(o.begin(), o.end());
  440. }
  441. return *this;
  442. }
  443. small_vector& operator=(small_vector&& o) {
  444. // TODO: optimization:
  445. // if both are internal, use move assignment where possible
  446. if (FOLLY_LIKELY(this != &o)) {
  447. clear();
  448. swap(o);
  449. }
  450. return *this;
  451. }
  452. bool operator==(small_vector const& o) const {
  453. return size() == o.size() && std::equal(begin(), end(), o.begin());
  454. }
  455. bool operator<(small_vector const& o) const {
  456. return std::lexicographical_compare(begin(), end(), o.begin(), o.end());
  457. }
  458. static constexpr size_type max_size() {
  459. return !BaseType::kShouldUseHeap ? static_cast<size_type>(MaxInline)
  460. : BaseType::policyMaxSize();
  461. }
  462. size_type size() const {
  463. return this->doSize();
  464. }
  465. bool empty() const {
  466. return !size();
  467. }
  468. iterator begin() {
  469. return data();
  470. }
  471. iterator end() {
  472. return data() + size();
  473. }
  474. const_iterator begin() const {
  475. return data();
  476. }
  477. const_iterator end() const {
  478. return data() + size();
  479. }
  480. const_iterator cbegin() const {
  481. return begin();
  482. }
  483. const_iterator cend() const {
  484. return end();
  485. }
  486. reverse_iterator rbegin() {
  487. return reverse_iterator(end());
  488. }
  489. reverse_iterator rend() {
  490. return reverse_iterator(begin());
  491. }
  492. const_reverse_iterator rbegin() const {
  493. return const_reverse_iterator(end());
  494. }
  495. const_reverse_iterator rend() const {
  496. return const_reverse_iterator(begin());
  497. }
  498. const_reverse_iterator crbegin() const {
  499. return rbegin();
  500. }
  501. const_reverse_iterator crend() const {
  502. return rend();
  503. }
  504. /*
  505. * Usually one of the simplest functions in a Container-like class
  506. * but a bit more complex here. We have to handle all combinations
  507. * of in-place vs. heap between this and o.
  508. *
  509. * Basic guarantee only. Provides the nothrow guarantee iff our
  510. * value_type has a nothrow move or copy constructor.
  511. */
  512. void swap(small_vector& o) {
  513. using std::swap; // Allow ADL on swap for our value_type.
  514. if (this->isExtern() && o.isExtern()) {
  515. this->swapSizePolicy(o);
  516. auto thisCapacity = this->capacity();
  517. auto oCapacity = o.capacity();
  518. auto* tmp = u.pdata_.heap_;
  519. u.pdata_.heap_ = o.u.pdata_.heap_;
  520. o.u.pdata_.heap_ = tmp;
  521. this->setCapacity(oCapacity);
  522. o.setCapacity(thisCapacity);
  523. return;
  524. }
  525. if (!this->isExtern() && !o.isExtern()) {
  526. auto& oldSmall = size() < o.size() ? *this : o;
  527. auto& oldLarge = size() < o.size() ? o : *this;
  528. for (size_type i = 0; i < oldSmall.size(); ++i) {
  529. swap(oldSmall[i], oldLarge[i]);
  530. }
  531. size_type i = oldSmall.size();
  532. const size_type ci = i;
  533. try {
  534. for (; i < oldLarge.size(); ++i) {
  535. auto addr = oldSmall.begin() + i;
  536. new (addr) value_type(std::move(oldLarge[i]));
  537. oldLarge[i].~value_type();
  538. }
  539. } catch (...) {
  540. oldSmall.setSize(i);
  541. for (; i < oldLarge.size(); ++i) {
  542. oldLarge[i].~value_type();
  543. }
  544. oldLarge.setSize(ci);
  545. throw;
  546. }
  547. oldSmall.setSize(i);
  548. oldLarge.setSize(ci);
  549. return;
  550. }
  551. // isExtern != o.isExtern()
  552. auto& oldExtern = o.isExtern() ? o : *this;
  553. auto& oldIntern = o.isExtern() ? *this : o;
  554. auto oldExternCapacity = oldExtern.capacity();
  555. auto oldExternHeap = oldExtern.u.pdata_.heap_;
  556. auto buff = oldExtern.u.buffer();
  557. size_type i = 0;
  558. try {
  559. for (; i < oldIntern.size(); ++i) {
  560. new (&buff[i]) value_type(std::move(oldIntern[i]));
  561. oldIntern[i].~value_type();
  562. }
  563. } catch (...) {
  564. for (size_type kill = 0; kill < i; ++kill) {
  565. buff[kill].~value_type();
  566. }
  567. for (; i < oldIntern.size(); ++i) {
  568. oldIntern[i].~value_type();
  569. }
  570. oldIntern.setSize(0);
  571. oldExtern.u.pdata_.heap_ = oldExternHeap;
  572. oldExtern.setCapacity(oldExternCapacity);
  573. throw;
  574. }
  575. oldIntern.u.pdata_.heap_ = oldExternHeap;
  576. this->swapSizePolicy(o);
  577. oldIntern.setCapacity(oldExternCapacity);
  578. }
  579. void resize(size_type sz) {
  580. if (sz < size()) {
  581. erase(begin() + sz, end());
  582. return;
  583. }
  584. makeSize(sz);
  585. detail::populateMemForward(
  586. begin() + size(), sz - size(), [&](void* p) { new (p) value_type(); });
  587. this->setSize(sz);
  588. }
  589. void resize(size_type sz, value_type const& v) {
  590. if (sz < size()) {
  591. erase(begin() + sz, end());
  592. return;
  593. }
  594. makeSize(sz);
  595. detail::populateMemForward(
  596. begin() + size(), sz - size(), [&](void* p) { new (p) value_type(v); });
  597. this->setSize(sz);
  598. }
  599. value_type* data() noexcept {
  600. return this->isExtern() ? u.heap() : u.buffer();
  601. }
  602. value_type const* data() const noexcept {
  603. return this->isExtern() ? u.heap() : u.buffer();
  604. }
  605. template <class... Args>
  606. iterator emplace(const_iterator p, Args&&... args) {
  607. if (p == cend()) {
  608. emplace_back(std::forward<Args>(args)...);
  609. return end() - 1;
  610. }
  611. /*
  612. * We implement emplace at places other than at the back with a
  613. * temporary for exception safety reasons. It is possible to
  614. * avoid having to do this, but it becomes hard to maintain the
  615. * basic exception safety guarantee (unless you respond to a copy
  616. * constructor throwing by clearing the whole vector).
  617. *
  618. * The reason for this is that otherwise you have to destruct an
  619. * element before constructing this one in its place---if the
  620. * constructor throws, you either need a nothrow default
  621. * constructor or a nothrow copy/move to get something back in the
  622. * "gap", and the vector requirements don't guarantee we have any
  623. * of these. Clearing the whole vector is a legal response in
  624. * this situation, but it seems like this implementation is easy
  625. * enough and probably better.
  626. */
  627. return insert(p, value_type(std::forward<Args>(args)...));
  628. }
  629. void reserve(size_type sz) {
  630. makeSize(sz);
  631. }
  632. size_type capacity() const {
  633. if (this->isExtern()) {
  634. if (u.hasCapacity()) {
  635. return u.getCapacity();
  636. }
  637. return malloc_usable_size(u.pdata_.heap_) / sizeof(value_type);
  638. }
  639. return MaxInline;
  640. }
  641. void shrink_to_fit() {
  642. if (!this->isExtern()) {
  643. return;
  644. }
  645. small_vector tmp(begin(), end());
  646. tmp.swap(*this);
  647. }
  648. template <class... Args>
  649. void emplace_back(Args&&... args) {
  650. if (capacity() == size()) {
  651. // Any of args may be references into the vector.
  652. // When we are reallocating, we have to be careful to construct the new
  653. // element before modifying the data in the old buffer.
  654. makeSize(
  655. size() + 1,
  656. [&](void* p) { new (p) value_type(std::forward<Args>(args)...); },
  657. size());
  658. } else {
  659. new (end()) value_type(std::forward<Args>(args)...);
  660. }
  661. this->setSize(size() + 1);
  662. }
  663. void push_back(value_type&& t) {
  664. return emplace_back(std::move(t));
  665. }
  666. void push_back(value_type const& t) {
  667. emplace_back(t);
  668. }
  669. void pop_back() {
  670. erase(end() - 1);
  671. }
  672. iterator insert(const_iterator constp, value_type&& t) {
  673. iterator p = unconst(constp);
  674. if (p == end()) {
  675. push_back(std::move(t));
  676. return end() - 1;
  677. }
  678. auto offset = p - begin();
  679. if (capacity() == size()) {
  680. makeSize(
  681. size() + 1,
  682. [&t](void* ptr) { new (ptr) value_type(std::move(t)); },
  683. offset);
  684. this->setSize(this->size() + 1);
  685. } else {
  686. detail::moveObjectsRight(
  687. data() + offset, data() + size(), data() + size() + 1);
  688. this->setSize(size() + 1);
  689. data()[offset] = std::move(t);
  690. }
  691. return begin() + offset;
  692. }
  693. iterator insert(const_iterator p, value_type const& t) {
  694. // Make a copy and forward to the rvalue value_type&& overload
  695. // above.
  696. return insert(p, value_type(t));
  697. }
  698. iterator insert(const_iterator pos, size_type n, value_type const& val) {
  699. auto offset = pos - begin();
  700. makeSize(size() + n);
  701. detail::moveObjectsRight(
  702. data() + offset, data() + size(), data() + size() + n);
  703. this->setSize(size() + n);
  704. std::generate_n(begin() + offset, n, [&] { return val; });
  705. return begin() + offset;
  706. }
  707. template <class Arg>
  708. iterator insert(const_iterator p, Arg arg1, Arg arg2) {
  709. // Forward using std::is_arithmetic to get to the proper
  710. // implementation; this disambiguates between the iterators and
  711. // (size_t, value_type) meaning for this function.
  712. return insertImpl(unconst(p), arg1, arg2, std::is_arithmetic<Arg>());
  713. }
  714. iterator insert(const_iterator p, std::initializer_list<value_type> il) {
  715. return insert(p, il.begin(), il.end());
  716. }
  717. iterator erase(const_iterator q) {
  718. std::move(unconst(q) + 1, end(), unconst(q));
  719. (data() + size() - 1)->~value_type();
  720. this->setSize(size() - 1);
  721. return unconst(q);
  722. }
  723. iterator erase(const_iterator q1, const_iterator q2) {
  724. if (q1 == q2) {
  725. return unconst(q1);
  726. }
  727. std::move(unconst(q2), end(), unconst(q1));
  728. for (auto it = (end() - std::distance(q1, q2)); it != end(); ++it) {
  729. it->~value_type();
  730. }
  731. this->setSize(size() - (q2 - q1));
  732. return unconst(q1);
  733. }
  734. void clear() {
  735. erase(begin(), end());
  736. }
  737. template <class Arg>
  738. void assign(Arg first, Arg last) {
  739. clear();
  740. insert(end(), first, last);
  741. }
  742. void assign(std::initializer_list<value_type> il) {
  743. assign(il.begin(), il.end());
  744. }
  745. void assign(size_type n, const value_type& t) {
  746. clear();
  747. insert(end(), n, t);
  748. }
  749. reference front() {
  750. assert(!empty());
  751. return *begin();
  752. }
  753. reference back() {
  754. assert(!empty());
  755. return *(end() - 1);
  756. }
  757. const_reference front() const {
  758. assert(!empty());
  759. return *begin();
  760. }
  761. const_reference back() const {
  762. assert(!empty());
  763. return *(end() - 1);
  764. }
  765. reference operator[](size_type i) {
  766. assert(i < size());
  767. return *(begin() + i);
  768. }
  769. const_reference operator[](size_type i) const {
  770. assert(i < size());
  771. return *(begin() + i);
  772. }
  773. reference at(size_type i) {
  774. if (i >= size()) {
  775. throw_exception<std::out_of_range>("index out of range");
  776. }
  777. return (*this)[i];
  778. }
  779. const_reference at(size_type i) const {
  780. if (i >= size()) {
  781. throw_exception<std::out_of_range>("index out of range");
  782. }
  783. return (*this)[i];
  784. }
  785. private:
  786. static iterator unconst(const_iterator it) {
  787. return const_cast<iterator>(it);
  788. }
  789. // The std::false_type argument is part of disambiguating the
  790. // iterator insert functions from integral types (see insert().)
  791. template <class It>
  792. iterator insertImpl(iterator pos, It first, It last, std::false_type) {
  793. typedef typename std::iterator_traits<It>::iterator_category categ;
  794. if (std::is_same<categ, std::input_iterator_tag>::value) {
  795. auto offset = pos - begin();
  796. while (first != last) {
  797. pos = insert(pos, *first++);
  798. ++pos;
  799. }
  800. return begin() + offset;
  801. }
  802. auto distance = std::distance(first, last);
  803. auto offset = pos - begin();
  804. makeSize(size() + distance);
  805. detail::moveObjectsRight(
  806. data() + offset, data() + size(), data() + size() + distance);
  807. this->setSize(size() + distance);
  808. std::copy_n(first, distance, begin() + offset);
  809. return begin() + offset;
  810. }
  811. iterator
  812. insertImpl(iterator pos, size_type n, const value_type& val, std::true_type) {
  813. // The true_type means this should call the size_t,value_type
  814. // overload. (See insert().)
  815. return insert(pos, n, val);
  816. }
  817. // The std::false_type argument came from std::is_arithmetic as part
  818. // of disambiguating an overload (see the comment in the
  819. // constructor).
  820. template <class It>
  821. void constructImpl(It first, It last, std::false_type) {
  822. typedef typename std::iterator_traits<It>::iterator_category categ;
  823. if (std::is_same<categ, std::input_iterator_tag>::value) {
  824. // With iterators that only allow a single pass, we can't really
  825. // do anything sane here.
  826. while (first != last) {
  827. emplace_back(*first++);
  828. }
  829. return;
  830. }
  831. auto distance = std::distance(first, last);
  832. makeSize(distance);
  833. this->setSize(distance);
  834. try {
  835. detail::populateMemForward(
  836. data(), distance, [&](void* p) { new (p) value_type(*first++); });
  837. } catch (...) {
  838. if (this->isExtern()) {
  839. u.freeHeap();
  840. }
  841. throw;
  842. }
  843. }
  844. template <typename InitFunc>
  845. void doConstruct(size_type n, InitFunc&& func) {
  846. makeSize(n);
  847. this->setSize(n);
  848. try {
  849. detail::populateMemForward(data(), n, std::forward<InitFunc>(func));
  850. } catch (...) {
  851. if (this->isExtern()) {
  852. u.freeHeap();
  853. }
  854. throw;
  855. }
  856. }
  857. // The true_type means we should forward to the size_t,value_type
  858. // overload.
  859. void constructImpl(size_type n, value_type const& val, std::true_type) {
  860. doConstruct(n, [&](void* p) { new (p) value_type(val); });
  861. }
  862. /*
  863. * Compute the size after growth.
  864. */
  865. size_type computeNewSize() const {
  866. return std::min((3 * capacity()) / 2 + 1, max_size());
  867. }
  868. void makeSize(size_type newSize) {
  869. makeSizeInternal(newSize, false, [](void*) { assume_unreachable(); }, 0);
  870. }
  871. template <typename EmplaceFunc>
  872. void makeSize(size_type newSize, EmplaceFunc&& emplaceFunc, size_type pos) {
  873. assert(size() == capacity());
  874. makeSizeInternal(
  875. newSize, true, std::forward<EmplaceFunc>(emplaceFunc), pos);
  876. }
  877. /*
  878. * Ensure we have a large enough memory region to be size `newSize'.
  879. * Will move/copy elements if we are spilling to heap_ or needed to
  880. * allocate a new region, but if resized in place doesn't initialize
  881. * anything in the new region. In any case doesn't change size().
  882. * Supports insertion of new element during reallocation by given
  883. * pointer to new element and position of new element.
  884. * NOTE: If reallocation is not needed, insert must be false,
  885. * because we only know how to emplace elements into new memory.
  886. */
  887. template <typename EmplaceFunc>
  888. void makeSizeInternal(
  889. size_type newSize,
  890. bool insert,
  891. EmplaceFunc&& emplaceFunc,
  892. size_type pos) {
  893. if (newSize > max_size()) {
  894. throw std::length_error("max_size exceeded in small_vector");
  895. }
  896. if (newSize <= capacity()) {
  897. assert(!insert);
  898. return;
  899. }
  900. assert(this->kShouldUseHeap);
  901. // This branch isn't needed for correctness, but allows the optimizer to
  902. // skip generating code for the rest of this function in NoHeap
  903. // small_vectors.
  904. if (!this->kShouldUseHeap) {
  905. return;
  906. }
  907. newSize = std::max(newSize, computeNewSize());
  908. auto needBytes = newSize * sizeof(value_type);
  909. // If the capacity isn't explicitly stored inline, but the heap
  910. // allocation is grown to over some threshold, we should store
  911. // a capacity at the front of the heap allocation.
  912. bool heapifyCapacity =
  913. !kHasInlineCapacity && needBytes > kHeapifyCapacityThreshold;
  914. if (heapifyCapacity) {
  915. needBytes += kHeapifyCapacitySize;
  916. }
  917. auto const sizeBytes = goodMallocSize(needBytes);
  918. void* newh = checkedMalloc(sizeBytes);
  919. // We expect newh to be at least 2-aligned, because we want to
  920. // use its least significant bit as a flag.
  921. assert(!detail::pointerFlagGet(newh));
  922. value_type* newp = static_cast<value_type*>(
  923. heapifyCapacity ? detail::shiftPointer(newh, kHeapifyCapacitySize)
  924. : newh);
  925. try {
  926. if (insert) {
  927. // move and insert the new element
  928. this->moveToUninitializedEmplace(
  929. begin(), end(), newp, pos, std::forward<EmplaceFunc>(emplaceFunc));
  930. } else {
  931. // move without inserting new element
  932. this->moveToUninitialized(begin(), end(), newp);
  933. }
  934. } catch (...) {
  935. free(newh);
  936. throw;
  937. }
  938. for (auto& val : *this) {
  939. val.~value_type();
  940. }
  941. if (this->isExtern()) {
  942. u.freeHeap();
  943. }
  944. auto availableSizeBytes = sizeBytes;
  945. if (heapifyCapacity) {
  946. u.pdata_.heap_ = detail::pointerFlagSet(newh);
  947. availableSizeBytes -= kHeapifyCapacitySize;
  948. } else {
  949. u.pdata_.heap_ = newh;
  950. }
  951. this->setExtern(true);
  952. this->setCapacity(availableSizeBytes / sizeof(value_type));
  953. }
  954. /*
  955. * This will set the capacity field, stored inline in the storage_ field
  956. * if there is sufficient room to store it.
  957. */
  958. void setCapacity(size_type newCapacity) {
  959. assert(this->isExtern());
  960. if (u.hasCapacity()) {
  961. assert(newCapacity < std::numeric_limits<InternalSizeType>::max());
  962. u.setCapacity(newCapacity);
  963. }
  964. }
  965. private:
  966. struct HeapPtrWithCapacity {
  967. void* heap_;
  968. InternalSizeType capacity_;
  969. InternalSizeType getCapacity() const {
  970. return capacity_;
  971. }
  972. void setCapacity(InternalSizeType c) {
  973. capacity_ = c;
  974. }
  975. } FOLLY_SV_PACK_ATTR;
  976. struct HeapPtr {
  977. // Lower order bit of heap_ is used as flag to indicate whether capacity is
  978. // stored at the front of the heap allocation.
  979. void* heap_;
  980. InternalSizeType getCapacity() const {
  981. assert(detail::pointerFlagGet(heap_));
  982. return *static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_));
  983. }
  984. void setCapacity(InternalSizeType c) {
  985. *static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_)) = c;
  986. }
  987. } FOLLY_SV_PACK_ATTR;
  988. typedef typename std::aligned_storage<
  989. sizeof(value_type) * MaxInline,
  990. alignof(value_type)>::type InlineStorageDataType;
  991. typedef typename std::conditional<
  992. sizeof(value_type) * MaxInline != 0,
  993. InlineStorageDataType,
  994. void*>::type InlineStorageType;
  995. static bool constexpr kHasInlineCapacity =
  996. sizeof(HeapPtrWithCapacity) < sizeof(InlineStorageType);
  997. // This value should we multiple of word size.
  998. static size_t constexpr kHeapifyCapacitySize = sizeof(
  999. typename std::
  1000. aligned_storage<sizeof(InternalSizeType), alignof(value_type)>::type);
  1001. // Threshold to control capacity heapifying.
  1002. static size_t constexpr kHeapifyCapacityThreshold =
  1003. 100 * kHeapifyCapacitySize;
  1004. typedef typename std::
  1005. conditional<kHasInlineCapacity, HeapPtrWithCapacity, HeapPtr>::type
  1006. PointerType;
  1007. union Data {
  1008. explicit Data() {
  1009. pdata_.heap_ = nullptr;
  1010. }
  1011. PointerType pdata_;
  1012. InlineStorageType storage_;
  1013. value_type* buffer() noexcept {
  1014. void* vp = &storage_;
  1015. return static_cast<value_type*>(vp);
  1016. }
  1017. value_type const* buffer() const noexcept {
  1018. return const_cast<Data*>(this)->buffer();
  1019. }
  1020. value_type* heap() noexcept {
  1021. if (kHasInlineCapacity || !detail::pointerFlagGet(pdata_.heap_)) {
  1022. return static_cast<value_type*>(pdata_.heap_);
  1023. } else {
  1024. return static_cast<value_type*>(detail::shiftPointer(
  1025. detail::pointerFlagClear(pdata_.heap_), kHeapifyCapacitySize));
  1026. }
  1027. }
  1028. value_type const* heap() const noexcept {
  1029. return const_cast<Data*>(this)->heap();
  1030. }
  1031. bool hasCapacity() const {
  1032. return kHasInlineCapacity || detail::pointerFlagGet(pdata_.heap_);
  1033. }
  1034. InternalSizeType getCapacity() const {
  1035. return pdata_.getCapacity();
  1036. }
  1037. void setCapacity(InternalSizeType c) {
  1038. pdata_.setCapacity(c);
  1039. }
  1040. void freeHeap() {
  1041. auto vp = detail::pointerFlagClear(pdata_.heap_);
  1042. free(vp);
  1043. }
  1044. } u;
  1045. };
  1046. FOLLY_SV_PACK_POP
  1047. //////////////////////////////////////////////////////////////////////
  1048. // Basic guarantee only, or provides the nothrow guarantee iff T has a
  1049. // nothrow move or copy constructor.
  1050. template <class T, std::size_t MaxInline, class A, class B, class C>
  1051. void swap(
  1052. small_vector<T, MaxInline, A, B, C>& a,
  1053. small_vector<T, MaxInline, A, B, C>& b) {
  1054. a.swap(b);
  1055. }
  1056. //////////////////////////////////////////////////////////////////////
  1057. namespace detail {
  1058. // Format support.
  1059. template <class T, size_t M, class A, class B, class C>
  1060. struct IndexableTraits<small_vector<T, M, A, B, C>>
  1061. : public IndexableTraitsSeq<small_vector<T, M, A, B, C>> {};
  1062. } // namespace detail
  1063. } // namespace folly
  1064. FOLLY_POP_WARNING
  1065. #undef FOLLY_SV_PACK_ATTR
  1066. #undef FOLLY_SV_PACK_PUSH
  1067. #undef FOLLY_SV_PACK_POP