-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
FBVector.h
1696 lines (1498 loc) · 51.8 KB
/
FBVector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Nicholas Ormrod (njormrod)
* Andrei Alexandrescu (aalexandre)
*
* FBVector is Facebook's drop-in implementation of std::vector. It has special
* optimizations for use with relocatable types and jemalloc.
*/
#pragma once
//=============================================================================
// headers
#include <algorithm>
#include <cassert>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <folly/FormatTraits.h>
#include <folly/Likely.h>
#include <folly/ScopeGuard.h>
#include <folly/Traits.h>
#include <folly/lang/Exception.h>
#include <folly/memory/Malloc.h>
//=============================================================================
// forward declaration
namespace folly {
template <class T, class Allocator = std::allocator<T>>
class fbvector;
} // namespace folly
//=============================================================================
// unrolling
#define FOLLY_FBV_UNROLL_PTR(first, last, OP) \
do { \
for (; (last) - (first) >= 4; (first) += 4) { \
OP(((first) + 0)); \
OP(((first) + 1)); \
OP(((first) + 2)); \
OP(((first) + 3)); \
} \
for (; (first) != (last); ++(first)) \
OP((first)); \
} while (0)
//=============================================================================
///////////////////////////////////////////////////////////////////////////////
// //
// fbvector class //
// //
///////////////////////////////////////////////////////////////////////////////
namespace folly {
namespace detail {
inline void* thunk_return_nullptr() {
return nullptr;
}
} // namespace detail
template <class T, class Allocator>
class fbvector {
//===========================================================================
//---------------------------------------------------------------------------
// implementation
private:
typedef std::allocator_traits<Allocator> A;
struct Impl : public Allocator {
// typedefs
typedef typename A::pointer pointer;
typedef typename A::size_type size_type;
// data
pointer b_, e_, z_;
// constructors
Impl() : Allocator(), b_(nullptr), e_(nullptr), z_(nullptr) {}
/* implicit */ Impl(const Allocator& alloc)
: Allocator(alloc), b_(nullptr), e_(nullptr), z_(nullptr) {}
/* implicit */ Impl(Allocator&& alloc)
: Allocator(std::move(alloc)), b_(nullptr), e_(nullptr), z_(nullptr) {}
/* implicit */ Impl(size_type n, const Allocator& alloc = Allocator())
: Allocator(alloc) {
init(n);
}
Impl(Impl&& other) noexcept
: Allocator(std::move(other)),
b_(other.b_),
e_(other.e_),
z_(other.z_) {
other.b_ = other.e_ = other.z_ = nullptr;
}
// destructor
~Impl() { destroy(); }
// allocation
// note that 'allocate' and 'deallocate' are inherited from Allocator
T* D_allocate(size_type n) {
if (usingStdAllocator) {
return static_cast<T*>(checkedMalloc(n * sizeof(T)));
} else {
return std::allocator_traits<Allocator>::allocate(*this, n);
}
}
void D_deallocate(T* p, size_type n) noexcept {
if (usingStdAllocator) {
free(p);
} else {
std::allocator_traits<Allocator>::deallocate(*this, p, n);
}
}
// helpers
void swapData(Impl& other) {
std::swap(b_, other.b_);
std::swap(e_, other.e_);
std::swap(z_, other.z_);
}
// data ops
inline void destroy() noexcept {
if (b_) {
// THIS DISPATCH CODE IS DUPLICATED IN fbvector::D_destroy_range_a.
// It has been inlined here for speed. It calls the static fbvector
// methods to perform the actual destruction.
if (usingStdAllocator) {
S_destroy_range(b_, e_);
} else {
S_destroy_range_a(*this, b_, e_);
}
D_deallocate(b_, size_type(z_ - b_));
}
}
void init(size_type n) {
if (UNLIKELY(n == 0)) {
b_ = e_ = z_ = nullptr;
} else {
size_type sz = folly::goodMallocSize(n * sizeof(T)) / sizeof(T);
b_ = D_allocate(sz);
e_ = b_;
z_ = b_ + sz;
}
}
void set(pointer newB, size_type newSize, size_type newCap) {
z_ = newB + newCap;
e_ = newB + newSize;
b_ = newB;
}
void reset(size_type newCap) {
destroy();
auto rollback = makeGuard([&] { init(0); });
init(newCap);
rollback.dismiss();
}
void reset() { // same as reset(0)
destroy();
b_ = e_ = z_ = nullptr;
}
} impl_;
static void swap(Impl& a, Impl& b) {
using std::swap;
if (!usingStdAllocator) {
swap(static_cast<Allocator&>(a), static_cast<Allocator&>(b));
}
a.swapData(b);
}
//===========================================================================
//---------------------------------------------------------------------------
// types and constants
public:
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef typename std::make_signed<size_type>::type difference_type;
typedef Allocator allocator_type;
typedef typename A::pointer pointer;
typedef typename A::const_pointer const_pointer;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
private:
static constexpr bool should_pass_by_value =
is_trivially_copyable<T>::value &&
sizeof(T) <= 16; // don't force large structures to be passed by value
typedef typename std::conditional<should_pass_by_value, T, const T&>::type VT;
typedef typename std::conditional<should_pass_by_value, T, T&&>::type MT;
static constexpr bool usingStdAllocator =
std::is_same<Allocator, std::allocator<T>>::value;
typedef bool_constant<
usingStdAllocator || A::propagate_on_container_move_assignment::value>
moveIsSwap;
//===========================================================================
//---------------------------------------------------------------------------
// allocator helpers
private:
//---------------------------------------------------------------------------
// allocate
T* M_allocate(size_type n) { return impl_.D_allocate(n); }
//---------------------------------------------------------------------------
// deallocate
void M_deallocate(T* p, size_type n) noexcept { impl_.D_deallocate(p, n); }
//---------------------------------------------------------------------------
// construct
// GCC is very sensitive to the exact way that construct is called. For
// that reason there are several different specializations of construct.
template <typename U, typename... Args>
void M_construct(U* p, Args&&... args) {
if (usingStdAllocator) {
new (p) U(std::forward<Args>(args)...);
} else {
std::allocator_traits<Allocator>::construct(
impl_, p, std::forward<Args>(args)...);
}
}
template <typename U, typename... Args>
static void S_construct(U* p, Args&&... args) {
new (p) U(std::forward<Args>(args)...);
}
template <typename U, typename... Args>
static void S_construct_a(Allocator& a, U* p, Args&&... args) {
std::allocator_traits<Allocator>::construct(
a, p, std::forward<Args>(args)...);
}
// scalar optimization
// TODO we can expand this optimization to: default copyable and assignable
template <
typename U,
typename Enable = typename std::enable_if<std::is_scalar<U>::value>::type>
void M_construct(U* p, U arg) {
if (usingStdAllocator) {
*p = arg;
} else {
std::allocator_traits<Allocator>::construct(impl_, p, arg);
}
}
template <
typename U,
typename Enable = typename std::enable_if<std::is_scalar<U>::value>::type>
static void S_construct(U* p, U arg) {
*p = arg;
}
template <
typename U,
typename Enable = typename std::enable_if<std::is_scalar<U>::value>::type>
static void S_construct_a(Allocator& a, U* p, U arg) {
std::allocator_traits<Allocator>::construct(a, p, arg);
}
// const& optimization
template <
typename U,
typename Enable =
typename std::enable_if<!std::is_scalar<U>::value>::type>
void M_construct(U* p, const U& value) {
if (usingStdAllocator) {
new (p) U(value);
} else {
std::allocator_traits<Allocator>::construct(impl_, p, value);
}
}
template <
typename U,
typename Enable =
typename std::enable_if<!std::is_scalar<U>::value>::type>
static void S_construct(U* p, const U& value) {
new (p) U(value);
}
template <
typename U,
typename Enable =
typename std::enable_if<!std::is_scalar<U>::value>::type>
static void S_construct_a(Allocator& a, U* p, const U& value) {
std::allocator_traits<Allocator>::construct(a, p, value);
}
//---------------------------------------------------------------------------
// destroy
void M_destroy(T* p) noexcept {
if (usingStdAllocator) {
if (!std::is_trivially_destructible<T>::value) {
p->~T();
}
} else {
std::allocator_traits<Allocator>::destroy(impl_, p);
}
}
//===========================================================================
//---------------------------------------------------------------------------
// algorithmic helpers
private:
//---------------------------------------------------------------------------
// destroy_range
// wrappers
void M_destroy_range_e(T* pos) noexcept {
D_destroy_range_a(pos, impl_.e_);
impl_.e_ = pos;
}
// dispatch
// THIS DISPATCH CODE IS DUPLICATED IN IMPL. SEE IMPL FOR DETAILS.
void D_destroy_range_a(T* first, T* last) noexcept {
if (usingStdAllocator) {
S_destroy_range(first, last);
} else {
S_destroy_range_a(impl_, first, last);
}
}
// allocator
static void S_destroy_range_a(Allocator& a, T* first, T* last) noexcept {
for (; first != last; ++first) {
std::allocator_traits<Allocator>::destroy(a, first);
}
}
// optimized
static void S_destroy_range(T* first, T* last) noexcept {
if (!std::is_trivially_destructible<T>::value) {
#define FOLLY_FBV_OP(p) (p)->~T()
// EXPERIMENTAL DATA on fbvector<vector<int>> (where each vector<int> has
// size 0), were vector<int> to be relocatable.
// The unrolled version seems to work faster for small to medium sized
// fbvectors. It gets a 10% speedup on fbvectors of size 1024, 64, and
// 16.
// The simple loop version seems to work faster for large fbvectors. The
// unrolled version is about 6% slower on fbvectors on size 16384.
// The two methods seem tied for very large fbvectors. The unrolled
// version is about 0.5% slower on size 262144.
// for (; first != last; ++first) first->~T();
FOLLY_FBV_UNROLL_PTR(first, last, FOLLY_FBV_OP);
#undef FOLLY_FBV_OP
}
}
//---------------------------------------------------------------------------
// uninitialized_fill_n
// wrappers
void M_uninitialized_fill_n_e(size_type sz) {
D_uninitialized_fill_n_a(impl_.e_, sz);
impl_.e_ += sz;
}
void M_uninitialized_fill_n_e(size_type sz, VT value) {
D_uninitialized_fill_n_a(impl_.e_, sz, value);
impl_.e_ += sz;
}
// dispatch
void D_uninitialized_fill_n_a(T* dest, size_type sz) {
if (usingStdAllocator) {
S_uninitialized_fill_n(dest, sz);
} else {
S_uninitialized_fill_n_a(impl_, dest, sz);
}
}
void D_uninitialized_fill_n_a(T* dest, size_type sz, VT value) {
if (usingStdAllocator) {
S_uninitialized_fill_n(dest, sz, value);
} else {
S_uninitialized_fill_n_a(impl_, dest, sz, value);
}
}
// allocator
template <typename... Args>
static void S_uninitialized_fill_n_a(
Allocator& a, T* dest, size_type sz, Args&&... args) {
auto b = dest;
auto e = dest + sz;
auto rollback = makeGuard([&] { S_destroy_range_a(a, dest, b); });
for (; b != e; ++b) {
std::allocator_traits<Allocator>::construct(
a, b, std::forward<Args>(args)...);
}
rollback.dismiss();
}
// optimized
static void S_uninitialized_fill_n(T* dest, size_type n) {
if (folly::IsZeroInitializable<T>::value) {
if (LIKELY(n != 0)) {
std::memset((void*)dest, 0, sizeof(T) * n);
}
} else {
auto b = dest;
auto e = dest + n;
auto rollback = makeGuard([&] {
--b;
for (; b >= dest; --b) {
b->~T();
}
});
for (; b != e; ++b) {
S_construct(b);
}
rollback.dismiss();
}
}
static void S_uninitialized_fill_n(T* dest, size_type n, const T& value) {
auto b = dest;
auto e = dest + n;
auto rollback = makeGuard([&] { S_destroy_range(dest, b); });
for (; b != e; ++b) {
S_construct(b, value);
}
rollback.dismiss();
}
//---------------------------------------------------------------------------
// uninitialized_copy
// it is possible to add an optimization for the case where
// It = move(T*) and IsRelocatable<T> and Is0Initializeable<T>
// wrappers
template <typename It>
void M_uninitialized_copy_e(It first, It last) {
D_uninitialized_copy_a(impl_.e_, first, last);
impl_.e_ += std::distance(first, last);
}
template <typename It>
void M_uninitialized_move_e(It first, It last) {
D_uninitialized_move_a(impl_.e_, first, last);
impl_.e_ += std::distance(first, last);
}
// dispatch
template <typename It>
void D_uninitialized_copy_a(T* dest, It first, It last) {
if (usingStdAllocator) {
if (folly::is_trivially_copyable<T>::value) {
S_uninitialized_copy_bits(dest, first, last);
} else {
S_uninitialized_copy(dest, first, last);
}
} else {
S_uninitialized_copy_a(impl_, dest, first, last);
}
}
template <typename It>
void D_uninitialized_move_a(T* dest, It first, It last) {
D_uninitialized_copy_a(
dest, std::make_move_iterator(first), std::make_move_iterator(last));
}
// allocator
template <typename It>
static void S_uninitialized_copy_a(Allocator& a, T* dest, It first, It last) {
auto b = dest;
auto rollback = makeGuard([&] { S_destroy_range_a(a, dest, b); });
for (; first != last; ++first, ++b) {
std::allocator_traits<Allocator>::construct(a, b, *first);
}
rollback.dismiss();
}
// optimized
template <typename It>
static void S_uninitialized_copy(T* dest, It first, It last) {
auto b = dest;
auto rollback = makeGuard([&] { S_destroy_range(dest, b); });
for (; first != last; ++first, ++b) {
S_construct(b, *first);
}
rollback.dismiss();
}
static void S_uninitialized_copy_bits(
T* dest, const T* first, const T* last) {
if (last != first) {
std::memcpy((void*)dest, (void*)first, (last - first) * sizeof(T));
}
}
static void S_uninitialized_copy_bits(
T* dest, std::move_iterator<T*> first, std::move_iterator<T*> last) {
T* bFirst = first.base();
T* bLast = last.base();
if (bLast != bFirst) {
std::memcpy((void*)dest, (void*)bFirst, (bLast - bFirst) * sizeof(T));
}
}
template <typename It>
static void S_uninitialized_copy_bits(T* dest, It first, It last) {
S_uninitialized_copy(dest, first, last);
}
//---------------------------------------------------------------------------
// copy_n
// This function is "unsafe": it assumes that the iterator can be advanced at
// least n times. However, as a private function, that unsafety is managed
// wholly by fbvector itself.
template <typename It>
static It S_copy_n(T* dest, It first, size_type n) {
auto e = dest + n;
for (; dest != e; ++dest, ++first) {
*dest = *first;
}
return first;
}
static const T* S_copy_n(T* dest, const T* first, size_type n) {
if (is_trivially_copyable<T>::value) {
std::memcpy((void*)dest, (void*)first, n * sizeof(T));
return first + n;
} else {
return S_copy_n<const T*>(dest, first, n);
}
}
static std::move_iterator<T*> S_copy_n(
T* dest, std::move_iterator<T*> mIt, size_type n) {
if (is_trivially_copyable<T>::value) {
T* first = mIt.base();
std::memcpy((void*)dest, (void*)first, n * sizeof(T));
return std::make_move_iterator(first + n);
} else {
return S_copy_n<std::move_iterator<T*>>(dest, mIt, n);
}
}
//===========================================================================
//---------------------------------------------------------------------------
// relocation helpers
private:
// Relocation is divided into three parts:
//
// 1: relocate_move
// Performs the actual movement of data from point a to point b.
//
// 2: relocate_done
// Destroys the old data.
//
// 3: relocate_undo
// Destoys the new data and restores the old data.
//
// The three steps are used because there may be an exception after part 1
// has completed. If that is the case, then relocate_undo can nullify the
// initial move. Otherwise, relocate_done performs the last bit of tidying
// up.
//
// The relocation trio may use either memcpy, move, or copy. It is decided
// by the following case statement:
//
// IsRelocatable && usingStdAllocator -> memcpy
// has_nothrow_move && usingStdAllocator -> move
// cannot copy -> move
// default -> copy
//
// If the class is non-copyable then it must be movable. However, if the
// move constructor is not noexcept, i.e. an error could be thrown, then
// relocate_undo will be unable to restore the old data, for fear of a
// second exception being thrown. This is a known and unavoidable
// deficiency. In lieu of a strong exception guarantee, relocate_undo does
// the next best thing: it provides a weak exception guarantee by
// destroying the new data, but leaving the old data in an indeterminate
// state. Note that that indeterminate state will be valid, since the
// old data has not been destroyed; it has merely been the source of a
// move, which is required to leave the source in a valid state.
// wrappers
void M_relocate(T* newB) {
relocate_move(newB, impl_.b_, impl_.e_);
relocate_done(newB, impl_.b_, impl_.e_);
}
// dispatch type trait
typedef bool_constant<folly::IsRelocatable<T>::value && usingStdAllocator>
relocate_use_memcpy;
typedef bool_constant<
(std::is_nothrow_move_constructible<T>::value && usingStdAllocator) ||
!std::is_copy_constructible<T>::value>
relocate_use_move;
// move
void relocate_move(T* dest, T* first, T* last) {
relocate_move_or_memcpy(dest, first, last, relocate_use_memcpy());
}
void relocate_move_or_memcpy(T* dest, T* first, T* last, std::true_type) {
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) {
relocate_move_or_copy(dest, first, last, relocate_use_move());
}
void relocate_move_or_copy(T* dest, T* first, T* last, std::true_type) {
D_uninitialized_move_a(dest, first, last);
}
void relocate_move_or_copy(T* dest, T* first, T* last, std::false_type) {
D_uninitialized_copy_a(dest, first, last);
}
// done
void relocate_done(T* /*dest*/, T* first, T* last) noexcept {
if (folly::IsRelocatable<T>::value && usingStdAllocator) {
// used memcpy; data has been relocated, do not call destructor
} else {
D_destroy_range_a(first, last);
}
}
// undo
void relocate_undo(T* dest, T* first, T* last) noexcept {
if (folly::IsRelocatable<T>::value && usingStdAllocator) {
// used memcpy, old data is still valid, nothing to do
} else if (
std::is_nothrow_move_constructible<T>::value && usingStdAllocator) {
// noexcept move everything back, aka relocate_move
relocate_move(first, dest, dest + (last - first));
} else if (!std::is_copy_constructible<T>::value) {
// weak guarantee
D_destroy_range_a(dest, dest + (last - first));
} else {
// used copy, old data is still valid
D_destroy_range_a(dest, dest + (last - first));
}
}
//===========================================================================
//---------------------------------------------------------------------------
// construct/copy/destroy
public:
fbvector() = default;
explicit fbvector(const Allocator& a) : impl_(a) {}
explicit fbvector(size_type n, const Allocator& a = Allocator())
: impl_(n, a) {
M_uninitialized_fill_n_e(n);
}
fbvector(size_type n, VT value, const Allocator& a = Allocator())
: impl_(n, a) {
M_uninitialized_fill_n_e(n, value);
}
template <
class It,
class Category = typename std::iterator_traits<It>::iterator_category>
fbvector(It first, It last, const Allocator& a = Allocator())
: fbvector(first, last, a, Category()) {}
fbvector(const fbvector& other)
: impl_(
other.size(),
A::select_on_container_copy_construction(other.impl_)) {
M_uninitialized_copy_e(other.begin(), other.end());
}
fbvector(fbvector&& other) noexcept : impl_(std::move(other.impl_)) {}
fbvector(const fbvector& other, const Allocator& a)
: fbvector(other.begin(), other.end(), a) {}
/* may throw */ fbvector(fbvector&& other, const Allocator& a) : impl_(a) {
if (impl_ == other.impl_) {
impl_.swapData(other.impl_);
} else {
impl_.init(other.size());
M_uninitialized_move_e(other.begin(), other.end());
}
}
fbvector(std::initializer_list<T> il, const Allocator& a = Allocator())
: fbvector(il.begin(), il.end(), a) {}
~fbvector() = default; // the cleanup occurs in impl_
fbvector& operator=(const fbvector& other) {
if (UNLIKELY(this == &other)) {
return *this;
}
if (!usingStdAllocator &&
A::propagate_on_container_copy_assignment::value) {
if (impl_ != other.impl_) {
// can't use other's different allocator to clean up self
impl_.reset();
}
(Allocator&)impl_ = (Allocator&)other.impl_;
}
assign(other.begin(), other.end());
return *this;
}
fbvector& operator=(fbvector&& other) {
if (UNLIKELY(this == &other)) {
return *this;
}
moveFrom(std::move(other), moveIsSwap());
return *this;
}
fbvector& operator=(std::initializer_list<T> il) {
assign(il.begin(), il.end());
return *this;
}
template <
class It,
class Category = typename std::iterator_traits<It>::iterator_category>
void assign(It first, It last) {
assign(first, last, Category());
}
void assign(size_type n, VT value) {
if (n > capacity()) {
// Not enough space. Do not reserve in place, since we will
// discard the old values anyways.
if (dataIsInternalAndNotVT(value)) {
T copy(std::move(value));
impl_.reset(n);
M_uninitialized_fill_n_e(n, copy);
} else {
impl_.reset(n);
M_uninitialized_fill_n_e(n, value);
}
} else if (n <= size()) {
auto newE = impl_.b_ + n;
std::fill(impl_.b_, newE, value);
M_destroy_range_e(newE);
} else {
std::fill(impl_.b_, impl_.e_, value);
M_uninitialized_fill_n_e(n - size(), value);
}
}
void assign(std::initializer_list<T> il) { assign(il.begin(), il.end()); }
allocator_type get_allocator() const noexcept { return impl_; }
private:
// contract dispatch for iterator types fbvector(It first, It last)
template <class ForwardIterator>
fbvector(
ForwardIterator first,
ForwardIterator last,
const Allocator& a,
std::forward_iterator_tag)
: impl_(size_type(std::distance(first, last)), a) {
M_uninitialized_copy_e(first, last);
}
template <class InputIterator>
fbvector(
InputIterator first,
InputIterator last,
const Allocator& a,
std::input_iterator_tag)
: impl_(a) {
for (; first != last; ++first) {
emplace_back(*first);
}
}
// contract dispatch for allocator movement in operator=(fbvector&&)
void moveFrom(fbvector&& other, std::true_type) { swap(impl_, other.impl_); }
void moveFrom(fbvector&& other, std::false_type) {
if (impl_ == other.impl_) {
impl_.swapData(other.impl_);
} else {
impl_.reset(other.size());
M_uninitialized_move_e(other.begin(), other.end());
}
}
// contract dispatch for iterator types in assign(It first, It last)
template <class ForwardIterator>
void assign(
ForwardIterator first, ForwardIterator last, std::forward_iterator_tag) {
const auto newSize = size_type(std::distance(first, last));
if (newSize > capacity()) {
impl_.reset(newSize);
M_uninitialized_copy_e(first, last);
} else if (newSize <= size()) {
auto newEnd = std::copy(first, last, impl_.b_);
M_destroy_range_e(newEnd);
} else {
auto mid = S_copy_n(impl_.b_, first, size());
M_uninitialized_copy_e<decltype(last)>(mid, last);
}
}
template <class InputIterator>
void assign(
InputIterator first, InputIterator last, std::input_iterator_tag) {
auto p = impl_.b_;
for (; first != last && p != impl_.e_; ++first, ++p) {
*p = *first;
}
if (p != impl_.e_) {
M_destroy_range_e(p);
} else {
for (; first != last; ++first) {
emplace_back(*first);
}
}
}
// contract dispatch for aliasing under VT optimization
bool dataIsInternalAndNotVT(const T& t) {
if (should_pass_by_value) {
return false;
}
return dataIsInternal(t);
}
bool dataIsInternal(const T& t) {
return UNLIKELY(
impl_.b_ <= std::addressof(t) && std::addressof(t) < impl_.e_);
}
//===========================================================================
//---------------------------------------------------------------------------
// iterators
public:
iterator begin() noexcept { return impl_.b_; }
const_iterator begin() const noexcept { return impl_.b_; }
iterator end() noexcept { return impl_.e_; }
const_iterator end() const noexcept { return impl_.e_; }
reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(end());
}
reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(begin());
}
const_iterator cbegin() const noexcept { return impl_.b_; }
const_iterator cend() const noexcept { return impl_.e_; }
const_reverse_iterator crbegin() const noexcept {
return const_reverse_iterator(end());
}
const_reverse_iterator crend() const noexcept {
return const_reverse_iterator(begin());
}
//===========================================================================
//---------------------------------------------------------------------------
// capacity
public:
size_type size() const noexcept { return size_type(impl_.e_ - impl_.b_); }
size_type max_size() const noexcept {
// good luck gettin' there
return ~size_type(0);
}
void resize(size_type n) {
if (n <= size()) {
M_destroy_range_e(impl_.b_ + n);
} else {
reserve(n);
M_uninitialized_fill_n_e(n - size());
}
}
void resize(size_type n, VT t) {
if (n <= size()) {
M_destroy_range_e(impl_.b_ + n);
} else if (dataIsInternalAndNotVT(t) && n > capacity()) {
T copy(t);
reserve(n);
M_uninitialized_fill_n_e(n - size(), copy);
} else {
reserve(n);
M_uninitialized_fill_n_e(n - size(), t);
}
}
size_type capacity() const noexcept { return size_type(impl_.z_ - impl_.b_); }
bool empty() const noexcept { return impl_.b_ == impl_.e_; }
void reserve(size_type n) {
if (n <= capacity()) {
return;
}
if (impl_.b_ && reserve_in_place(n)) {
return;
}
auto newCap = folly::goodMallocSize(n * sizeof(T)) / sizeof(T);
auto newB = M_allocate(newCap);
{
auto rollback = makeGuard([&] { M_deallocate(newB, newCap); });
M_relocate(newB);
rollback.dismiss();
}
if (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();
if (newCap >= oldCap) {
return;
}
void* p = impl_.b_;
// xallocx() will shrink to precisely newCapacityBytes (which was generated
// by goodMallocSize()) if it successfully shrinks in place.
if ((usingJEMalloc() && usingStdAllocator) &&
newCapacityBytes >= folly::jemallocMinInPlaceExpandable &&
xallocx(p, newCapacityBytes, 0, 0) == newCapacityBytes) {
impl_.z_ += newCap - oldCap;
} else {
T* newB = static_cast<T*>(catch_exception(
[&] { return M_allocate(newCap); }, //
&detail::thunk_return_nullptr));
if (!newB) {
return;
}
if (!catch_exception(
[&] { return M_relocate(newB), true; },
[&] { return M_deallocate(newB, newCap), false; })) {
return;
}