forked from questor/eastl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
1556 lines (1334 loc) · 54.3 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:
// getTemporaryBuffer
// 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
// uninitializedMove
// uninitializedMove_if_noexcept - Extention to standard functionality.
// uninitializedMove_n
// uninitializedFill
// uninitializedFillN
// uninitialized_relocate - Extention to standard functionality.
// uninitializedCopyPtr - Extention to standard functionality.
// uninitializedMove_ptr - Extention to standard functionality.
// uninitializedMove_ptr_if_noexcept- Extention to standard functionality.
// uninitializedFillPtr - Extention to standard functionality.
// uninitializedFillNPtr - Extention to standard functionality.
// uninitializedCopyFill - Extention to standard functionality.
// uninitializedFillCopy - Extention to standard functionality.
// uninitializedCopyCopy - Extention to standard functionality.
//
// In-place destructor helpers:
// destruct(T*)
// destruct(first, last)
//
// Alignment
// align
// align_advance - Extention to standard functionality.
//
// Allocator-related
// uses_allocator
// allocator_arg_t
// allocator_arg
//
// Pointers
// pointer_traits
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_MEMORY_H
#define EASTL_MEMORY_H
#include <eastl/internal/config.h>
#include <eastl/internal/generic_iterator.h>
#include <eastl/internal/pair_fwd_decls.h>
#include <eastl/internal/functional_base.h>
#include <eastl/internal/allocator_traits_fwd_decls.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>
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <stdlib.h>
#include <new>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4530) // C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
#pragma warning(disable: 4571) // catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
#endif
EA_DISABLE_SN_WARNING(828); // The EDG SN compiler has a bug in its handling of variadic template arguments and mistakenly reports "parameter "args" was never referenced"
#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
/// addressof
///
/// From the C++11 Standard, section 20.6.12.1
/// Returns the actual address of the object or function referenced by r, even in the presence of an overloaded operator&.
///
template<typename T>
T* addressof(T& value) EASTL_NOEXCEPT
{
return reinterpret_cast<T*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(value)));
}
/// 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::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.
///
/// 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>
class late_constructed
{
public:
typedef late_constructed<T, autoConstruct> this_type;
typedef T value_type;
typedef typename eastl::aligned_storage<sizeof(value_type),
eastl::alignment_of<value_type>::value>::type storage_type;
late_constructed() EASTL_NOEXCEPT // In the case of the late_constructed instance being at global scope, we rely on the
: mpValue(NULL) {} // compiler executing this constructor or placing the instance in auto-zeroed-at-startup memory.
~late_constructed()
{
if(mpValue)
(*mpValue).~value_type();
}
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED
template <typename... Args>
void construct(Args&&... args)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(eastl::forward<Args>(args)...);
}
#else
void construct()
{
if(!mpValue)
mpValue = new(&mStorage) value_type;
}
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename A1>
void construct(A1&& a1)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(eastl::forward<A1>(a1));
}
template <typename A1, typename A2>
void construct(A1&& a1, A2&& a2)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(eastl::forward<A1>(a1), eastl::forward<A2>(a2));
}
template <typename A1, typename A2, typename A3>
void construct(A1&& a1, A2&& a2, A3&& a3)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(eastl::forward<A1>(a1), eastl::forward<A2>(a2), eastl::forward<A3>(a3));
}
#endif
template <typename A1>
void construct(const A1& a1)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(a1);
}
template <typename A1, typename A2>
void construct(const A1& a1, const A2& a2)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(a1, a2);
}
template <typename A1, typename A2, typename A3>
void construct(const A1& a1, const A2& a2, const A3& a3)
{
if(!mpValue)
mpValue = new(&mStorage) value_type(a1, a2, a3);
}
#endif
bool is_constructed() const EASTL_NOEXCEPT
{ return mpValue != NULL; }
void destruct()
{
if(mpValue)
{
(*mpValue).~value_type();
mpValue = NULL;
}
}
value_type& operator*() EASTL_NOEXCEPT
{
if(!mpValue)
construct();
EA_ANALYSIS_ASSUME(mpValue);
return *mpValue;
}
value_type* operator->() EASTL_NOEXCEPT
{
if(!mpValue)
construct();
return mpValue;
}
value_type* get() 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>
class late_constructed<T, false> : public late_constructed<T, true>
{
public:
typedef late_constructed<T, true> base_type;
typename base_type::value_type& operator*() 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; }
typename base_type::value_type* get() 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 raw_storage_iterator : public iterator<EASTL_ITC_NS::output_iterator_tag, void, void, void, void>
{
protected:
OutputIterator mIterator;
public:
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<OutputIterator, T>& operator++()
{
++mIterator;
return *this;
}
raw_storage_iterator<OutputIterator, T> operator++(int)
{
raw_storage_iterator<OutputIterator, T> 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
{
for(; first != last; ++first, ++dest)
::new((void*)&*dest) value_type(*first);
}
catch(...)
{
for(; origDest < dest; ++origDest)
(*origDest).~value_type();
throw;
}
#else
for(; first != last; ++first, ++dest)
::new((void*)&*dest) value_type(*first);
#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)
{
return (T*)memcpy(dest, first, (size_t)((uintptr_t)last - (uintptr_t)first)) + (last - first);
}
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);
}
};
}
/// 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>
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;
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;
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_start(first, last, dest);
}
template <typename ForwardIterator, typename ForwardIteratorDest>
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;
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;
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_commit(first, last, dest);
}
template <typename ForwardIterator, typename ForwardIteratorDest>
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;
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;
return Internal::uninitialized_relocate_impl<bHasTrivialMove, IC>::do_move_abort(first, last, dest);
}
/// uninitialized_relocate
///
/// See above for documentation.
///
template <typename ForwardIterator, typename ForwardIteratorDest>
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;
}
// uninitializedCopy
//
namespace Internal
{
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedCopy_impl(InputIterator first, InputIterator last, ForwardIterator dest, true_type)
{
return eastl::copy(first, last, dest); // The copy() in turn will use memcpy for POD types.
}
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedCopy_impl(InputIterator first, InputIterator last, ForwardIterator dest, false_type)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(dest);
#if EASTL_EXCEPTIONS_ENABLED
try
{
for(; first != last; ++first, ++currentDest)
::new((void*)&*currentDest) value_type(*first);
}
catch(...)
{
for(; dest < currentDest; ++dest)
(*dest).~value_type();
throw;
}
#else
for(; first != last; ++first, ++currentDest)
::new((void*)&*currentDest) value_type(*first);
#endif
return currentDest;
}
}
/// 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<ForwardIterator>::value_type value_type;
// We use is_trivial, which in the C++11 Standard means is_trivially_copyable and is_trivially_default_constructible.
return Internal::uninitializedCopy_impl(first, last, result, eastl::is_trivial<value_type>());
}
/// 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
{
for(; n > 0; --n, ++first, ++currentDest)
::new((void*)&*currentDest) value_type(*first);
}
catch(...)
{
for(; dest < currentDest; ++dest)
(*dest).~value_type();
throw;
}
#else
for(; n > 0; --n, ++first, ++currentDest)
::new((void*)&*currentDest) value_type(*first);
#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>
inline Result uninitializedCopyPtr(First first, Last last, Result result)
{
typedef typename eastl::iterator_traits<generic_iterator<Result, void> >::value_type value_type;
const generic_iterator<Result, void> i(Internal::uninitializedCopy_impl(eastl::generic_iterator<First, void>(first), // generic_iterator makes a pointer act like an iterator.
eastl::generic_iterator<Last, void>(last),
eastl::generic_iterator<Result, void>(result),
eastl::is_trivially_copy_assignable<value_type>()));
return i.base();
}
/// 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.
///
#if EASTL_MOVE_SEMANTICS_ENABLED
namespace Internal
{
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedMove_impl(InputIterator first, InputIterator last, ForwardIterator dest, true_type)
{
return eastl::copy(first, last, dest); // The copy() in turn will use memcpy for is_trivially_copy_assignable (e.g. POD) types.
}
template <typename InputIterator, typename ForwardIterator>
inline ForwardIterator uninitializedMove_impl(InputIterator first, InputIterator last, ForwardIterator dest, false_type)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(dest);
// We must run a loop over every element and move-construct it at the new location.
#if EASTL_EXCEPTIONS_ENABLED
try
{
for(; first != last; ++first, ++currentDest)
::new((void*)&*currentDest) value_type(eastl::move(*first)); // If value_type has a move constructor then it will be used here.
}
catch(...)
{
// We have a problem here: If an exception occurs while doing the loop below then we will
// have values that were moved from the source to the dest that may need to be moved back
// in the catch. What does the C++11 Standard say about this? And what happens if there's an
// exception while moving them back? We may want to trace through a conforming C++11 Standard
// Library to see what it does and do something similar. Given that rvalue references are
// objects that are going away, we may not need to move the values back, though that has the
// side effect of a certain kind of lost elements problem.
for(; dest < currentDest; ++dest)
(*dest).~value_type();
throw;
}
#else
for(; first != last; ++first, ++currentDest)
::new((void*)&*currentDest) value_type(eastl::move(*first)); // If value_type has a move constructor then it will be used here.
#endif
return currentDest;
}
}
template <typename First, typename Last, typename Result>
inline Result uninitializedMove_ptr(First first, Last last, Result dest)
{
typedef typename eastl::iterator_traits<generic_iterator<Result, void> >::value_type value_type;
const generic_iterator<Result, void> i(Internal::uninitializedMove_impl(eastl::generic_iterator<First, void>(first), // generic_iterator makes a pointer act like an iterator.
eastl::generic_iterator<Last, void>(last),
eastl::generic_iterator<Result, void>(dest),
eastl::is_trivially_copy_assignable<value_type>())); // is_trivially_copy_assignable identifies if copy assignment would be as valid as move assignment, which means we have the opportunity to memcpy/memmove optimization.
return i.base();
}
#else
template <typename First, typename Last, typename Result>
inline Result uninitializedMove_ptr(First first, Last last, Result dest)
{
return uninitializedCopyPtr(first, last, dest);
}
#endif
/// 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)
{
#if EASTL_MOVE_SEMANTICS_ENABLED
return eastl::uninitializedCopy(eastl::make_move_iterator(first), eastl::make_move_iterator(last), dest);
#else
return eastl::uninitializedCopy(first, last, dest);
#endif
}
/// 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_MOVE_SEMANTICS_ENABLED
return eastl::uninitializedCopy(eastl::make_move_if_noexcept_iterator(first), eastl::make_move_if_noexcept_iterator(last), dest);
#else
return eastl::uninitializedCopy(first, last, dest);
#endif
}
/// uninitializedMove_ptr_if_noexcept
///
template <typename First, typename Last, typename Result>
inline Result uninitializedMove_ptr_if_noexcept(First first, Last last, Result dest)
{
#if EASTL_EXCEPTIONS_ENABLED
return eastl::uninitializedMove_if_noexcept(first, last, dest);
#else
return eastl::uninitializedMove_ptr(first, last, dest);
#endif
}
/// 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)
{
#if EASTL_MOVE_SEMANTICS_ENABLED
return eastl::uninitializedCopy_n(eastl::make_move_iterator(first), n, dest);
#else
return eastl::uninitializedCopy_n(first, n, dest);
#endif
}
// 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
///
/// 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>
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
{
for (; currentDest != last; ++currentDest)
::new (static_cast<void*>(&*currentDest)) value_type();
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#else
for (; currentDest != last; ++currentDest)
::new (static_cast<void*>(&*currentDest)) value_type();
#endif
}
/// uninitialized_default_fillN
///
/// 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);
///
template <typename ForwardIterator, typename Count>
inline void uninitialized_default_fillN(ForwardIterator first, Count n)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
ForwardIterator currentDest(first);
#if EASTL_EXCEPTIONS_ENABLED
try
{
for (; n > 0; --n, ++currentDest)
::new (static_cast<void*>(&*currentDest)) value_type();
}
catch (...)
{
for (; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#else
for (; n > 0; --n, ++currentDest)
::new (static_cast<void*>(&*currentDest)) value_type();
#endif
}
EA_RESTORE_VC_WARNING()
/// 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);
#if EASTL_EXCEPTIONS_ENABLED
try
{
for(; currentDest != last; ++currentDest)
::new((void*)&*currentDest) value_type(value);
}
catch(...)
{
for(; first < currentDest; ++first)
(*first).~value_type();
throw;
}
#else
for(; currentDest != last; ++currentDest)
::new((void*)&*currentDest) value_type(value);
#endif
}
}
template <typename ForwardIterator, typename T>
inline void uninitializedFill(ForwardIterator first, ForwardIterator last, const T& value)
{
typedef typename eastl::iterator_traits<ForwardIterator>::value_type value_type;
Internal::uninitializedFill_impl(first, last, value, eastl::is_trivially_copy_assignable<value_type>());
}
/// uninitializedFillPtr
///
/// This is a specialization of uninitializedFill for iterators that are pointers.
/// It exists so that we can declare a value_type for the iterator, which you
/// can't do with a pointer by itself.
///
template <typename T>
inline void uninitializedFillPtr(T* first, T* last, const T& value)
{
typedef typename eastl::iterator_traits<eastl::generic_iterator<T*, void> >::value_type value_type;
Internal::uninitializedFill_impl(eastl::generic_iterator<T*, void>(first), eastl::generic_iterator<T*, void>(last), value, eastl::is_trivially_copy_assignable<value_type>());
}
/// uninitializedFillN