-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmemory
2441 lines (1999 loc) · 86.2 KB
/
memory
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
// memory standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _MEMORY_
#define _MEMORY_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <exception>
#include <iosfwd>
#include <type_traits>
#include <typeinfo>
#include <xmemory>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
// FUNCTION TEMPLATE uninitialized_copy_n
#if _HAS_IF_CONSTEXPR
template <class _InIt, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _NoThrowFwdIt _Dest) {
// copy [_First, _First + _Count) to [_Dest, ...)
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_copy_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};
for (; 0 < _Count; --_Count, (void) ++_UFirst) {
_Backout._Emplace_back(*_UFirst);
}
_UDest = _Backout._Release();
}
_Seek_wrapped(_Dest, _UDest);
}
return _Dest;
}
#else // ^^^ _HAS_IF_CONSTEXPR / !_HAS_IF_CONSTEXPR vvv
template <class _InIt, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt _Uninitialized_copy_n_unchecked2(_InIt _First, _Diff _Count, const _NoThrowFwdIt _Dest,
false_type) { // copy [_First, _First + _Count) to [_Dest, ...), no special optimization
_Uninitialized_backout<_NoThrowFwdIt> _Backout{_Dest};
for (; 0 < _Count; --_Count, (void) ++_First) {
_Backout._Emplace_back(*_First);
}
return _Backout._Release();
}
template <class _InIt, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt _Uninitialized_copy_n_unchecked2(const _InIt _First, const _Diff _Count, const _NoThrowFwdIt _Dest,
true_type) { // copy [_First, _First + _Count) to [_Dest, ...), memmove optimization
return _Copy_memmove(_First, _First + _Count, _Dest);
}
template <class _InIt, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _NoThrowFwdIt _Dest) {
// copy [_First, _First + _Count) to [_Dest, ...)]
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
_Seek_wrapped(_Dest, _Uninitialized_copy_n_unchecked2(_UFirst, _Count, _UDest,
bool_constant<_Ptr_copy_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial>{}));
}
return _Dest;
}
#endif // _HAS_IF_CONSTEXPR
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template <class _InTy, size_t _InSize, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt uninitialized_copy_n(_InTy (&_First)[_InSize], const _Diff _Count_raw, _NoThrowFwdIt _Dest) {
// copy [_First, _First + _Count) to [_Dest, ...), array input
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_First, _Count);
return _STD uninitialized_copy_n(static_cast<_InTy*>(_First), _Count, _Dest);
}
return _Dest;
}
template <class _InIt, class _Diff, class _OutTy, size_t _OutSize>
_OutTy* uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _OutTy (&_Dest)[_OutSize]) {
// copy [_First, _First + _Count) to [_Dest, ...), array dest
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_Dest, _Count);
return _STD uninitialized_copy_n(_Get_unwrapped_n(_First, _Count), _Count, static_cast<_OutTy*>(_Dest));
}
return _Dest;
}
template <class _InTy, size_t _InSize, class _Diff, class _OutTy, size_t _OutSize>
_OutTy* uninitialized_copy_n(_InTy (&_First)[_InSize], const _Diff _Count_raw, _OutTy (&_Dest)[_OutSize]) {
// copy [_First, _First + _Count) to [_Dest, ...), array input/dest
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_First, _Count);
_STL_VERIFY_ARRAY_SIZE(_Dest, _Count);
return _STD uninitialized_copy_n(static_cast<_InTy*>(_First), _Count, static_cast<_OutTy*>(_Dest));
}
return _Dest;
}
#endif // _ITERATOR_DEBUG_ARRAY_OVERLOADS
#if _HAS_CXX17
// FUNCTION TEMPLATE uninitialized_move
template <class _InIt, class _NoThrowFwdIt>
_NoThrowFwdIt uninitialized_move(const _InIt _First, const _InIt _Last, _NoThrowFwdIt _Dest) {
// move [_First, _Last) to raw [_Dest, ...)
_Adl_verify_range(_First, _Last);
const auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
const auto _UDest = _Get_unwrapped_n(_Dest, _Idl_distance<_InIt>(_UFirst, _ULast));
_Seek_wrapped(_Dest, _Uninitialized_move_unchecked(_UFirst, _ULast, _UDest));
return _Dest;
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template <class _InIt, class _OutTy, size_t _OutSize>
_OutTy* uninitialized_move(const _InIt _First, const _InIt _Last, _OutTy (&_Dest)[_OutSize]) {
// move [_First, _Last) to raw [_Dest, ...)
return _STD uninitialized_move(_First, _Last, _Array_iterator<_OutTy, _OutSize>(_Dest))._Unwrapped();
}
#endif // _ITERATOR_DEBUG_ARRAY_OVERLOADS
// FUNCTION TEMPLATE uninitialized_move_n
#if _HAS_IF_CONSTEXPR
template <class _InIt, class _Diff, class _NoThrowFwdIt>
pair<_InIt, _NoThrowFwdIt> uninitialized_move_n(_InIt _First, const _Diff _Count_raw, _NoThrowFwdIt _Dest) {
// move [_First, _First + _Count) to [_Dest, ...)
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_move_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
_UFirst += _Count;
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};
for (; 0 < _Count; --_Count, (void) ++_UFirst) {
_Backout._Emplace_back(_STD move(*_UFirst));
}
_UDest = _Backout._Release();
}
_Seek_wrapped(_Dest, _UDest);
_Seek_wrapped(_First, _UFirst);
}
return {_First, _Dest};
}
#else // ^^^ _HAS_IF_CONSTEXPR / !_HAS_IF_CONSTEXPR vvv
template <class _InIt, class _Diff, class _NoThrowFwdIt>
pair<_InIt, _NoThrowFwdIt> _Uninitialized_move_n_unchecked1(_InIt _First, _Diff _Count, const _NoThrowFwdIt _Dest,
false_type) { // move [_First, _First + _Count) to [_Dest, ...), no special optimization
_Uninitialized_backout<_NoThrowFwdIt> _Backout{_Dest};
for (; 0 < _Count; --_Count, (void) ++_First) {
_Backout._Emplace_back(_STD move(*_First));
}
return pair<_InIt, _NoThrowFwdIt>(_First, _Backout._Release());
}
template <class _InIt, class _Diff, class _NoThrowFwdIt>
pair<_InIt, _NoThrowFwdIt> _Uninitialized_move_n_unchecked1(_InIt _First, _Diff _Count, _NoThrowFwdIt _Dest,
true_type) { // move [_First, _First + _Count) to [_Dest, ...), memmove optimization
if (0 < _Count) {
_Dest = _Copy_memmove(_First, _First + _Count, _Dest);
_First += _Count;
}
return pair<_InIt, _NoThrowFwdIt>(_First, _Dest);
}
template <class _InIt, class _Diff, class _NoThrowFwdIt>
pair<_InIt, _NoThrowFwdIt> uninitialized_move_n(_InIt _First, const _Diff _Count_raw, _NoThrowFwdIt _Dest) {
// move [_First, _First + _Count) to [_Dest, ...)
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
const auto _Result = _Uninitialized_move_n_unchecked1(_UFirst, _Count, _UDest,
bool_constant<_Ptr_move_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial>{});
_Seek_wrapped(_Dest, _Result.second);
_Seek_wrapped(_First, _Result.first);
}
return {_First, _Dest};
}
#endif // _HAS_IF_CONSTEXPR
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template <class _InTy, size_t _InSize, class _Diff, class _NoThrowFwdIt>
pair<_InTy*, _NoThrowFwdIt> uninitialized_move_n(_InTy (&_First)[_InSize], const _Diff _Count_raw,
_NoThrowFwdIt _Dest) { // move [_First, _First + _Count) to [_Dest, ...), array input
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_First, _Count);
return _STD uninitialized_move_n(static_cast<_InTy*>(_First), _Count, _Dest);
}
return {_First, _Dest};
}
template <class _InIt, class _Diff, class _OutTy, size_t _OutSize>
pair<_InIt, _OutTy*> uninitialized_move_n(_InIt _First, const _Diff _Count_raw, _OutTy (&_Dest)[_OutSize]) {
// move [_First, _First + _Count) to [_Dest, ...), array dest
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_Dest, _Count);
return _STD uninitialized_move_n(_First, _Count, static_cast<_OutTy*>(_Dest));
}
return {_First, _Dest};
}
template <class _InTy, size_t _InSize, class _Diff, class _OutTy, size_t _OutSize>
pair<_InTy*, _OutTy*> uninitialized_move_n(_InTy (&_First)[_InSize], const _Diff _Count_raw,
_OutTy (&_Dest)[_OutSize]) { // move [_First, _First + _Count) to [_Dest, ...), array input/dest
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_STL_VERIFY_ARRAY_SIZE(_First, _Count);
_STL_VERIFY_ARRAY_SIZE(_Dest, _Count);
return _STD uninitialized_move_n(static_cast<_InTy*>(_First), _Count, static_cast<_OutTy*>(_Dest));
}
return {_First, _Dest};
}
#endif // _ITERATOR_DEBUG_ARRAY_OVERLOADS
#endif // _HAS_CXX17
// FUNCTION TEMPLATE uninitialized_fill_n
#if _HAS_IF_CONSTEXPR
template <class _NoThrowFwdIt, class _Diff, class _Tval>
_NoThrowFwdIt uninitialized_fill_n(_NoThrowFwdIt _First, const _Diff _Count_raw, const _Tval& _Val) {
// copy _Count copies of _Val to raw _First
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
if constexpr (_Fill_memset_is_safe<_Unwrapped_n_t<_NoThrowFwdIt>, _Tval>) {
_CSTD memset(_UFirst, static_cast<unsigned char>(_Val), _Count);
_UFirst += _Count;
} else {
_Uninitialized_backout<_Unwrapped_n_t<_NoThrowFwdIt>> _Backout{_UFirst};
for (; 0 < _Count; --_Count) {
_Backout._Emplace_back(_Val);
}
_UFirst = _Backout._Release();
}
_Seek_wrapped(_First, _UFirst);
}
return _First;
}
#else // ^^^ _HAS_IF_CONSTEXPR // !_HAS_IF_CONSTEXPR vvv
template <class _NoThrowFwdIt, class _Diff, class _Tval>
_NoThrowFwdIt _Uninitialized_fill_n_unchecked1(
const _NoThrowFwdIt _First, _Diff _Count, const _Tval& _Val, false_type) {
// copy _Count copies of _Val to raw _First, no special optimization
_Uninitialized_backout<_NoThrowFwdIt> _Backout{_First};
for (; 0 < _Count; --_Count) {
_Backout._Emplace_back(_Val);
}
return _Backout._Release();
}
template <class _NoThrowFwdIt, class _Diff, class _Tval>
_NoThrowFwdIt _Uninitialized_fill_n_unchecked1(
const _NoThrowFwdIt _First, const _Diff _Count, const _Tval& _Val, true_type) {
// copy _Count copies of _Val to raw _First, memset optimization
_CSTD memset(_First, static_cast<unsigned char>(_Val), _Count);
return _First + _Count;
}
template <class _NoThrowFwdIt, class _Diff, class _Tval>
_NoThrowFwdIt uninitialized_fill_n(_NoThrowFwdIt _First, const _Diff _Count_raw, const _Tval& _Val) {
// copy _Count copies of _Val to raw _First
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
const auto _UFirst = _Get_unwrapped_n(_First, _Count);
_Seek_wrapped(_First, _Uninitialized_fill_n_unchecked1(_UFirst, _Count, _Val,
bool_constant<_Fill_memset_is_safe<_Unwrapped_t<_NoThrowFwdIt>, _Tval>>{}));
}
return _First;
}
#endif // _HAS_IF_CONSTEXPR
#if _HAS_CXX17
// FUNCTION TEMPLATE destroy_at
template <class _Ty>
void destroy_at(_Ty* const _Location) { // destroy _Ty at memory address _Location
_Location->~_Ty();
}
// FUNCTION TEMPLATE destroy
template <class _NoThrowFwdIt>
void destroy(const _NoThrowFwdIt _First, const _NoThrowFwdIt _Last) { // destroy all elements in [_First, _Last)
_Adl_verify_range(_First, _Last);
_Destroy_range(_Get_unwrapped(_First), _Get_unwrapped(_Last));
}
// FUNCTION TEMPLATE destroy_n
template <class _NoThrowFwdIt, class _Diff>
_NoThrowFwdIt destroy_n(_NoThrowFwdIt _First, const _Diff _Count_raw) {
// destroy all elements in [_First, _First + _Count)
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
auto _UFirst = _Get_unwrapped_n(_First, _Count);
if constexpr (is_trivially_destructible_v<_Iter_value_t<_NoThrowFwdIt>>) {
_STD advance(_UFirst, _Count);
} else {
do {
_Destroy_in_place(*_UFirst);
++_UFirst;
--_Count;
} while (0 < _Count);
}
_Seek_wrapped(_First, _UFirst);
}
return _First;
}
// FUNCTION TEMPLATE uninitialized_default_construct
template <class _NoThrowFwdIt>
void uninitialized_default_construct(const _NoThrowFwdIt _First, const _NoThrowFwdIt _Last) {
// default-initialize all elements in [_First, _Last)
using _Ty = _Iter_value_t<_NoThrowFwdIt>;
_Adl_verify_range(_First, _Last);
if constexpr (!is_trivially_default_constructible_v<_Ty>) {
const auto _ULast = _Get_unwrapped(_Last);
_Uninitialized_backout _Backout{_Get_unwrapped(_First)};
for (; _Backout._Last != _ULast; ++_Backout._Last) {
::new (static_cast<void*>(_Unfancy(_Backout._Last))) _Ty;
}
_Backout._Release();
}
}
// FUNCTION TEMPLATE uninitialized_default_construct_n
template <class _NoThrowFwdIt, class _Diff>
_NoThrowFwdIt uninitialized_default_construct_n(_NoThrowFwdIt _First, const _Diff _Count_raw) {
// default-initialize all elements in [_First, _First + _Count_raw)
using _Ty = _Iter_value_t<_NoThrowFwdIt>;
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
if constexpr (is_trivially_default_constructible_v<_Ty>) {
_STD advance(_First, _Count);
} else {
_Uninitialized_backout _Backout{_Get_unwrapped_n(_First, _Count)};
for (; 0 < _Count; ++_Backout._Last, (void) --_Count) {
::new (static_cast<void*>(_Unfancy(_Backout._Last))) _Ty;
}
_Seek_wrapped(_First, _Backout._Release());
}
}
return _First;
}
// FUNCTION TEMPLATE uninitialized_value_construct
template <class _NoThrowFwdIt>
void uninitialized_value_construct(const _NoThrowFwdIt _First, const _NoThrowFwdIt _Last) {
// value-initialize all elements in [_First, _Last)
_Adl_verify_range(_First, _Last);
const auto _UFirst = _Get_unwrapped(_First);
const auto _ULast = _Get_unwrapped(_Last);
if constexpr (_Use_memset_value_construct_v<_Unwrapped_t<_NoThrowFwdIt>>) {
_Zero_range(_UFirst, _ULast);
} else {
_Uninitialized_backout _Backout{_UFirst};
while (_Backout._Last != _ULast) {
_Backout._Emplace_back();
}
_Backout._Release();
}
}
// FUNCTION TEMPLATE uninitialized_value_construct_n
template <class _NoThrowFwdIt, class _Diff>
_NoThrowFwdIt uninitialized_value_construct_n(_NoThrowFwdIt _First, const _Diff _Count_raw) {
// value-initialize all elements in [_First, _First + _Count_raw)
_Algorithm_int_t<_Diff> _Count = _Count_raw;
if (0 < _Count) {
_Seek_wrapped(_First, _Uninitialized_value_construct_n_unchecked1(_Get_unwrapped_n(_First, _Count), _Count));
}
return _First;
}
#endif // _HAS_CXX17
// CLASS TEMPLATE raw_storage_iterator
template <class _OutIt, class _Ty>
class _CXX17_DEPRECATE_RAW_STORAGE_ITERATOR raw_storage_iterator { // wrap stores to raw buffer as output iterator
public:
using iterator_category = output_iterator_tag;
using value_type = void;
using difference_type = void;
using pointer = void;
using reference = void;
explicit raw_storage_iterator(_OutIt _First) : _Next(_First) {}
_NODISCARD raw_storage_iterator& operator*() { // pretend to return designated value
return *this;
}
raw_storage_iterator& operator=(const _Ty& _Val) { // construct value designated by stored iterator
_Construct_in_place(*_Next, _Val);
return *this;
}
raw_storage_iterator& operator=(_Ty&& _Val) { // construct value designated by stored iterator
_Construct_in_place(*_Next, _STD move(_Val));
return *this;
}
raw_storage_iterator& operator++() {
++_Next;
return *this;
}
raw_storage_iterator operator++(int) {
raw_storage_iterator _Ans = *this;
++_Next;
return _Ans;
}
_NODISCARD _OutIt base() const {
return _Next;
}
private:
_OutIt _Next;
};
#if _HAS_AUTO_PTR_ETC
// CLASS TEMPLATE auto_ptr
template <class _Ty>
class auto_ptr;
template <class _Ty>
struct auto_ptr_ref { // proxy reference for auto_ptr copying
explicit auto_ptr_ref(_Ty* _Right) : _Ref(_Right) {}
_Ty* _Ref; // generic pointer to auto_ptr ptr
};
template <class _Ty>
class auto_ptr { // wrap an object pointer to ensure destruction
public:
using element_type = _Ty;
explicit auto_ptr(_Ty* _Ptr = nullptr) noexcept : _Myptr(_Ptr) {}
auto_ptr(auto_ptr& _Right) noexcept : _Myptr(_Right.release()) {}
auto_ptr(auto_ptr_ref<_Ty> _Right) noexcept {
_Ty* _Ptr = _Right._Ref;
_Right._Ref = nullptr; // release old
_Myptr = _Ptr; // reset this
}
template <class _Other>
operator auto_ptr<_Other>() noexcept { // convert to compatible auto_ptr
return auto_ptr<_Other>(*this);
}
template <class _Other>
operator auto_ptr_ref<_Other>() noexcept { // convert to compatible auto_ptr_ref
_Other* _Cvtptr = _Myptr; // test implicit conversion
auto_ptr_ref<_Other> _Ans(_Cvtptr);
_Myptr = nullptr; // pass ownership to auto_ptr_ref
return _Ans;
}
template <class _Other>
auto_ptr& operator=(auto_ptr<_Other>& _Right) noexcept {
reset(_Right.release());
return *this;
}
template <class _Other>
auto_ptr(auto_ptr<_Other>& _Right) noexcept : _Myptr(_Right.release()) {}
auto_ptr& operator=(auto_ptr& _Right) noexcept {
reset(_Right.release());
return *this;
}
auto_ptr& operator=(auto_ptr_ref<_Ty> _Right) noexcept {
_Ty* _Ptr = _Right._Ref;
_Right._Ref = 0; // release old
reset(_Ptr); // set new
return *this;
}
~auto_ptr() noexcept {
delete _Myptr;
}
_NODISCARD _Ty& operator*() const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(_Myptr, "auto_ptr not dereferenceable");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return *get();
}
_NODISCARD _Ty* operator->() const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(_Myptr, "auto_ptr not dereferenceable");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return get();
}
_NODISCARD _Ty* get() const noexcept {
return _Myptr;
}
_Ty* release() noexcept {
_Ty* _Tmp = _Myptr;
_Myptr = nullptr;
return _Tmp;
}
void reset(_Ty* _Ptr = nullptr) noexcept { // destroy designated object and store new pointer
if (_Ptr != _Myptr) {
delete _Myptr;
}
_Myptr = _Ptr;
}
private:
_Ty* _Myptr; // the wrapped object pointer
};
template <>
class auto_ptr<void> {
public:
using element_type = void;
};
#endif // _HAS_AUTO_PTR_ETC
// CLASS bad_weak_ptr
class bad_weak_ptr : public exception { // exception type for invalid use of expired weak_ptr object
public:
bad_weak_ptr() noexcept {}
_NODISCARD virtual const char* __CLR_OR_THIS_CALL what() const noexcept override {
// return pointer to message string
return "bad_weak_ptr";
}
};
[[noreturn]] inline void _Throw_bad_weak_ptr() {
_THROW(bad_weak_ptr{});
}
// CLASS _Ref_count_base
class __declspec(novtable) _Ref_count_base { // common code for reference counting
private:
#ifdef _M_CEE_PURE
// permanent workaround to avoid mentioning _purecall in msvcurt.lib, ptrustu.lib, or other support libs
virtual void _Destroy() noexcept {
_STD terminate();
}
virtual void _Delete_this() noexcept {
_STD terminate();
}
#else // ^^^ _M_CEE_PURE / !_M_CEE_PURE vvv
virtual void _Destroy() noexcept = 0; // destroy managed resource
virtual void _Delete_this() noexcept = 0; // destroy self
#endif // _M_CEE_PURE
_Atomic_counter_t _Uses = 1;
_Atomic_counter_t _Weaks = 1;
protected:
constexpr _Ref_count_base() noexcept = default; // non-atomic initializations
public:
_Ref_count_base(const _Ref_count_base&) = delete;
_Ref_count_base& operator=(const _Ref_count_base&) = delete;
virtual ~_Ref_count_base() noexcept {} // TRANSITION, should be non-virtual
bool _Incref_nz() noexcept { // increment use count if not zero, return true if successful
auto& _Volatile_uses = reinterpret_cast<volatile long&>(_Uses);
long _Count = _ISO_VOLATILE_LOAD32(_Volatile_uses);
while (_Count != 0) {
const long _Old_value = _INTRIN_RELAXED(_InterlockedCompareExchange)(&_Volatile_uses, _Count + 1, _Count);
if (_Old_value == _Count) {
return true;
}
_Count = _Old_value;
}
return false;
}
void _Incref() noexcept { // increment use count
_MT_INCR(_Uses);
}
void _Incwref() noexcept { // increment weak reference count
_MT_INCR(_Weaks);
}
void _Decref() noexcept { // decrement use count
if (_MT_DECR(_Uses) == 0) {
_Destroy();
_Decwref();
}
}
void _Decwref() noexcept { // decrement weak reference count
if (_MT_DECR(_Weaks) == 0) {
_Delete_this();
}
}
long _Use_count() const noexcept {
return static_cast<long>(_Uses);
}
virtual void* _Get_deleter(const type_info&) const noexcept {
return nullptr;
}
};
// CLASS TEMPLATE _Ref_count
template <class _Ty>
class _Ref_count : public _Ref_count_base { // handle reference counting for pointer without deleter
public:
explicit _Ref_count(_Ty* _Px) : _Ref_count_base(), _Ptr(_Px) {}
private:
virtual void _Destroy() noexcept override { // destroy managed resource
delete _Ptr;
}
virtual void _Delete_this() noexcept override { // destroy self
delete this;
}
_Ty* _Ptr;
};
// CLASS TEMPLATE _Ref_count_resource
template <class _Resource, class _Dx>
class _Ref_count_resource : public _Ref_count_base { // handle reference counting for object with deleter
public:
_Ref_count_resource(_Resource _Px, _Dx _Dt)
: _Ref_count_base(), _Mypair(_One_then_variadic_args_t(), _STD move(_Dt), _Px) {}
virtual void* _Get_deleter(const type_info& _Typeid) const noexcept override {
#if _HAS_STATIC_RTTI
if (_Typeid == typeid(_Dx)) {
return const_cast<_Dx*>(_STD addressof(_Mypair._Get_first()));
}
#else // _HAS_STATIC_RTTI
(void) _Typeid;
#endif // _HAS_STATIC_RTTI
return nullptr;
}
private:
virtual void _Destroy() noexcept override { // destroy managed resource
_Mypair._Get_first()(_Mypair._Myval2);
}
virtual void _Delete_this() noexcept override { // destroy self
delete this;
}
_Compressed_pair<_Dx, _Resource> _Mypair;
};
// CLASS TEMPLATE _Ref_count_resource_alloc
template <class _Resource, class _Dx, class _Alloc>
class _Ref_count_resource_alloc : public _Ref_count_base {
// handle reference counting for object with deleter and allocator
public:
_Ref_count_resource_alloc(_Resource _Px, _Dx _Dt, const _Alloc& _Ax)
: _Ref_count_base(),
_Mypair(_One_then_variadic_args_t(), _STD move(_Dt), _One_then_variadic_args_t(), _Ax, _Px) {}
virtual void* _Get_deleter(const type_info& _Typeid) const noexcept override {
#if _HAS_STATIC_RTTI
if (_Typeid == typeid(_Dx)) {
return const_cast<_Dx*>(_STD addressof(_Mypair._Get_first()));
}
#else // _HAS_STATIC_RTTI
(void) _Typeid;
#endif // _HAS_STATIC_RTTI
return nullptr;
}
private:
using _Myalty = _Rebind_alloc_t<_Alloc, _Ref_count_resource_alloc>;
virtual void _Destroy() noexcept override { // destroy managed resource
_Mypair._Get_first()(_Mypair._Myval2._Myval2);
}
virtual void _Delete_this() noexcept override { // destroy self
_Myalty _Al = _Mypair._Myval2._Get_first();
this->~_Ref_count_resource_alloc();
_Deallocate_plain(_Al, this);
}
_Compressed_pair<_Dx, _Compressed_pair<_Myalty, _Resource>> _Mypair;
};
// DECLARATIONS
template <class _Ty>
struct default_delete;
template <class _Ty, class _Dx = default_delete<_Ty>>
class unique_ptr;
template <class _Ty>
class shared_ptr;
template <class _Ty>
class weak_ptr;
template <class _Yty, class = void>
struct _Can_enable_shared : false_type {}; // detect unambiguous and accessible inheritance from enable_shared_from_this
template <class _Yty>
struct _Can_enable_shared<_Yty, void_t<typename _Yty::_Esft_type>>
: is_convertible<remove_cv_t<_Yty>*, typename _Yty::_Esft_type*>::type {
// is_convertible is necessary to verify unambiguous inheritance
};
#if !_HAS_IF_CONSTEXPR
template <class _Other, class _Yty>
void _Enable_shared_from_this1(const shared_ptr<_Other>& _This, _Yty* _Ptr, true_type) noexcept {
// enable shared_from_this
if (_Ptr && _Ptr->_Wptr.expired()) {
_Ptr->_Wptr = shared_ptr<remove_cv_t<_Yty>>(_This, const_cast<remove_cv_t<_Yty>*>(_Ptr));
}
}
template <class _Other, class _Yty>
void _Enable_shared_from_this1(const shared_ptr<_Other>&, _Yty*, false_type) noexcept {
// don't enable shared_from_this
}
#endif // !_HAS_IF_CONSTEXPR
// CLASS TEMPLATE _Ptr_base
struct _Exception_ptr_access;
template <class _Ty>
class _Ptr_base { // base class for shared_ptr and weak_ptr
public:
using element_type = remove_extent_t<_Ty>;
_NODISCARD long use_count() const noexcept {
return _Rep ? _Rep->_Use_count() : 0;
}
template <class _Ty2>
_NODISCARD bool owner_before(const _Ptr_base<_Ty2>& _Right) const noexcept { // compare addresses of manager objects
return _Rep < _Right._Rep;
}
_Ptr_base(const _Ptr_base&) = delete;
_Ptr_base& operator=(const _Ptr_base&) = delete;
protected:
_NODISCARD element_type* get() const noexcept {
return _Ptr;
}
constexpr _Ptr_base() noexcept = default;
~_Ptr_base() = default;
template <class _Ty2>
void _Move_construct_from(_Ptr_base<_Ty2>&& _Right) noexcept {
// implement shared_ptr's (converting) move ctor and weak_ptr's move ctor
_Ptr = _Right._Ptr;
_Rep = _Right._Rep;
_Right._Ptr = nullptr;
_Right._Rep = nullptr;
}
template <class _Ty2>
void _Copy_construct_from(const shared_ptr<_Ty2>& _Other) noexcept {
// implement shared_ptr's (converting) copy ctor
if (_Other._Rep) {
_Other._Rep->_Incref();
}
_Ptr = _Other._Ptr;
_Rep = _Other._Rep;
}
template <class _Ty2>
void _Alias_construct_from(const shared_ptr<_Ty2>& _Other, element_type* _Px) noexcept {
// implement shared_ptr's aliasing ctor
if (_Other._Rep) {
_Other._Rep->_Incref();
}
_Ptr = _Px;
_Rep = _Other._Rep;
}
template <class _Ty2>
void _Alias_move_construct_from(shared_ptr<_Ty2>&& _Other, element_type* _Px) noexcept {
// implement shared_ptr's aliasing move ctor
_Ptr = _Px;
_Rep = _Other._Rep;
_Other._Ptr = nullptr;
_Other._Rep = nullptr;
}
template <class _Ty0>
friend class weak_ptr; // specifically, weak_ptr::lock()
template <class _Ty2>
bool _Construct_from_weak(const weak_ptr<_Ty2>& _Other) noexcept {
// implement shared_ptr's ctor from weak_ptr, and weak_ptr::lock()
if (_Other._Rep && _Other._Rep->_Incref_nz()) {
_Ptr = _Other._Ptr;
_Rep = _Other._Rep;
return true;
}
return false;
}
void _Decref() noexcept { // decrement reference count
if (_Rep) {
_Rep->_Decref();
}
}
void _Swap(_Ptr_base& _Right) noexcept { // swap pointers
_STD swap(_Ptr, _Right._Ptr);
_STD swap(_Rep, _Right._Rep);
}
template <class _Ty2>
void _Weakly_construct_from(const _Ptr_base<_Ty2>& _Other) noexcept { // implement weak_ptr's ctors
if (_Other._Rep) {
_Ptr = _Other._Ptr;
_Rep = _Other._Rep;
_Rep->_Incwref();
} else {
_STL_INTERNAL_CHECK(!_Ptr && !_Rep);
}
}
void _Decwref() noexcept { // decrement weak reference count
if (_Rep) {
_Rep->_Decwref();
}
}
private:
element_type* _Ptr{nullptr};
_Ref_count_base* _Rep{nullptr};
template <class _Ty0>
friend class _Ptr_base;
friend shared_ptr<_Ty>;
friend _Exception_ptr_access;
#if _HAS_STATIC_RTTI
template <class _Dx, class _Ty0>
friend _Dx* get_deleter(const shared_ptr<_Ty0>& _Sx) noexcept;
#endif // _HAS_STATIC_RTTI
};
// TYPE TRAIT _Can_scalar_delete
template <class _Yty, class = void>
struct _Can_scalar_delete : false_type {};
template <class _Yty>
struct _Can_scalar_delete<_Yty, void_t<decltype(delete _STD declval<_Yty*>())>> : true_type {};
// TYPE TRAIT _Can_array_delete
template <class _Yty, class = void>
struct _Can_array_delete : false_type {};
template <class _Yty>
struct _Can_array_delete<_Yty, void_t<decltype(delete[] _STD declval<_Yty*>())>> : true_type {};
// TYPE TRAIT _Can_call_function_object
template <class _Fx, class _Arg, class = void>
struct _Can_call_function_object : false_type {};
template <class _Fx, class _Arg>
struct _Can_call_function_object<_Fx, _Arg, void_t<decltype(_STD declval<_Fx>()(_STD declval<_Arg>()))>> : true_type {};
// TYPE TRAIT _SP_convertible
template <class _Yty, class _Ty>
struct _SP_convertible : is_convertible<_Yty*, _Ty*>::type {};
template <class _Yty, class _Uty>
struct _SP_convertible<_Yty, _Uty[]> : is_convertible<_Yty (*)[], _Uty (*)[]>::type {};
template <class _Yty, class _Uty, size_t _Ext>
struct _SP_convertible<_Yty, _Uty[_Ext]> : is_convertible<_Yty (*)[_Ext], _Uty (*)[_Ext]>::type {};
// TYPE TRAIT _SP_pointer_compatible
template <class _Yty, class _Ty>
struct _SP_pointer_compatible : is_convertible<_Yty*, _Ty*>::type {
// N4659 [util.smartptr.shared]/5 "a pointer type Y* is said to be compatible
// with a pointer type T* " "when either Y* is convertible to T* ..."
};
template <class _Uty, size_t _Ext>
struct _SP_pointer_compatible<_Uty[_Ext], _Uty[]> : true_type {
// N4659 [util.smartptr.shared]/5 "... or Y is U[N] and T is cv U[]."
};
template <class _Uty, size_t _Ext>
struct _SP_pointer_compatible<_Uty[_Ext], const _Uty[]> : true_type {
// N4659 [util.smartptr.shared]/5 "... or Y is U[N] and T is cv U[]."
};
template <class _Uty, size_t _Ext>
struct _SP_pointer_compatible<_Uty[_Ext], volatile _Uty[]> : true_type {
// N4659 [util.smartptr.shared]/5 "... or Y is U[N] and T is cv U[]."
};
template <class _Uty, size_t _Ext>
struct _SP_pointer_compatible<_Uty[_Ext], const volatile _Uty[]> : true_type {
// N4659 [util.smartptr.shared]/5 "... or Y is U[N] and T is cv U[]."
};
// CLASS TEMPLATE shared_ptr
template <class _Ux>
struct _Temporary_owner {
_Ux* _Ptr;
explicit _Temporary_owner(_Ux* const _Ptr_) noexcept : _Ptr(_Ptr_) {}
_Temporary_owner(const _Temporary_owner&) = delete;
_Temporary_owner& operator=(const _Temporary_owner&) = delete;
~_Temporary_owner() {
delete _Ptr;
}
};
template <class _UxptrOrNullptr, class _Dx>
struct _Temporary_owner_del {
_UxptrOrNullptr _Ptr;
_Dx& _Dt;
bool _Call_deleter = true;
explicit _Temporary_owner_del(const _UxptrOrNullptr _Ptr_, _Dx& _Dt_) noexcept : _Ptr(_Ptr_), _Dt(_Dt_) {}
_Temporary_owner_del(const _Temporary_owner_del&) = delete;
_Temporary_owner_del& operator=(const _Temporary_owner_del&) = delete;
~_Temporary_owner_del() {
if (_Call_deleter) {
_Dt(_Ptr);
}
}
};
template <class _Ty>