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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
|
/*
* Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef WTF_Vector_h
#define WTF_Vector_h
#include "wtf/Alignment.h"
#include "wtf/ConditionalDestructor.h"
#include "wtf/ContainerAnnotations.h"
#include "wtf/Noncopyable.h"
#include "wtf/NotFound.h"
#include "wtf/StdLibExtras.h"
#include "wtf/VectorTraits.h"
#include "wtf/allocator/PartitionAllocator.h"
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <string.h>
#include <utility>
// For ASAN builds, disable inline buffers completely as they cause various
// issues.
#ifdef ANNOTATE_CONTIGUOUS_CONTAINER
#define INLINE_CAPACITY 0
#else
#define INLINE_CAPACITY inlineCapacity
#endif
namespace WTF {
#if defined(MEMORY_SANITIZER_INITIAL_SIZE)
static const size_t kInitialVectorSize = 1;
#else
#ifndef WTF_VECTOR_INITIAL_SIZE
#define WTF_VECTOR_INITIAL_SIZE 4
#endif
static const size_t kInitialVectorSize = WTF_VECTOR_INITIAL_SIZE;
#endif
template <typename T, size_t inlineBuffer, typename Allocator>
class Deque;
template <bool needsDestruction, typename T>
struct VectorDestructor;
template <typename T>
struct VectorDestructor<false, T> {
STATIC_ONLY(VectorDestructor);
static void destruct(T*, T*) {}
};
template <typename T>
struct VectorDestructor<true, T> {
STATIC_ONLY(VectorDestructor);
static void destruct(T* begin, T* end) {
for (T* cur = begin; cur != end; ++cur)
cur->~T();
}
};
template <bool unusedSlotsMustBeZeroed, typename T>
struct VectorUnusedSlotClearer;
template <typename T>
struct VectorUnusedSlotClearer<false, T> {
STATIC_ONLY(VectorUnusedSlotClearer);
static void clear(T*, T*) {}
#if DCHECK_IS_ON()
static void checkCleared(const T*, const T*) {}
#endif
};
template <typename T>
struct VectorUnusedSlotClearer<true, T> {
STATIC_ONLY(VectorUnusedSlotClearer);
static void clear(T* begin, T* end) {
memset(reinterpret_cast<void*>(begin), 0, sizeof(T) * (end - begin));
}
#if DCHECK_IS_ON()
static void checkCleared(const T* begin, const T* end) {
const unsigned char* unusedArea =
reinterpret_cast<const unsigned char*>(begin);
const unsigned char* endAddress =
reinterpret_cast<const unsigned char*>(end);
DCHECK_GE(endAddress, unusedArea);
for (int i = 0; i < endAddress - unusedArea; ++i)
DCHECK(!unusedArea[i]);
}
#endif
};
template <bool canInitializeWithMemset, typename T>
struct VectorInitializer;
template <typename T>
struct VectorInitializer<false, T> {
STATIC_ONLY(VectorInitializer);
static void initialize(T* begin, T* end) {
for (T* cur = begin; cur != end; ++cur)
new (NotNull, cur) T;
}
};
template <typename T>
struct VectorInitializer<true, T> {
STATIC_ONLY(VectorInitializer);
static void initialize(T* begin, T* end) {
memset(begin, 0,
reinterpret_cast<char*>(end) - reinterpret_cast<char*>(begin));
}
};
template <bool canMoveWithMemcpy, typename T>
struct VectorMover;
template <typename T>
struct VectorMover<false, T> {
STATIC_ONLY(VectorMover);
static void move(T* src, T* srcEnd, T* dst) {
while (src != srcEnd) {
new (NotNull, dst) T(std::move(*src));
src->~T();
++dst;
++src;
}
}
static void moveOverlapping(T* src, T* srcEnd, T* dst) {
if (src > dst) {
move(src, srcEnd, dst);
} else {
T* dstEnd = dst + (srcEnd - src);
while (src != srcEnd) {
--srcEnd;
--dstEnd;
new (NotNull, dstEnd) T(std::move(*srcEnd));
srcEnd->~T();
}
}
}
static void swap(T* src, T* srcEnd, T* dst) {
std::swap_ranges(src, srcEnd, dst);
}
};
template <typename T>
struct VectorMover<true, T> {
STATIC_ONLY(VectorMover);
static void move(const T* src, const T* srcEnd, T* dst) {
if (LIKELY(dst && src))
memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) -
reinterpret_cast<const char*>(src));
}
static void moveOverlapping(const T* src, const T* srcEnd, T* dst) {
if (LIKELY(dst && src))
memmove(dst, src, reinterpret_cast<const char*>(srcEnd) -
reinterpret_cast<const char*>(src));
}
static void swap(T* src, T* srcEnd, T* dst) {
std::swap_ranges(reinterpret_cast<char*>(src),
reinterpret_cast<char*>(srcEnd),
reinterpret_cast<char*>(dst));
}
};
template <bool canCopyWithMemcpy, typename T>
struct VectorCopier;
template <typename T>
struct VectorCopier<false, T> {
STATIC_ONLY(VectorCopier);
template <typename U>
static void uninitializedCopy(const U* src, const U* srcEnd, T* dst) {
while (src != srcEnd) {
new (NotNull, dst) T(*src);
++dst;
++src;
}
}
};
template <typename T>
struct VectorCopier<true, T> {
STATIC_ONLY(VectorCopier);
static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) {
if (LIKELY(dst && src))
memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) -
reinterpret_cast<const char*>(src));
}
template <typename U>
static void uninitializedCopy(const U* src, const U* srcEnd, T* dst) {
VectorCopier<false, T>::uninitializedCopy(src, srcEnd, dst);
}
};
template <bool canFillWithMemset, typename T>
struct VectorFiller;
template <typename T>
struct VectorFiller<false, T> {
STATIC_ONLY(VectorFiller);
static void uninitializedFill(T* dst, T* dstEnd, const T& val) {
while (dst != dstEnd) {
new (NotNull, dst) T(val);
++dst;
}
}
};
template <typename T>
struct VectorFiller<true, T> {
STATIC_ONLY(VectorFiller);
static void uninitializedFill(T* dst, T* dstEnd, const T& val) {
static_assert(sizeof(T) == sizeof(char), "size of type should be one");
#if COMPILER(GCC) && defined(_FORTIFY_SOURCE)
if (!__builtin_constant_p(dstEnd - dst) || (!(dstEnd - dst)))
memset(dst, val, dstEnd - dst);
#else
memset(dst, val, dstEnd - dst);
#endif
}
};
template <bool canCompareWithMemcmp, typename T>
struct VectorComparer;
template <typename T>
struct VectorComparer<false, T> {
STATIC_ONLY(VectorComparer);
static bool compare(const T* a, const T* b, size_t size) {
DCHECK(a);
DCHECK(b);
return std::equal(a, a + size, b);
}
};
template <typename T>
struct VectorComparer<true, T> {
STATIC_ONLY(VectorComparer);
static bool compare(const T* a, const T* b, size_t size) {
DCHECK(a);
DCHECK(b);
return memcmp(a, b, sizeof(T) * size) == 0;
}
};
template <typename T>
struct VectorElementComparer {
STATIC_ONLY(VectorElementComparer);
template <typename U>
static bool compareElement(const T& left, const U& right) {
return left == right;
}
};
template <typename T>
struct VectorElementComparer<std::unique_ptr<T>> {
STATIC_ONLY(VectorElementComparer);
template <typename U>
static bool compareElement(const std::unique_ptr<T>& left, const U& right) {
return left.get() == right;
}
};
template <typename T>
struct VectorTypeOperations {
STATIC_ONLY(VectorTypeOperations);
static void destruct(T* begin, T* end) {
VectorDestructor<VectorTraits<T>::needsDestruction, T>::destruct(begin,
end);
}
static void initialize(T* begin, T* end) {
VectorInitializer<VectorTraits<T>::canInitializeWithMemset, T>::initialize(
begin, end);
}
static void move(T* src, T* srcEnd, T* dst) {
VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::move(src, srcEnd, dst);
}
static void moveOverlapping(T* src, T* srcEnd, T* dst) {
VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::moveOverlapping(
src, srcEnd, dst);
}
static void swap(T* src, T* srcEnd, T* dst) {
VectorMover<VectorTraits<T>::canMoveWithMemcpy, T>::swap(src, srcEnd, dst);
}
static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) {
VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(
src, srcEnd, dst);
}
static void uninitializedFill(T* dst, T* dstEnd, const T& val) {
VectorFiller<VectorTraits<T>::canFillWithMemset, T>::uninitializedFill(
dst, dstEnd, val);
}
static bool compare(const T* a, const T* b, size_t size) {
return VectorComparer<VectorTraits<T>::canCompareWithMemcmp, T>::compare(
a, b, size);
}
template <typename U>
static bool compareElement(const T& left, U&& right) {
return VectorElementComparer<T>::compareElement(left,
std::forward<U>(right));
}
};
template <typename T, bool hasInlineCapacity, typename Allocator>
class VectorBufferBase {
WTF_MAKE_NONCOPYABLE(VectorBufferBase);
DISALLOW_NEW();
public:
void allocateBuffer(size_t newCapacity) {
DCHECK(newCapacity);
size_t sizeToAllocate = allocationSize(newCapacity);
if (hasInlineCapacity)
m_buffer =
Allocator::template allocateInlineVectorBacking<T>(sizeToAllocate);
else
m_buffer = Allocator::template allocateVectorBacking<T>(sizeToAllocate);
m_capacity = sizeToAllocate / sizeof(T);
}
void allocateExpandedBuffer(size_t newCapacity) {
DCHECK(newCapacity);
size_t sizeToAllocate = allocationSize(newCapacity);
if (hasInlineCapacity)
m_buffer =
Allocator::template allocateInlineVectorBacking<T>(sizeToAllocate);
else
m_buffer =
Allocator::template allocateExpandedVectorBacking<T>(sizeToAllocate);
m_capacity = sizeToAllocate / sizeof(T);
}
size_t allocationSize(size_t capacity) const {
return Allocator::template quantizedSize<T>(capacity);
}
T* buffer() { return m_buffer; }
const T* buffer() const { return m_buffer; }
size_t capacity() const { return m_capacity; }
void clearUnusedSlots(T* from, T* to) {
// If the vector backing is garbage-collected and needs tracing or
// finalizing, we clear out the unused slots so that the visitor or the
// finalizer does not cause a problem when visiting the unused slots.
VectorUnusedSlotClearer<
Allocator::isGarbageCollected &&
(VectorTraits<T>::needsDestruction ||
IsTraceableInCollectionTrait<VectorTraits<T>>::value),
T>::clear(from, to);
}
void checkUnusedSlots(const T* from, const T* to) {
#if DCHECK_IS_ON() && !defined(ANNOTATE_CONTIGUOUS_CONTAINER)
VectorUnusedSlotClearer<
Allocator::isGarbageCollected &&
(VectorTraits<T>::needsDestruction ||
IsTraceableInCollectionTrait<VectorTraits<T>>::value),
T>::checkCleared(from, to);
#endif
}
// |end| is exclusive, a la STL.
struct OffsetRange final {
OffsetRange() : begin(0), end(0) {}
explicit OffsetRange(size_t begin, size_t end) : begin(begin), end(end) {
DCHECK_LE(begin, end);
}
bool empty() const { return begin == end; }
size_t begin;
size_t end;
};
protected:
VectorBufferBase() : m_buffer(nullptr), m_capacity(0) {}
VectorBufferBase(T* buffer, size_t capacity)
: m_buffer(buffer), m_capacity(capacity) {}
T* m_buffer;
unsigned m_capacity;
unsigned m_size;
};
template <typename T,
size_t inlineCapacity,
typename Allocator = PartitionAllocator>
class VectorBuffer;
template <typename T, typename Allocator>
class VectorBuffer<T, 0, Allocator>
: protected VectorBufferBase<T, false, Allocator> {
private:
using Base = VectorBufferBase<T, false, Allocator>;
public:
using OffsetRange = typename Base::OffsetRange;
VectorBuffer() {}
explicit VectorBuffer(size_t capacity) {
// Calling malloc(0) might take a lock and may actually do an allocation
// on some systems.
if (capacity)
allocateBuffer(capacity);
}
void destruct() {
deallocateBuffer(m_buffer);
m_buffer = nullptr;
}
void deallocateBuffer(T* bufferToDeallocate) {
Allocator::freeVectorBacking(bufferToDeallocate);
}
bool expandBuffer(size_t newCapacity) {
size_t sizeToAllocate = allocationSize(newCapacity);
if (Allocator::expandVectorBacking(m_buffer, sizeToAllocate)) {
m_capacity = sizeToAllocate / sizeof(T);
return true;
}
return false;
}
inline bool shrinkBuffer(size_t newCapacity) {
DCHECK_LT(newCapacity, capacity());
size_t sizeToAllocate = allocationSize(newCapacity);
if (Allocator::shrinkVectorBacking(m_buffer, allocationSize(capacity()),
sizeToAllocate)) {
m_capacity = sizeToAllocate / sizeof(T);
return true;
}
return false;
}
void resetBufferPointer() {
m_buffer = nullptr;
m_capacity = 0;
}
// See the other specialization for the meaning of |thisHole| and |otherHole|.
// They are irrelevant in this case.
void swapVectorBuffer(VectorBuffer<T, 0, Allocator>& other,
OffsetRange thisHole,
OffsetRange otherHole) {
static_assert(VectorTraits<T>::canSwapUsingCopyOrMove,
"Cannot swap HeapVectors of TraceWrapperMembers.");
std::swap(m_buffer, other.m_buffer);
std::swap(m_capacity, other.m_capacity);
std::swap(m_size, other.m_size);
}
using Base::allocateBuffer;
using Base::allocationSize;
using Base::buffer;
using Base::capacity;
using Base::clearUnusedSlots;
using Base::checkUnusedSlots;
bool hasOutOfLineBuffer() const {
// When inlineCapacity is 0 we have an out of line buffer if we have a
// buffer.
return buffer();
}
T** bufferSlot() { return &m_buffer; }
protected:
using Base::m_size;
private:
using Base::m_buffer;
using Base::m_capacity;
};
template <typename T, size_t inlineCapacity, typename Allocator>
class VectorBuffer : protected VectorBufferBase<T, true, Allocator> {
WTF_MAKE_NONCOPYABLE(VectorBuffer);
private:
using Base = VectorBufferBase<T, true, Allocator>;
public:
using OffsetRange = typename Base::OffsetRange;
VectorBuffer() : Base(inlineBuffer(), inlineCapacity) {}
explicit VectorBuffer(size_t capacity)
: Base(inlineBuffer(), inlineCapacity) {
if (capacity > inlineCapacity)
Base::allocateBuffer(capacity);
}
void destruct() {
deallocateBuffer(m_buffer);
m_buffer = nullptr;
}
NEVER_INLINE void reallyDeallocateBuffer(T* bufferToDeallocate) {
Allocator::freeInlineVectorBacking(bufferToDeallocate);
}
void deallocateBuffer(T* bufferToDeallocate) {
if (UNLIKELY(bufferToDeallocate != inlineBuffer()))
reallyDeallocateBuffer(bufferToDeallocate);
}
bool expandBuffer(size_t newCapacity) {
DCHECK_GT(newCapacity, inlineCapacity);
if (m_buffer == inlineBuffer())
return false;
size_t sizeToAllocate = allocationSize(newCapacity);
if (Allocator::expandInlineVectorBacking(m_buffer, sizeToAllocate)) {
m_capacity = sizeToAllocate / sizeof(T);
return true;
}
return false;
}
inline bool shrinkBuffer(size_t newCapacity) {
DCHECK_LT(newCapacity, capacity());
if (newCapacity <= inlineCapacity) {
// We need to switch to inlineBuffer. Vector::shrinkCapacity will
// handle it.
return false;
}
DCHECK_NE(m_buffer, inlineBuffer());
size_t newSize = allocationSize(newCapacity);
if (!Allocator::shrinkInlineVectorBacking(
m_buffer, allocationSize(capacity()), newSize))
return false;
m_capacity = newSize / sizeof(T);
return true;
}
void resetBufferPointer() {
m_buffer = inlineBuffer();
m_capacity = inlineCapacity;
}
void allocateBuffer(size_t newCapacity) {
// FIXME: This should DCHECK(!m_buffer) to catch misuse/leaks.
if (newCapacity > inlineCapacity)
Base::allocateBuffer(newCapacity);
else
resetBufferPointer();
}
void allocateExpandedBuffer(size_t newCapacity) {
if (newCapacity > inlineCapacity)
Base::allocateExpandedBuffer(newCapacity);
else
resetBufferPointer();
}
size_t allocationSize(size_t capacity) const {
if (capacity <= inlineCapacity)
return m_inlineBufferSize;
return Base::allocationSize(capacity);
}
// Swap two vector buffers, both of which have the same non-zero inline
// capacity.
//
// If the data is in an out-of-line buffer, we can just pass the pointers
// across the two buffers. If the data is in an inline buffer, we need to
// either swap or move each element, depending on whether each slot is
// occupied or not.
//
// Further complication comes from the fact that VectorBuffer is also used as
// the backing store of a Deque. Deque allocates the objects like a ring
// buffer, so there may be a "hole" (unallocated region) in the middle of the
// buffer. This function assumes elements in a range [m_buffer, m_buffer +
// m_size) are all allocated except for elements within |thisHole|. The same
// applies for |other.m_buffer| and |otherHole|.
void swapVectorBuffer(VectorBuffer<T, inlineCapacity, Allocator>& other,
OffsetRange thisHole,
OffsetRange otherHole) {
using TypeOperations = VectorTypeOperations<T>;
static_assert(VectorTraits<T>::canSwapUsingCopyOrMove,
"Cannot swap HeapVectors of TraceWrapperMembers.");
if (buffer() != inlineBuffer() && other.buffer() != other.inlineBuffer()) {
// The easiest case: both buffers are non-inline. We just need to swap the
// pointers.
std::swap(m_buffer, other.m_buffer);
std::swap(m_capacity, other.m_capacity);
std::swap(m_size, other.m_size);
return;
}
Allocator::enterGCForbiddenScope();
// Otherwise, we at least need to move some elements from one inline buffer
// to another.
//
// Terminology: "source" is a place from which elements are copied, and
// "destination" is a place to which elements are copied. thisSource or
// otherSource can be empty (represented by nullptr) when this range or
// other range is in an out-of-line buffer.
//
// We first record which range needs to get moved and where elements in such
// a range will go. Elements in an inline buffer will go to the other
// buffer's inline buffer. Elements in an out-of-line buffer won't move,
// because we can just swap pointers of out-of-line buffers.
T* thisSourceBegin = nullptr;
size_t thisSourceSize = 0;
T* thisDestinationBegin = nullptr;
if (buffer() == inlineBuffer()) {
thisSourceBegin = buffer();
thisSourceSize = m_size;
thisDestinationBegin = other.inlineBuffer();
if (!thisHole.empty()) { // Sanity check.
DCHECK_LT(thisHole.begin, thisHole.end);
DCHECK_LE(thisHole.end, thisSourceSize);
}
} else {
// We don't need the hole information for an out-of-line buffer.
thisHole.begin = thisHole.end = 0;
}
T* otherSourceBegin = nullptr;
size_t otherSourceSize = 0;
T* otherDestinationBegin = nullptr;
if (other.buffer() == other.inlineBuffer()) {
otherSourceBegin = other.buffer();
otherSourceSize = other.m_size;
otherDestinationBegin = inlineBuffer();
if (!otherHole.empty()) {
DCHECK_LT(otherHole.begin, otherHole.end);
DCHECK_LE(otherHole.end, otherSourceSize);
}
} else {
otherHole.begin = otherHole.end = 0;
}
// Next, we mutate members and do other bookkeeping. We do pointer swapping
// (for out-of-line buffers) here if we can. From now on, don't assume
// buffer() or capacity() maintains their original values.
std::swap(m_capacity, other.m_capacity);
if (thisSourceBegin &&
!otherSourceBegin) { // Our buffer is inline, theirs is not.
DCHECK_EQ(buffer(), inlineBuffer());
DCHECK_NE(other.buffer(), other.inlineBuffer());
ANNOTATE_DELETE_BUFFER(m_buffer, inlineCapacity, m_size);
m_buffer = other.buffer();
other.m_buffer = other.inlineBuffer();
std::swap(m_size, other.m_size);
ANNOTATE_NEW_BUFFER(other.m_buffer, inlineCapacity, other.m_size);
} else if (!thisSourceBegin &&
otherSourceBegin) { // Their buffer is inline, ours is not.
DCHECK_NE(buffer(), inlineBuffer());
DCHECK_EQ(other.buffer(), other.inlineBuffer());
ANNOTATE_DELETE_BUFFER(other.m_buffer, inlineCapacity, other.m_size);
other.m_buffer = buffer();
m_buffer = inlineBuffer();
std::swap(m_size, other.m_size);
ANNOTATE_NEW_BUFFER(m_buffer, inlineCapacity, m_size);
} else { // Both buffers are inline.
DCHECK(thisSourceBegin);
DCHECK(otherSourceBegin);
DCHECK_EQ(buffer(), inlineBuffer());
DCHECK_EQ(other.buffer(), other.inlineBuffer());
ANNOTATE_CHANGE_SIZE(m_buffer, inlineCapacity, m_size, other.m_size);
ANNOTATE_CHANGE_SIZE(other.m_buffer, inlineCapacity, other.m_size,
m_size);
std::swap(m_size, other.m_size);
}
// We are ready to move elements. We determine an action for each "section",
// which is a contiguous range such that all elements in the range are
// treated similarly.
size_t sectionBegin = 0;
while (sectionBegin < inlineCapacity) {
// To determine the end of this section, we list up all the boundaries
// where the "occupiedness" may change.
size_t sectionEnd = inlineCapacity;
if (thisSourceBegin && sectionBegin < thisSourceSize)
sectionEnd = std::min(sectionEnd, thisSourceSize);
if (!thisHole.empty() && sectionBegin < thisHole.begin)
sectionEnd = std::min(sectionEnd, thisHole.begin);
if (!thisHole.empty() && sectionBegin < thisHole.end)
sectionEnd = std::min(sectionEnd, thisHole.end);
if (otherSourceBegin && sectionBegin < otherSourceSize)
sectionEnd = std::min(sectionEnd, otherSourceSize);
if (!otherHole.empty() && sectionBegin < otherHole.begin)
sectionEnd = std::min(sectionEnd, otherHole.begin);
if (!otherHole.empty() && sectionBegin < otherHole.end)
sectionEnd = std::min(sectionEnd, otherHole.end);
DCHECK_LT(sectionBegin, sectionEnd);
// Is the |sectionBegin|-th element of |thisSource| occupied?
bool thisOccupied = false;
if (thisSourceBegin && sectionBegin < thisSourceSize) {
// Yes, it's occupied, unless the position is in a hole.
if (thisHole.empty() || sectionBegin < thisHole.begin ||
sectionBegin >= thisHole.end)
thisOccupied = true;
}
bool otherOccupied = false;
if (otherSourceBegin && sectionBegin < otherSourceSize) {
if (otherHole.empty() || sectionBegin < otherHole.begin ||
sectionBegin >= otherHole.end)
otherOccupied = true;
}
if (thisOccupied && otherOccupied) {
// Both occupied; swap them. In this case, one's destination must be the
// other's source (i.e. both ranges are in inline buffers).
DCHECK_EQ(thisDestinationBegin, otherSourceBegin);
DCHECK_EQ(otherDestinationBegin, thisSourceBegin);
TypeOperations::swap(thisSourceBegin + sectionBegin,
thisSourceBegin + sectionEnd,
otherSourceBegin + sectionBegin);
} else if (thisOccupied) {
// Move from ours to theirs.
TypeOperations::move(thisSourceBegin + sectionBegin,
thisSourceBegin + sectionEnd,
thisDestinationBegin + sectionBegin);
Base::clearUnusedSlots(thisSourceBegin + sectionBegin,
thisSourceBegin + sectionEnd);
} else if (otherOccupied) {
// Move from theirs to ours.
TypeOperations::move(otherSourceBegin + sectionBegin,
otherSourceBegin + sectionEnd,
otherDestinationBegin + sectionBegin);
Base::clearUnusedSlots(otherSourceBegin + sectionBegin,
otherSourceBegin + sectionEnd);
} else {
// Both empty; nothing to do.
}
sectionBegin = sectionEnd;
}
Allocator::leaveGCForbiddenScope();
}
using Base::buffer;
using Base::capacity;
bool hasOutOfLineBuffer() const {
return buffer() && buffer() != inlineBuffer();
}
T** bufferSlot() { return &m_buffer; }
protected:
using Base::m_size;
private:
using Base::m_buffer;
using Base::m_capacity;
static const size_t m_inlineBufferSize = inlineCapacity * sizeof(T);
T* inlineBuffer() { return reinterpret_cast_ptr<T*>(m_inlineBuffer.buffer); }
const T* inlineBuffer() const {
return reinterpret_cast_ptr<const T*>(m_inlineBuffer.buffer);
}
AlignedBuffer<m_inlineBufferSize, WTF_ALIGN_OF(T)> m_inlineBuffer;
template <typename U, size_t inlineBuffer, typename V>
friend class Deque;
};
// Heap-allocated vectors with no inlineCapacity never need a destructor.
template <typename T,
size_t inlineCapacity = 0,
typename Allocator = PartitionAllocator>
class Vector
: private VectorBuffer<T, INLINE_CAPACITY, Allocator>,
public ConditionalDestructor<Vector<T, INLINE_CAPACITY, Allocator>,
(INLINE_CAPACITY == 0) &&
Allocator::isGarbageCollected> {
USE_ALLOCATOR(Vector, Allocator);
using Base = VectorBuffer<T, INLINE_CAPACITY, Allocator>;
using TypeOperations = VectorTypeOperations<T>;
using OffsetRange = typename Base::OffsetRange;
public:
typedef T ValueType;
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Vector() {
static_assert(!std::is_polymorphic<T>::value ||
!VectorTraits<T>::canInitializeWithMemset,
"Cannot initialize with memset if there is a vtable");
static_assert(Allocator::isGarbageCollected ||
!AllowsOnlyPlacementNew<T>::value ||
!IsTraceable<T>::value,
"Cannot put DISALLOW_NEW_EXCEPT_PLACEMENT_NEW objects that "
"have trace methods into an off-heap Vector");
static_assert(Allocator::isGarbageCollected ||
!IsPointerToGarbageCollectedType<T>::value,
"Cannot put raw pointers to garbage-collected classes into "
"an off-heap Vector. Use HeapVector<Member<T>> instead.");
ANNOTATE_NEW_BUFFER(begin(), capacity(), 0);
m_size = 0;
}
explicit Vector(size_t size) : Base(size) {
static_assert(!std::is_polymorphic<T>::value ||
!VectorTraits<T>::canInitializeWithMemset,
"Cannot initialize with memset if there is a vtable");
static_assert(Allocator::isGarbageCollected ||
!AllowsOnlyPlacementNew<T>::value ||
!IsTraceable<T>::value,
"Cannot put DISALLOW_NEW_EXCEPT_PLACEMENT_NEW objects that "
"have trace methods into an off-heap Vector");
static_assert(Allocator::isGarbageCollected ||
!IsPointerToGarbageCollectedType<T>::value,
"Cannot put raw pointers to garbage-collected classes into "
"an off-heap Vector. Use HeapVector<Member<T>> instead.");
ANNOTATE_NEW_BUFFER(begin(), capacity(), size);
m_size = size;
TypeOperations::initialize(begin(), end());
}
// Off-GC-heap vectors: Destructor should be called.
// On-GC-heap vectors: Destructor should be called for inline buffers (if
// any) but destructor shouldn't be called for vector backing since it is
// managed by the traced GC heap.
void finalize() {
if (!INLINE_CAPACITY) {
if (LIKELY(!Base::buffer()))
return;
}
ANNOTATE_DELETE_BUFFER(begin(), capacity(), m_size);
if (LIKELY(m_size) &&
!(Allocator::isGarbageCollected && this->hasOutOfLineBuffer())) {
TypeOperations::destruct(begin(), end());
m_size = 0; // Partial protection against use-after-free.
}
Base::destruct();
}
void finalizeGarbageCollectedObject() { finalize(); }
Vector(const Vector&);
template <size_t otherCapacity>
explicit Vector(const Vector<T, otherCapacity, Allocator>&);
Vector& operator=(const Vector&);
template <size_t otherCapacity>
Vector& operator=(const Vector<T, otherCapacity, Allocator>&);
Vector(Vector&&);
Vector& operator=(Vector&&);
Vector(std::initializer_list<T> elements);
Vector& operator=(std::initializer_list<T> elements);
size_t size() const { return m_size; }
size_t capacity() const { return Base::capacity(); }
bool isEmpty() const { return !size(); }
T& at(size_t i) {
RELEASE_ASSERT(i < size());
return Base::buffer()[i];
}
const T& at(size_t i) const {
RELEASE_ASSERT(i < size());
return Base::buffer()[i];
}
T& operator[](size_t i) { return at(i); }
const T& operator[](size_t i) const { return at(i); }
T* data() { return Base::buffer(); }
const T* data() const { return Base::buffer(); }
iterator begin() { return data(); }
iterator end() { return begin() + m_size; }
const_iterator begin() const { return data(); }
const_iterator end() const { return begin() + m_size; }
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
T& front() { return at(0); }
const T& front() const { return at(0); }
T& back() { return at(size() - 1); }
const T& back() const { return at(size() - 1); }
template <typename U>
bool contains(const U&) const;
template <typename U>
size_t find(const U&) const;
template <typename U>
size_t reverseFind(const U&) const;
void shrink(size_t);
void grow(size_t);
void resize(size_t);
void reserveCapacity(size_t newCapacity);
void reserveInitialCapacity(size_t initialCapacity);
void shrinkToFit() { shrinkCapacity(size()); }
void shrinkToReasonableCapacity() {
if (size() * 2 < capacity())
shrinkCapacity(size() + size() / 4 + 1);
}
void clear() { shrinkCapacity(0); }
template <typename U>
void append(const U*, size_t);
template <typename U>
void append(U&&);
template <typename U>
void push_back(U&&);
template <typename... Args>
T& emplace_back(Args&&...);
template <typename U>
void uncheckedAppend(U&& val);
template <typename U, size_t otherCapacity, typename V>
void appendVector(const Vector<U, otherCapacity, V>&);
template <typename U>
void insert(size_t position, const U*, size_t);
template <typename U>
void insert(size_t position, U&&);
template <typename U, size_t c, typename V>
void insert(size_t position, const Vector<U, c, V>&);
template <typename U>
void prepend(const U*, size_t);
template <typename U>
void prepend(U&&);
template <typename U, size_t c, typename V>
void prependVector(const Vector<U, c, V>&);
void remove(size_t position);
void remove(size_t position, size_t length);
void pop_back() {
DCHECK(!isEmpty());
shrink(size() - 1);
}
Vector(size_t size, const T& val) : Base(size) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), size);
m_size = size;
TypeOperations::uninitializedFill(begin(), end(), val);
}
void fill(const T&, size_t);
void fill(const T& val) { fill(val, size()); }
template <typename Iterator>
void appendRange(Iterator start, Iterator end);
void swap(Vector& other) {
Base::swapVectorBuffer(other, OffsetRange(), OffsetRange());
}
void reverse();
template <typename VisitorDispatcher>
void trace(VisitorDispatcher);
class GCForbiddenScope {
STACK_ALLOCATED();
public:
GCForbiddenScope() { Allocator::enterGCForbiddenScope(); }
~GCForbiddenScope() { Allocator::leaveGCForbiddenScope(); }
};
protected:
using Base::checkUnusedSlots;
using Base::clearUnusedSlots;
private:
void expandCapacity(size_t newMinCapacity);
T* expandCapacity(size_t newMinCapacity, T*);
T* expandCapacity(size_t newMinCapacity, const T* data) {
return expandCapacity(newMinCapacity, const_cast<T*>(data));
}
template <typename U>
U* expandCapacity(size_t newMinCapacity, U*);
void shrinkCapacity(size_t newCapacity);
template <typename U>
void appendSlowCase(U&&);
using Base::m_size;
using Base::buffer;
using Base::swapVectorBuffer;
using Base::allocateBuffer;
using Base::allocationSize;
};
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>::Vector(const Vector& other)
: Base(other.capacity()) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), other.size());
m_size = other.size();
TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <size_t otherCapacity>
Vector<T, inlineCapacity, Allocator>::Vector(
const Vector<T, otherCapacity, Allocator>& other)
: Base(other.capacity()) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), other.size());
m_size = other.size();
TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
}
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::
operator=(const Vector<T, inlineCapacity, Allocator>& other) {
if (UNLIKELY(&other == this))
return *this;
if (size() > other.size()) {
shrink(other.size());
} else if (other.size() > capacity()) {
clear();
reserveCapacity(other.size());
DCHECK(begin());
}
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, other.size());
std::copy(other.begin(), other.begin() + size(), begin());
TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end());
m_size = other.size();
return *this;
}
inline bool typelessPointersAreEqual(const void* a, const void* b) {
return a == b;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <size_t otherCapacity>
Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::
operator=(const Vector<T, otherCapacity, Allocator>& other) {
// If the inline capacities match, we should call the more specific
// template. If the inline capacities don't match, the two objects
// shouldn't be allocated the same address.
DCHECK(!typelessPointersAreEqual(&other, this));
if (size() > other.size()) {
shrink(other.size());
} else if (other.size() > capacity()) {
clear();
reserveCapacity(other.size());
DCHECK(begin());
}
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, other.size());
std::copy(other.begin(), other.begin() + size(), begin());
TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end());
m_size = other.size();
return *this;
}
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>::Vector(
Vector<T, inlineCapacity, Allocator>&& other) {
m_size = 0;
// It's a little weird to implement a move constructor using swap but this
// way we don't have to add a move constructor to VectorBuffer.
swap(other);
}
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::
operator=(Vector<T, inlineCapacity, Allocator>&& other) {
swap(other);
return *this;
}
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>::Vector(std::initializer_list<T> elements)
: Base(elements.size()) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), elements.size());
m_size = elements.size();
TypeOperations::uninitializedCopy(elements.begin(), elements.end(), begin());
}
template <typename T, size_t inlineCapacity, typename Allocator>
Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::
operator=(std::initializer_list<T> elements) {
if (size() > elements.size()) {
shrink(elements.size());
} else if (elements.size() > capacity()) {
clear();
reserveCapacity(elements.size());
DCHECK(begin());
}
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, elements.size());
std::copy(elements.begin(), elements.begin() + m_size, begin());
TypeOperations::uninitializedCopy(elements.begin() + m_size, elements.end(),
end());
m_size = elements.size();
return *this;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
bool Vector<T, inlineCapacity, Allocator>::contains(const U& value) const {
return find(value) != kNotFound;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
size_t Vector<T, inlineCapacity, Allocator>::find(const U& value) const {
const T* b = begin();
const T* e = end();
for (const T* iter = b; iter < e; ++iter) {
if (TypeOperations::compareElement(*iter, value))
return iter - b;
}
return kNotFound;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
size_t Vector<T, inlineCapacity, Allocator>::reverseFind(const U& value) const {
const T* b = begin();
const T* iter = end();
while (iter > b) {
--iter;
if (TypeOperations::compareElement(*iter, value))
return iter - b;
}
return kNotFound;
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::fill(const T& val, size_t newSize) {
if (size() > newSize) {
shrink(newSize);
} else if (newSize > capacity()) {
clear();
reserveCapacity(newSize);
DCHECK(begin());
}
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, newSize);
std::fill(begin(), end(), val);
TypeOperations::uninitializedFill(end(), begin() + newSize, val);
m_size = newSize;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename Iterator>
void Vector<T, inlineCapacity, Allocator>::appendRange(Iterator start,
Iterator end) {
for (Iterator it = start; it != end; ++it)
append(*it);
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::expandCapacity(
size_t newMinCapacity) {
size_t oldCapacity = capacity();
size_t expandedCapacity = oldCapacity;
// We use a more aggressive expansion strategy for Vectors with inline
// storage. This is because they are more likely to be on the stack, so the
// risk of heap bloat is minimized. Furthermore, exceeding the inline
// capacity limit is not supposed to happen in the common case and may
// indicate a pathological condition or microbenchmark.
if (INLINE_CAPACITY) {
expandedCapacity *= 2;
// Check for integer overflow, which could happen in the 32-bit build.
RELEASE_ASSERT(expandedCapacity > oldCapacity);
} else {
// This cannot integer overflow.
// On 64-bit, the "expanded" integer is 32-bit, and any encroachment
// above 2^32 will fail allocation in allocateBuffer(). On 32-bit,
// there's not enough address space to hold the old and new buffers. In
// addition, our underlying allocator is supposed to always fail on >
// (2^31 - 1) allocations.
expandedCapacity += (expandedCapacity / 4) + 1;
}
reserveCapacity(std::max(
newMinCapacity,
std::max(static_cast<size_t>(kInitialVectorSize), expandedCapacity)));
}
template <typename T, size_t inlineCapacity, typename Allocator>
T* Vector<T, inlineCapacity, Allocator>::expandCapacity(size_t newMinCapacity,
T* ptr) {
if (ptr < begin() || ptr >= end()) {
expandCapacity(newMinCapacity);
return ptr;
}
size_t index = ptr - begin();
expandCapacity(newMinCapacity);
return begin() + index;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
inline U* Vector<T, inlineCapacity, Allocator>::expandCapacity(
size_t newMinCapacity,
U* ptr) {
expandCapacity(newMinCapacity);
return ptr;
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::resize(size_t size) {
if (size <= m_size) {
TypeOperations::destruct(begin() + size, end());
clearUnusedSlots(begin() + size, end());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, size);
} else {
if (size > capacity())
expandCapacity(size);
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, size);
TypeOperations::initialize(end(), begin() + size);
}
m_size = size;
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::shrink(size_t size) {
DCHECK_LE(size, m_size);
TypeOperations::destruct(begin() + size, end());
clearUnusedSlots(begin() + size, end());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, size);
m_size = size;
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::grow(size_t size) {
DCHECK_GE(size, m_size);
if (size > capacity())
expandCapacity(size);
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, size);
TypeOperations::initialize(end(), begin() + size);
m_size = size;
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::reserveCapacity(size_t newCapacity) {
if (UNLIKELY(newCapacity <= capacity()))
return;
T* oldBuffer = begin();
if (!oldBuffer) {
Base::allocateBuffer(newCapacity);
return;
}
#ifdef ANNOTATE_CONTIGUOUS_CONTAINER
size_t oldCapacity = capacity();
#endif
// The Allocator::isGarbageCollected check is not needed. The check is just
// a static hint for a compiler to indicate that Base::expandBuffer returns
// false if Allocator is a PartitionAllocator.
if (Allocator::isGarbageCollected && Base::expandBuffer(newCapacity)) {
ANNOTATE_CHANGE_CAPACITY(begin(), oldCapacity, m_size, capacity());
return;
}
T* oldEnd = end();
Base::allocateExpandedBuffer(newCapacity);
ANNOTATE_NEW_BUFFER(begin(), capacity(), m_size);
TypeOperations::move(oldBuffer, oldEnd, begin());
clearUnusedSlots(oldBuffer, oldEnd);
ANNOTATE_DELETE_BUFFER(oldBuffer, oldCapacity, m_size);
Base::deallocateBuffer(oldBuffer);
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::reserveInitialCapacity(
size_t initialCapacity) {
DCHECK(!m_size);
DCHECK(capacity() == INLINE_CAPACITY);
if (initialCapacity > INLINE_CAPACITY) {
ANNOTATE_DELETE_BUFFER(begin(), capacity(), m_size);
Base::allocateBuffer(initialCapacity);
ANNOTATE_NEW_BUFFER(begin(), capacity(), m_size);
}
}
template <typename T, size_t inlineCapacity, typename Allocator>
void Vector<T, inlineCapacity, Allocator>::shrinkCapacity(size_t newCapacity) {
if (newCapacity >= capacity())
return;
if (newCapacity < size())
shrink(newCapacity);
T* oldBuffer = begin();
#ifdef ANNOTATE_CONTIGUOUS_CONTAINER
size_t oldCapacity = capacity();
#endif
if (newCapacity > 0) {
if (Base::shrinkBuffer(newCapacity)) {
ANNOTATE_CHANGE_CAPACITY(begin(), oldCapacity, m_size, capacity());
return;
}
T* oldEnd = end();
Base::allocateBuffer(newCapacity);
if (begin() != oldBuffer) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), m_size);
TypeOperations::move(oldBuffer, oldEnd, begin());
clearUnusedSlots(oldBuffer, oldEnd);
ANNOTATE_DELETE_BUFFER(oldBuffer, oldCapacity, m_size);
}
} else {
Base::resetBufferPointer();
#ifdef ANNOTATE_CONTIGUOUS_CONTAINER
if (oldBuffer != begin()) {
ANNOTATE_NEW_BUFFER(begin(), capacity(), m_size);
ANNOTATE_DELETE_BUFFER(oldBuffer, oldCapacity, m_size);
}
#endif
}
Base::deallocateBuffer(oldBuffer);
}
// Templatizing these is better than just letting the conversion happen
// implicitly, because for instance it allows a PassRefPtr to be appended to a
// RefPtr vector without refcount thrash.
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
void Vector<T, inlineCapacity, Allocator>::append(const U* data,
size_t dataSize) {
DCHECK(Allocator::isAllocationAllowed());
size_t newSize = m_size + dataSize;
if (newSize > capacity()) {
data = expandCapacity(newSize, data);
DCHECK(begin());
}
RELEASE_ASSERT(newSize >= m_size);
T* dest = end();
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, newSize);
VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(
data, &data[dataSize], dest);
m_size = newSize;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
ALWAYS_INLINE void Vector<T, inlineCapacity, Allocator>::push_back(U&& val) {
return append(std::forward<U>(val));
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
ALWAYS_INLINE void Vector<T, inlineCapacity, Allocator>::append(U&& val) {
DCHECK(Allocator::isAllocationAllowed());
if (LIKELY(size() != capacity())) {
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size + 1);
new (NotNull, end()) T(std::forward<U>(val));
++m_size;
return;
}
appendSlowCase(std::forward<U>(val));
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename... Args>
ALWAYS_INLINE T& Vector<T, inlineCapacity, Allocator>::emplace_back(
Args&&... args) {
static_assert(sizeof...(Args), "grow() must be called instead");
static_assert(sizeof...(Args) != 1, "append() must be called instead");
DCHECK(Allocator::isAllocationAllowed());
if (UNLIKELY(size() == capacity()))
expandCapacity(size() + 1);
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size + 1);
T* t = new (NotNull, end()) T(std::forward<Args>(args)...);
++m_size;
return *t;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
NEVER_INLINE void Vector<T, inlineCapacity, Allocator>::appendSlowCase(
U&& val) {
DCHECK_EQ(size(), capacity());
typename std::remove_reference<U>::type* ptr = &val;
ptr = expandCapacity(size() + 1, ptr);
DCHECK(begin());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size + 1);
new (NotNull, end()) T(std::forward<U>(*ptr));
++m_size;
}
// This version of append saves a branch in the case where you know that the
// vector's capacity is large enough for the append to succeed.
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
ALWAYS_INLINE void Vector<T, inlineCapacity, Allocator>::uncheckedAppend(
U&& val) {
#ifdef ANNOTATE_CONTIGUOUS_CONTAINER
// Vectors in ASAN builds don't have inlineCapacity.
append(std::forward<U>(val));
#else
DCHECK_LT(size(), capacity());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size + 1);
new (NotNull, end()) T(std::forward<U>(val));
++m_size;
#endif
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U, size_t otherCapacity, typename OtherAllocator>
inline void Vector<T, inlineCapacity, Allocator>::appendVector(
const Vector<U, otherCapacity, OtherAllocator>& val) {
append(val.begin(), val.size());
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
void Vector<T, inlineCapacity, Allocator>::insert(size_t position,
const U* data,
size_t dataSize) {
DCHECK(Allocator::isAllocationAllowed());
RELEASE_ASSERT(position <= size());
size_t newSize = m_size + dataSize;
if (newSize > capacity()) {
data = expandCapacity(newSize, data);
DCHECK(begin());
}
RELEASE_ASSERT(newSize >= m_size);
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, newSize);
T* spot = begin() + position;
TypeOperations::moveOverlapping(spot, end(), spot + dataSize);
VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(
data, &data[dataSize], spot);
m_size = newSize;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
inline void Vector<T, inlineCapacity, Allocator>::insert(size_t position,
U&& val) {
DCHECK(Allocator::isAllocationAllowed());
RELEASE_ASSERT(position <= size());
typename std::remove_reference<U>::type* data = &val;
if (size() == capacity()) {
data = expandCapacity(size() + 1, data);
DCHECK(begin());
}
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size + 1);
T* spot = begin() + position;
TypeOperations::moveOverlapping(spot, end(), spot + 1);
new (NotNull, spot) T(std::forward<U>(*data));
++m_size;
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U, size_t c, typename OtherAllocator>
inline void Vector<T, inlineCapacity, Allocator>::insert(
size_t position,
const Vector<U, c, OtherAllocator>& val) {
insert(position, val.begin(), val.size());
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
void Vector<T, inlineCapacity, Allocator>::prepend(const U* data,
size_t dataSize) {
insert(0, data, dataSize);
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
inline void Vector<T, inlineCapacity, Allocator>::prepend(U&& val) {
insert(0, std::forward<U>(val));
}
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U, size_t c, typename V>
inline void Vector<T, inlineCapacity, Allocator>::prependVector(
const Vector<U, c, V>& val) {
insert(0, val.begin(), val.size());
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::remove(size_t position) {
RELEASE_ASSERT(position < size());
T* spot = begin() + position;
spot->~T();
TypeOperations::moveOverlapping(spot + 1, end(), spot);
clearUnusedSlots(end() - 1, end());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size - 1);
--m_size;
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::remove(size_t position,
size_t length) {
SECURITY_DCHECK(position <= size());
if (!length)
return;
RELEASE_ASSERT(position + length <= size());
T* beginSpot = begin() + position;
T* endSpot = beginSpot + length;
TypeOperations::destruct(beginSpot, endSpot);
TypeOperations::moveOverlapping(endSpot, end(), beginSpot);
clearUnusedSlots(end() - length, end());
ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, m_size - length);
m_size -= length;
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void Vector<T, inlineCapacity, Allocator>::reverse() {
for (size_t i = 0; i < m_size / 2; ++i)
std::swap(at(i), at(m_size - 1 - i));
}
template <typename T, size_t inlineCapacity, typename Allocator>
inline void swap(Vector<T, inlineCapacity, Allocator>& a,
Vector<T, inlineCapacity, Allocator>& b) {
a.swap(b);
}
template <typename T,
size_t inlineCapacityA,
size_t inlineCapacityB,
typename Allocator>
bool operator==(const Vector<T, inlineCapacityA, Allocator>& a,
const Vector<T, inlineCapacityB, Allocator>& b) {
if (a.size() != b.size())
return false;
if (a.isEmpty())
return true;
return VectorTypeOperations<T>::compare(a.data(), b.data(), a.size());
}
template <typename T,
size_t inlineCapacityA,
size_t inlineCapacityB,
typename Allocator>
inline bool operator!=(const Vector<T, inlineCapacityA, Allocator>& a,
const Vector<T, inlineCapacityB, Allocator>& b) {
return !(a == b);
}
// This is only called if the allocator is a HeapAllocator. It is used when
// visiting during a tracing GC.
template <typename T, size_t inlineCapacity, typename Allocator>
template <typename VisitorDispatcher>
void Vector<T, inlineCapacity, Allocator>::trace(VisitorDispatcher visitor) {
DCHECK(Allocator::isGarbageCollected) << "Garbage collector must be enabled.";
if (!buffer())
return;
if (this->hasOutOfLineBuffer()) {
// This is a performance optimization for a case where the buffer has
// been already traced by somewhere. This can happen if the conservative
// scanning traced an on-stack (false-positive or real) pointer to the
// HeapVector, and then visitor->trace() traces the HeapVector.
if (Allocator::isHeapObjectAlive(buffer()))
return;
Allocator::markNoTracing(visitor, buffer());
Allocator::registerBackingStoreReference(visitor, Base::bufferSlot());
}
const T* bufferBegin = buffer();
const T* bufferEnd = buffer() + size();
if (IsTraceableInCollectionTrait<VectorTraits<T>>::value) {
for (const T* bufferEntry = bufferBegin; bufferEntry != bufferEnd;
bufferEntry++)
Allocator::template trace<VisitorDispatcher, T, VectorTraits<T>>(
visitor, *const_cast<T*>(bufferEntry));
checkUnusedSlots(buffer() + size(), buffer() + capacity());
}
}
} // namespace WTF
using WTF::Vector;
#endif // WTF_Vector_h
|