-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmemory.h
1733 lines (1501 loc) · 57.9 KB
/
memory.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) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements the following functions from the C++ standard that
// are found in the <memory> header:
//
// Temporary memory:
// returnTemporaryBuffer
//
// Utility:
// late_constructed - Extention to standard functionality.
//
// Uninitialized operations:
// These are the same as the copy, fill, and fillN algorithms, except that
// they *construct* the destination with the source values rather than assign
// the destination with the source values.
//
// uninitializedCopy
// uninitializedCopy_n
// uninitialized_default_construct
// uninitialized_default_construct_n
// uninitializedMove
// uninitializedMove_if_noexcept - Extention to standard functionality.
// uninitializedMove_n
// uninitializedFill
// uninitializedFillN
// uninitialized_value_construct
// uninitialized_value_construct_n
// uninitializedCopyFill - Extention to standard functionality.
// uninitializedFillCopy - Extention to standard functionality.
// uninitializedCopyCopy - Extention to standard functionality.
//
// In-place destructor helpers:
// destruct(T*) - Non-standard extension. Equivalent to destroy_at(T*)
// destruct(first, last) - Non-standard extension. Equivalent to destroy(first, last)
// destroy_at(T*)
// destroy(first, last)
// destroy_n(first, n)
//
// Alignment
// align
// align_advance - Extention to standard functionality.
//
// Allocator-related
// uses_allocator
// allocator_arg_t
// allocator_arg
//
// Pointers
// pointer_traits
//
// Deprecations:
// (EASTL_REMOVE_AT_2024_APRIL)
// uninitialized_relocate - Use one of the other uninitialized_xxxx functions with move semantics.
// uninitialized_default_fill - Use uninitialized_value_construct instead.
// uninitialized_default_fillN - Use uninitialized_value_construct_n instead.
//
// (EASTL_REMOVE_AT_2024_SEPT)
// uninitializedCopyPtr - Use uninitializedCopy instead.
// uninitializedMove_ptr - Use uninitializedMove instead.
// uninitializedMove_ptr_if_noexcept- Use uninitializedMove_if_noexcept instead.
// uninitializedFillPtr - Use uninitializedFill instead.
// uninitializedFillNPtr - Use uninitializedFillN instead.
//
// Deprecated in C++17:
// getTemporaryBuffer
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_MEMORY_H
#define EASTL_MEMORY_H
#include <eastl/internal/config.h>
#include <eastl/internal/memory_base.h>
#include <eastl/internal/pair_fwd_decls.h>
#include <eastl/internal/functional_base.h>
#include <eastl/algorithm.h>
#include <eastl/type_traits.h>
#include <eastl/allocator.h>
#include <eastl/iterator.h>
#include <eastl/utility.h>
#include <eastl/numeric_limits.h>
EA_DISABLE_ALL_VC_WARNINGS()
#include <stdlib.h>
#include <new>
EA_RESTORE_ALL_VC_WARNINGS()
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4146 - unary minus operator applied to unsigned type, result still unsigned
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
EA_DISABLE_VC_WARNING(4530 4146 4571);
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// EASTL_TEMP_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_TEMP_DEFAULT_NAME
#define EASTL_TEMP_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " temp" // Unless the user overrides something, this is "EASTL temp".
#endif
/// getTemporaryBuffer
///
/// From the C++ standard, section 20.4.3:
/// 1 Effects: Obtains a pointer to storage sufficient to store up to n adjacent T objects.
/// 2 Returns: A pair containing the buffer's address and capacity (in the units of sizeof(T)),
/// or a pair of 0 values if no storage can be obtained.
///
/// Note: The return value is space to hold T elements, but no T elements are constructed.
///
/// Our implementation here differs slightly in that we have alignment, alignmentOffset, and pName arguments.
/// Note that you can use the EASTL_NAME_VAL macro to make names go away in release builds.
///
/// Example usage:
/// pair<int*, ptrdiff_t> pr = getTemporaryBuffer<int>(100, 0, 0, EASTL_NAME_VAL("Temp int array"));
/// memset(pr.first, 0, 100 * sizeof(int));
/// returnTemporaryBuffer(pr.first);
///
template <typename T>
EASTL_REMOVE_AT_2024_APRIL
eastl::pair<T*, ptrdiff_t> getTemporaryBuffer(ptrdiff_t n, size_t alignment = 1, size_t alignmentOffset = 0, const char* pName = EASTL_TEMP_DEFAULT_NAME)
{
EASTLAllocatorType allocator(*EASTLAllocatorDefault(), pName);
return eastl::pair<T*, ptrdiff_t>(static_cast<T*>(EASTLAllocAligned(allocator, n * sizeof(T), alignment, alignmentOffset)), n);
}
/// returnTemporaryBuffer
///
/// From the C++ standard, section 20.4.3:
/// 3 Effects: Deallocates the buffer to which p points.
/// 4 Requires: The buffer shall have been previously allocated by getTemporaryBuffer.
///
/// Note: This function merely frees space and does not destruct any T elements.
///
/// Example usage:
/// pair<int*, ptrdiff_t> pr = getTemporaryBuffer<int>(300);
/// memset(pr.first, 0, 300 * sizeof(int));
/// returnTemporaryBuffer(pr.first, pr.second);
///
template <typename T>
void returnTemporaryBuffer(T* p, ptrdiff_t n = 0)
{
EASTLAllocatorType& allocator(*EASTLAllocatorDefault());
EASTLFree(allocator, p, n * sizeof(T));
}
/// late_constructed
///
/// Implements a smart pointer type which separates the memory allocation of an object from
/// the object's construction. The primary use case is to declare a global variable of the
/// late_construction type, which allows the memory to be global but the constructor executes
/// at some point after main() begins as opposed to before main, which is often dangerous
/// for non-trivial types.
///
/// The autoConstruct template parameter controls whether the object is automatically default
/// constructed upon first reference or must be manually constructed upon the first use of
/// operator * or ->. autoConstruct is convenient but it causes * and -> to be slightly slower
/// and may result in construction at an inconvenient time.
///
/// The autoDestruct template parameter controls whether the object, if constructed, is automatically
/// destructed when ~late_constructed() is called or must be manually destructed via a call to
/// destruct().
///
/// While construction can be automatic or manual, automatic destruction support is always present.
/// Thus you aren't required in any case to manually call destruct. However, you may safely manually
/// destruct the object at any time before the late_constructed destructor is executed.
///
/// You may still use late_constructed after calling destruct(), including calling construct()
/// again to reconstruct the instance. destruct returns the late_constructed instance to a
/// state equivalent to before construct was called.
///
/// Caveat: While late_constructed instances can be declared in global scope and initialize
/// prior to main() executing, you cannot otherwise use such globally declared instances prior
/// to main with guaranteed behavior unless you can ensure that the late_constructed instance
/// is itself constructed prior to your use of it.
///
/// Example usage (demonstrating manual-construction):
/// late_constructed<Widget, false> gWidget;
///
/// void main(){
/// gWidget.construct(kScrollbarType, kVertical, "MyScrollbar");
/// gWidget->SetValue(15);
/// gWidget.destruct();
/// }
///
/// Example usage (demonstrating auto-construction):
/// late_constructed<Widget, true> gWidget;
///
/// void main(){
/// gWidget->SetValue(15);
/// // You may want to call destruct here, but aren't required to do so unless the Widget type requires it.
/// }
///
template <typename T, bool autoConstruct = true, bool autoDestruct = true>
class late_constructed
{
public:
using this_type = late_constructed<T, autoConstruct, autoDestruct>;
using value_type = T;
using storage_type = eastl::aligned_storage_t<sizeof(value_type), eastl::alignment_of_v<value_type>>;
late_constructed() EASTL_NOEXCEPT // In the case of the late_constructed instance being at global scope, we rely on the
: mStorage(), mpValue(nullptr) {} // compiler executing this constructor or placing the instance in auto-zeroed-at-startup memory.
~late_constructed()
{
if (autoDestruct && mpValue)
(*mpValue).~value_type();
}
template <typename... Args>
void construct(Args&&... args)
{
if(!mpValue)
mpValue = new (&mStorage) value_type(eastl::forward<Args>(args)...);
}
bool is_constructed() const EASTL_NOEXCEPT
{ return mpValue != nullptr; }
void destruct()
{
if(mpValue)
{
(*mpValue).~value_type();
mpValue = nullptr;
}
}
value_type& operator*() EASTL_NOEXCEPT
{
if(!mpValue)
construct();
EA_ANALYSIS_ASSUME(mpValue);
return *mpValue;
}
const value_type& operator*() const EASTL_NOEXCEPT
{
if(!mpValue)
construct();
EA_ANALYSIS_ASSUME(mpValue);
return *mpValue;
}
value_type* operator->() EASTL_NOEXCEPT
{
if(!mpValue)
construct();
return mpValue;
}
const value_type* operator->() const EASTL_NOEXCEPT
{
if(!mpValue)
construct();
return mpValue;
}
value_type* get() EASTL_NOEXCEPT
{
if(!mpValue)
construct();
return mpValue;
}
const value_type* get() const EASTL_NOEXCEPT
{
if(!mpValue)
construct();
return mpValue;
}
protected:
storage_type mStorage; // Declared first because it may have aligment requirements, and it would be more space-efficient if it was first.
value_type* mpValue;
};
// Specialization that doesn't auto-construct on demand.
template <typename T, bool autoDestruct>
class late_constructed<T, false, autoDestruct> : public late_constructed<T, true, autoDestruct>
{
public:
typedef late_constructed<T, true, autoDestruct> base_type;
typename base_type::value_type& operator*() EASTL_NOEXCEPT
{ EASTL_ASSERT(base_type::mpValue); return *base_type::mpValue; }
const typename base_type::value_type& operator*() const EASTL_NOEXCEPT
{ EASTL_ASSERT(base_type::mpValue); return *base_type::mpValue; }
typename base_type::value_type* operator->() EASTL_NOEXCEPT
{ EASTL_ASSERT(base_type::mpValue); return base_type::mpValue; }
const typename base_type::value_type* operator->() const EASTL_NOEXCEPT
{ EASTL_ASSERT(base_type::mpValue); return base_type::mpValue; }
typename base_type::value_type* get() EASTL_NOEXCEPT
{ return base_type::mpValue; }
const typename base_type::value_type* get() const EASTL_NOEXCEPT
{ return base_type::mpValue; }
};
/// raw_storage_iterator
///
/// From the C++11 Standard, section 20.6.10 p1
/// raw_storage_iterator is provided to enable algorithms to store their results into uninitialized memory.
/// The formal template parameter OutputIterator is required to have its operator* return an object for
/// which operator& is defined and returns a pointer to T, and is also required to satisfy the requirements
/// of an output iterator (24.2.4).
template <typename OutputIterator, typename T>
class EASTL_REMOVE_AT_2024_APRIL raw_storage_iterator
{
protected:
OutputIterator mIterator;
public:
typedef EASTL_ITC_NS::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit raw_storage_iterator(OutputIterator iterator)
: mIterator(iterator)
{
}
raw_storage_iterator& operator*()
{
return *this;
}
raw_storage_iterator& operator=(const T& value)
{
::new(eastl::addressof(*mIterator)) T(value);
return *this;
}
raw_storage_iterator& operator++()
{
++mIterator;
return *this;
}
raw_storage_iterator operator++(int)
{
raw_storage_iterator tempIterator = *this;
++mIterator;
return tempIterator;
}
};
/// uninitialized_relocate (formerly named uninitializedMove prior to C++11)
///
/// This utility is deprecated in favor of C++11 rvalue move functionality.
///
/// uninitialized_relocate takes a constructed sequence of objects and an
/// uninitialized destination buffer. In the case of any exception thrown
/// while moving the objects, any newly constructed objects are guaranteed
/// to be destructed and the input left fully constructed.
///
/// In the case where you need to do multiple moves atomically, split the
/// calls into uninitialized_relocate_start/abort/commit.
///
/// uninitialized_relocate_start can possibly throw an exception. If it does,
/// you don't need to do anything. However, if it returns without throwing
/// an exception you need to guarantee that either uninitialized_relocate_abort
/// or uninitialized_relocate_commit is called.
///
/// Both uninitialized_relocate_abort and uninitialize_move_commit are
/// guaranteed to not throw C++ exceptions.
namespace Internal
{
template <bool hasTrivialMove, typename iteratorTag>
struct uninitialized_relocate_impl
{
template <typename ForwardIterator, typename ForwardIteratorDest>
static ForwardIteratorDest do_move_start(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
#if EASTL_EXCEPTIONS_ENABLED
ForwardIteratorDest origDest(dest);
try
{
#endif
for(; first != last; ++first, ++dest)
::new((void*)eastl::addressof(*dest)) value_type(*first);
#if EASTL_EXCEPTIONS_ENABLED
}
catch(...)
{
for(; origDest < dest; ++origDest)
(*origDest).~value_type();
throw;
}
#endif
return dest;
}
template <typename ForwardIterator, typename ForwardIteratorDest>
static ForwardIteratorDest do_move_commit(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest) //throw()
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
for(; first != last; ++first, ++dest)
(*first).~value_type();
return dest;
}
template <typename ForwardIterator, typename ForwardIteratorDest>
static ForwardIteratorDest do_move_abort(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest) //throw()
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
for(; first != last; ++first, ++dest)
(*dest).~value_type();
return dest;
}
};
template <>
struct uninitialized_relocate_impl<true, EASTL_ITC_NS::random_access_iterator_tag>
{
template <typename T>
static T* do_move_start(T* first, T* last, T* dest)
{
if (first != last)
return (T*)memcpy(dest, first, (size_t)((uintptr_t)last - (uintptr_t)first)) + (last - first);
else
return dest;
}
template <typename T>
static T* do_move_commit(T* first, T* last, T* dest)
{
return dest + (last - first);
}
template <typename T>
static T* do_move_abort(T* first, T* last, T* dest)
{
return dest + (last - first);
}
};
}
EASTL_INTERNAL_DISABLE_DEPRECATED() // 'has_trivial_relocate': was declared deprecated
/// uninitialized_relocate_start, uninitialized_relocate_commit, uninitialized_relocate_abort
///
/// This utility is deprecated in favor of C++11 rvalue move functionality.
///
/// After calling uninitialized_relocate_start, if it doesn't throw an exception,
/// both the source and destination iterators point to undefined data. If it
/// does throw an exception, the destination remains uninitialized and the source
/// is as it was before.
///
/// In order to make the iterators valid again you need to call either uninitialized_relocate_abort
/// or uninitialized_relocate_commit. The abort call makes the original source
/// iterator valid again, and commit makes the destination valid. Both abort
/// and commit are guaranteed to not throw C++ exceptions.
///
/// Example usage:
/// iterator dest2 = uninitialized_relocate_start(first, last, dest);
/// try {
/// // some code here that might throw an exception
/// }
/// catch(...)
/// {
/// uninitialized_relocate_abort(first, last, dest);
/// throw;
/// }
/// uninitialized_relocate_commit(first, last, dest);
///
template <typename ForwardIterator, typename ForwardIteratorDest>
EASTL_REMOVE_AT_2024_APRIL inline ForwardIteratorDest uninitialized_relocate_start(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::iterator_category IC;
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type_input;
typedef typename eastl::iterator_traits<ForwardIteratorDest>::value_type value_type_output;
EASTL_INTERNAL_DISABLE_DEPRECATED()
const bool bHasTrivialMove = type_and<has_trivial_relocate<value_type_input>::value,
is_pointer<ForwardIterator>::value,
is_pointer<ForwardIteratorDest>::value,
is_same<value_type_input, value_type_output>::value>::value;
EASTL_INTERNAL_RESTORE_DEPRECATED()
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_start(first, last, dest);
}
template <typename ForwardIterator, typename ForwardIteratorDest>
EASTL_REMOVE_AT_2024_APRIL inline ForwardIteratorDest uninitialized_relocate_commit(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::iterator_category IC;
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type_input;
typedef typename eastl::iterator_traits<ForwardIteratorDest>::value_type value_type_output;
EASTL_INTERNAL_DISABLE_DEPRECATED()
const bool bHasTrivialMove = type_and<has_trivial_relocate<value_type_input>::value,
is_pointer<ForwardIterator>::value,
is_pointer<ForwardIteratorDest>::value,
is_same<value_type_input, value_type_output>::value>::value;
EASTL_INTERNAL_RESTORE_DEPRECATED()
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_commit(first, last, dest);
}
template <typename ForwardIterator, typename ForwardIteratorDest>
EASTL_REMOVE_AT_2024_APRIL inline ForwardIteratorDest uninitialized_relocate_abort(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::iterator_category IC;
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type_input;
typedef typename eastl::iterator_traits<ForwardIteratorDest>::value_type value_type_output;
EASTL_INTERNAL_DISABLE_DEPRECATED()
const bool bHasTrivialMove = type_and<has_trivial_relocate<value_type_input>::value,
is_pointer<ForwardIterator>::value,
is_pointer<ForwardIteratorDest>::value,
is_same<value_type_input, value_type_output>::value>::value;
EASTL_INTERNAL_RESTORE_DEPRECATED()
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_abort(first, last, dest);
}
/// uninitialized_relocate
///
/// See above for documentation.
///
template <typename ForwardIterator, typename ForwardIteratorDest>
EASTL_REMOVE_AT_2024_APRIL inline ForwardIteratorDest uninitialized_relocate(ForwardIterator first, ForwardIterator last, ForwardIteratorDest dest)
{
ForwardIteratorDest result = uninitialized_relocate_start(first, last, dest);
eastl::uninitialized_relocate_commit(first, last, dest);
return result;
}
EASTL_INTERNAL_RESTORE_DEPRECATED()
// uninitializedCopy
//
namespace Internal
{
template<bool isTriviallyCopyable, bool isInputIteratorReferenceAddressable, bool areIteratorsContiguous>
struct uninitializedCopy_impl
{
template <typename InputIterator, typename ForwardIterator>
static ForwardIterator impl(InputIterator first, InputIterator last, ForwardIterator dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(dest);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for (; first != last; ++first, ++currentDest)
::new(static_cast<void*>(eastl::addressof(*currentDest))) value_type(*first);
#if EASTL_EXCEPTIONS_ENABLED
}
catch (...)
{
for (; dest < currentDest; ++dest)
(*dest).~value_type();
throw;
}
#endif
return currentDest;
}
};
template<>
struct uninitializedCopy_impl<true, true, false>
{
template <typename InputIterator, typename ForwardIterator>
static ForwardIterator impl(InputIterator first, InputIterator last, ForwardIterator dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
for (; first != last; ++first, ++dest)
memmove(eastl::addressof(*dest), eastl::addressof(*first), sizeof(value_type));
return dest;
}
};
template<>
struct uninitializedCopy_impl<true, true, true>
{
template <typename InputIterator, typename ForwardIterator>
static ForwardIterator impl(InputIterator first, InputIterator last, ForwardIterator dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
if (EASTL_UNLIKELY(first == last))
return dest;
auto count = (last - first);
memmove(eastl::addressof(*dest), eastl::addressof(*first), sizeof(value_type) * count);
return dest + count;
}
};
}
/// uninitializedCopy
///
/// Copies a source range to a destination, copy-constructing the destination with
/// the source values (and not *assigning* the destination with the source values).
/// Returns the end of the destination range (i.e. dest + (last - first)).
///
/// Declaration:
/// template <typename InputIterator, typename ForwardIterator>
/// ForwardIterator uninitializedCopy(InputIterator sourceFirst, InputIterator sourceLast, ForwardIterator destination);
///
/// Example usage:
/// SomeClass* pArray = malloc(10 * sizeof(SomeClass));
/// uninitializedCopy(pSourceDataBegin, pSourceDataBegin + 10, pArray);
///
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedCopy(InputIterator first, InputIterator last, ForwardIterator result)
{
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IIC;
typedef typename eastl::iterator_traits<ForwardIterator>::iterator_category OIC;
typedef typename eastl::iterator_traits<InputIterator>::value_type value_type_input;
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type_output;
// isTriviallyCopyable identifies if (non-overlapping) objects may be safely copied by means of memcpy/memmove.
const bool isTriviallyCopyable = eastl::is_same<value_type_input, value_type_output>::value && eastl::is_trivially_copyable<value_type_output>::value;
// ie. is eastl::addressof(*first) valid? ie. invalid for iterators that return value_type&&.
const bool isInputIteratorReferenceAddressable = eastl::is_convertible<typename eastl::add_lvalue_reference<value_type_input>::type, typename eastl::iterator_traits<InputIterator>::reference>::value;
// can memcpy/memmove a contiguous block, not just the individual elements?
const bool areIteratorsContiguous = (eastl::is_pointer<InputIterator>::value || internal::is_contiguous_iterator<IIC>::value) &&
(eastl::is_pointer<ForwardIterator>::value || internal::is_contiguous_iterator<OIC>::value);
return Internal::uninitializedCopy_impl<isTriviallyCopyable, isInputIteratorReferenceAddressable, areIteratorsContiguous>::impl(first, last, result);
}
/// uninitializedCopy_n
///
/// Copies count elements from a range beginning at first to an uninitialized memory area
/// beginning at dest. The elements in the uninitialized area are constructed using copy constructor.
/// If an exception is thrown during the initialization, the function has no final effects.
///
/// first: Beginning of the range of the elements to copy.
/// dest: Beginning of the destination range.
/// return value: Iterator of dest type to the element past the last element copied.
///
namespace Internal
{
template <typename InputIterator, typename Count, typename ForwardIterator, typename IteratorTag>
struct uninitializedCopy_n_impl
{
static ForwardIterator impl(InputIterator first, Count n, ForwardIterator dest)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(dest);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for(; n > 0; --n, ++first, ++currentDest)
::new((void*)(eastl::addressof(*currentDest))) value_type(*first);
#if EASTL_EXCEPTIONS_ENABLED
}
catch(...)
{
for(; dest < currentDest; ++dest)
(*dest).~value_type();
throw;
}
#endif
return currentDest;
}
};
template <typename InputIterator, typename Count, typename ForwardIterator>
struct uninitializedCopy_n_impl<InputIterator, Count, ForwardIterator, EASTL_ITC_NS::random_access_iterator_tag>
{
static inline ForwardIterator impl(InputIterator first, Count n, ForwardIterator dest)
{
return eastl::uninitializedCopy(first, first + n, dest);
}
};
}
template<typename InputIterator, typename Count, typename ForwardIterator>
inline ForwardIterator uninitializedCopy_n(InputIterator first, Count n, ForwardIterator dest)
{
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
return Internal::uninitializedCopy_n_impl<InputIterator, Count, ForwardIterator, IC>::impl(first, n, dest);
}
/// uninitializedCopyPtr
///
/// This is a specialization of uninitializedCopy for iterators that are pointers. We use it because
/// internally it uses generic_iterator to make pointers act like regular eastl::iterator.
///
template <typename First, typename Last, typename Result>
EASTL_REMOVE_AT_2024_SEPT inline Result uninitializedCopyPtr(First first, Last last, Result result)
{
return eastl::uninitializedCopy(first, last, result);
}
/// uninitializedMove_ptr
///
/// This is a specialization of uninitializedMove for iterators that are pointers. We use it because
/// internally it uses generic_iterator to make pointers act like regular eastl::iterator.
///
template <typename First, typename Last, typename Result>
EASTL_REMOVE_AT_2024_SEPT inline Result uninitializedMove_ptr(First first, Last last, Result dest)
{
return uninitializedMove(first, last, dest);
}
/// uninitializedMove
///
/// Moves a source range to a destination, move-constructing the destination with
/// the source values (and not *assigning* the destination with the source values).
/// Returns the end of the destination range (i.e. dest + (last - first)).
///
/// uninitializedMove is not part of any current C++ Standard, up to C++14.
///
/// Declaration:
/// template <typename InputIterator, typename ForwardIterator>
/// ForwardIterator uninitializedMove(InputIterator sourceFirst, InputIterator sourceLast, ForwardIterator destination);
///
/// Example usage:
/// SomeClass* pArray = malloc(10 * sizeof(SomeClass));
/// uninitializedMove(pSourceDataBegin, pSourceDataBegin + 10, pArray);
///
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedMove(InputIterator first, InputIterator last, ForwardIterator dest)
{
return eastl::uninitializedCopy(eastl::make_move_iterator(first), eastl::make_move_iterator(last), dest);
}
/// uninitializedMove_if_noexcept
///
/// If the iterated type can be moved without exceptions, move construct the dest with the input. Else copy-construct
/// the dest witih the input. If move isn't supported by the compiler, do regular copy.
///
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedMove_if_noexcept(InputIterator first, InputIterator last, ForwardIterator dest)
{
#if EASTL_EXCEPTIONS_ENABLED
return eastl::uninitializedCopy(eastl::make_move_if_noexcept_iterator(first), eastl::make_move_if_noexcept_iterator(last), dest);
#else
return eastl::uninitializedMove(first, last, dest);
#endif
}
/// uninitializedMove_ptr_if_noexcept
///
template <typename First, typename Last, typename Result>
EASTL_REMOVE_AT_2024_SEPT inline Result uninitializedMove_ptr_if_noexcept(First first, Last last, Result dest)
{
return eastl::uninitializedMove_if_noexcept(first, last, dest);
}
/// uninitializedMove_n
///
/// Moves count elements from a range beginning at first to an uninitialized memory area
/// beginning at dest. The elements in the uninitialized area are constructed using copy constructor.
/// If an exception is thrown during the initialization, the function has no final effects.
///
/// first: Beginning of the range of the elements to move.
/// dest: Beginning of the destination range.
/// return value: Iterator of dest type to the element past the last element moved.
///
template<typename InputIterator, typename Count, typename ForwardIterator>
inline ForwardIterator uninitializedMove_n(InputIterator first, Count n, ForwardIterator dest)
{
return eastl::uninitializedCopy_n(eastl::make_move_iterator(first), n, dest);
}
// Disable warning C4345 - behavior change: an object of POD type constructed with an initializer of the form ()
// will be default-initialized.
// This is the behavior we intend below.
EA_DISABLE_VC_WARNING(4345)
/// uninitialized_default_fill
///
/// Deprecated. Use uninitialized_value_construct for the std conforming alternative.
///
/// Default-constructs the elements in the destination range.
/// Returns void. It wouldn't be useful to return the end of the destination range,
/// as that is the same as the 'last' input parameter.
///
/// Declaration:
/// template <typename ForwardIterator, typename T>
/// void uninitialized_default_fill(ForwardIterator destinationFirst, ForwardIterator destinationLast);
///
template <typename ForwardIterator>
EASTL_REMOVE_AT_2024_APRIL inline void uninitialized_default_fill(ForwardIterator first, ForwardIterator last)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for (; currentDest != last; ++currentDest)
::new (eastl::addressof(*currentDest)) value_type();
#if EASTL_EXCEPTIONS_ENABLED
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#endif
}
/// uninitialized_default_fillN
///
/// Deprecated. Use uninitialized_value_construct_n for the std conforming alternative.
///
/// Default-constructs the range of [first, first + n).
/// Returns void as per the C++ standard, though returning the end input iterator
/// value may be of use.
///
/// Declaration:
/// template <typename ForwardIterator, typename Count, typename T>
/// void uninitialized_default_fillN(ForwardIterator destination, Count n);
///
namespace Internal
{
template <typename ForwardIterator, typename Count>
inline void uninitialized_default_fillN_impl(ForwardIterator first, Count n, false_type)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for (; n > 0; --n, ++currentDest)
::new (eastl::addressof(*currentDest)) value_type();
#if EASTL_EXCEPTIONS_ENABLED
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#endif
}
template <typename ForwardIterator, typename Count>
inline void uninitialized_default_fillN_impl(ForwardIterator first, Count n, true_type)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
memset(first, 0, sizeof(value_type) * n);
}
}
template <typename ForwardIterator, typename Count>
EASTL_REMOVE_AT_2024_APRIL inline void uninitialized_default_fillN(ForwardIterator first, Count n)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
Internal::uninitialized_default_fillN_impl(first, n, is_scalar<value_type>());
}
EA_RESTORE_VC_WARNING()
/// uninitialized_default_construct
///
/// Constructs objects in the uninitialized storage designated by the range [first, last) by default-initialization.
///
/// Default-initialization:
/// If T is a class, the default constructor is called; otherwise, no initialization is done, resulting in
/// indeterminate values.
///
/// http://en.cppreference.com/w/cpp/memory/uninitialized_default_construct
///
template <typename ForwardIterator>
inline void uninitialized_default_construct(ForwardIterator first, ForwardIterator last)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for (; currentDest != last; ++currentDest)
::new (eastl::addressof(*currentDest)) value_type;
#if EASTL_EXCEPTIONS_ENABLED
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#endif
}
/// uninitialized_default_construct_n
///
/// Constructs n objects in the uninitialized storage starting at first by default-initialization.
///
/// http://en.cppreference.com/w/cpp/memory/uninitialized_default_construct_n
///
template <typename ForwardIterator, typename Count>
inline ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Count n)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);
#if EASTL_EXCEPTIONS_ENABLED
try
{
#endif
for (; n > 0; --n, ++currentDest)
::new (eastl::addressof(*currentDest)) value_type;
return currentDest;
#if EASTL_EXCEPTIONS_ENABLED
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#endif
}
/// uninitializedFill
///
/// Copy-constructs the elements in the destination range with the given input value.
/// Returns void. It wouldn't be useful to return the end of the destination range,
/// as that is the same as the 'last' input parameter.
///
/// Declaration:
/// template <typename ForwardIterator, typename T>
/// void uninitializedFill(ForwardIterator destinationFirst, ForwardIterator destinationLast, const T& value);
///
namespace Internal
{
template <typename ForwardIterator, typename T>
inline void uninitializedFill_impl(ForwardIterator first, ForwardIterator last, const T& value, true_type)
{
eastl::fill(first, last, value);
}
template <typename ForwardIterator, typename T>
void uninitializedFill_impl(ForwardIterator first, ForwardIterator last, const T& value, false_type)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);