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
|
/*
* Copyright (C) 2017-2020 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/utilities/heap_allocator.h"
#include "test.h"
#include "gtest/gtest.h"
#include <iostream>
#include <random>
using namespace NEO;
const size_t sizeThreshold = 16 * 4096;
class HeapAllocatorUnderTest : public HeapAllocator {
public:
HeapAllocatorUnderTest(uint64_t address, uint64_t size, size_t threshold) : HeapAllocator(address, size, threshold) {}
HeapAllocatorUnderTest(uint64_t address, uint64_t size) : HeapAllocator(address, size) {}
uint64_t getLeftBound() const { return this->pLeftBound; }
uint64_t getRightBound() const { return this->pRightBound; }
uint64_t getavailableSize() const { return this->availableSize; }
size_t getThresholdSize() const { return this->sizeThreshold; }
using HeapAllocator::defragment;
uint64_t getFromFreedChunks(size_t size, std::vector<HeapChunk> &vec) {
size_t sizeOfFreedChunk;
return HeapAllocator::getFromFreedChunks(size, vec, sizeOfFreedChunk);
}
void storeInFreedChunks(uint64_t ptr, size_t size, std::vector<HeapChunk> &vec) { return HeapAllocator::storeInFreedChunks(ptr, size, vec); }
std::vector<HeapChunk> &getFreedChunksSmall() { return this->freedChunksSmall; };
std::vector<HeapChunk> &getFreedChunksBig() { return this->freedChunksBig; };
using HeapAllocator::allocationAlignment;
};
TEST(HeapAllocatorTest, WhenHeapAllocatorIsCreatedThenThresholdIsSet) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size);
EXPECT_NE(0u, heapAllocator->getThresholdSize());
EXPECT_EQ(MemoryConstants::pageSize, heapAllocator->allocationAlignment);
}
TEST(HeapAllocatorTest, WhenAllocatingThenUsageStatisticsAreUpdated) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size);
EXPECT_EQ(heapAllocator->getavailableSize(), heapAllocator->getLeftSize());
EXPECT_EQ(0u, heapAllocator->getUsedSize());
EXPECT_EQ((double)0.0, heapAllocator->getUsage());
size_t ptrSize = 4096;
auto ptr = heapAllocator->allocate(ptrSize);
EXPECT_EQ(4096u, heapAllocator->getUsedSize());
EXPECT_LT((double)0.0, heapAllocator->getUsage());
heapAllocator->free(ptr, ptrSize);
}
TEST(HeapAllocatorTest, GivenExactSizeChunkInFreedChunksWhenGetIsCalledThenChunkIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrFreed = 0x101000llu;
size_t sizeFreed = MemoryConstants::pageSize * 2;
freedChunks.emplace_back(ptrFreed, sizeFreed);
auto ptrReturned = heapAllocator->getFromFreedChunks(sizeFreed, freedChunks);
EXPECT_EQ(ptrFreed, ptrReturned); // ptr returned is the one that was stored
EXPECT_EQ(0u, freedChunks.size()); // entry in freed container is removed
}
TEST(HeapAllocatorTest, GivenOnlySmallerSizeChunksInFreedChunksWhenGetIsCalledThenNullptrIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
freedChunks.emplace_back(0x100000llu, 4096);
freedChunks.emplace_back(0x101000llu, 4096);
freedChunks.emplace_back(0x105000llu, 4096);
freedChunks.emplace_back(0x104000llu, 4096);
freedChunks.emplace_back(0x102000llu, 8192);
freedChunks.emplace_back(0x109000llu, 8192);
freedChunks.emplace_back(0x107000llu, 4096);
EXPECT_EQ(7u, freedChunks.size());
auto ptrReturned = heapAllocator->getFromFreedChunks(4 * 4096, freedChunks);
EXPECT_EQ(0llu, ptrReturned);
EXPECT_EQ(7u, freedChunks.size());
}
TEST(HeapAllocatorTest, GivenOnlyBiggerSizeChunksInFreedChunksWhenGetIsCalledThenBestFitChunkIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pUpperBound = ptrBase + size;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrExpected = 0llu;
pUpperBound -= 4096;
freedChunks.emplace_back(pUpperBound, 4096);
pUpperBound -= 5 * 4096;
freedChunks.emplace_back(pUpperBound, 5 * 4096);
pUpperBound -= 4 * 4096;
freedChunks.emplace_back(pUpperBound, 4 * 4096);
ptrExpected = pUpperBound;
pUpperBound -= 5 * 4096;
freedChunks.emplace_back(pUpperBound, 5 * 4096);
pUpperBound -= 4 * 4096;
freedChunks.emplace_back(pUpperBound, 4 * 4096);
EXPECT_EQ(5u, freedChunks.size());
auto ptrReturned = heapAllocator->getFromFreedChunks(3 * 4096, freedChunks);
EXPECT_EQ(ptrExpected, ptrReturned);
EXPECT_EQ(4u, freedChunks.size());
}
TEST(HeapAllocatorTest, GivenOnlyMoreThanTwiceBiggerSizeChunksInFreedChunksWhenGetIsCalledThenSplittedChunkIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrExpected = 0llu;
size_t requestedSize = 3 * 4096;
freedChunks.emplace_back(pLowerBound, 4096);
pLowerBound += 4096;
freedChunks.emplace_back(pLowerBound, 9 * 4096);
pLowerBound += 9 * 4096;
freedChunks.emplace_back(pLowerBound, 7 * 4096);
size_t deltaSize = 7 * 4096 - requestedSize;
ptrExpected = pLowerBound + deltaSize;
EXPECT_EQ(3u, freedChunks.size());
auto ptrReturned = heapAllocator->getFromFreedChunks(requestedSize, freedChunks);
EXPECT_EQ(ptrExpected, ptrReturned);
EXPECT_EQ(3u, freedChunks.size());
EXPECT_EQ(pLowerBound, freedChunks[2].ptr);
EXPECT_EQ(deltaSize, freedChunks[2].size);
}
TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrExpected = 0llu;
size_t expectedSize = 9 * 4096;
freedChunks.emplace_back(pLowerBound, 4096);
pLowerBound += 4096;
freedChunks.emplace_back(pLowerBound, 9 * 4096);
ptrExpected = pLowerBound;
pLowerBound += 9 * 4096;
EXPECT_EQ(ptrExpected, freedChunks[1].ptr);
EXPECT_EQ(expectedSize, freedChunks[1].size);
EXPECT_EQ(2u, freedChunks.size());
auto ptrToStore = pLowerBound;
size_t sizeToStore = 2 * 4096;
expectedSize += sizeToStore;
heapAllocator->storeInFreedChunks(ptrToStore, sizeToStore, freedChunks);
EXPECT_EQ(2u, freedChunks.size());
EXPECT_EQ(ptrExpected, freedChunks[1].ptr);
EXPECT_EQ(expectedSize, freedChunks[1].size);
}
TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToRightBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
uint64_t ptrExpected = 0llu;
size_t expectedSize = 9 * 4096;
freedChunks.emplace_back(pLowerBound, 4096);
pLowerBound += 4096;
pLowerBound += 4096; // space between stored chunk and chunk to store
auto ptrToStore = pLowerBound;
size_t sizeToStore = 2 * 4096;
pLowerBound += sizeToStore;
freedChunks.emplace_back(pLowerBound, 9 * 4096);
ptrExpected = pLowerBound;
EXPECT_EQ(ptrExpected, freedChunks[1].ptr);
EXPECT_EQ(expectedSize, freedChunks[1].size);
EXPECT_EQ(2u, freedChunks.size());
expectedSize += sizeToStore;
ptrExpected = ptrToStore;
heapAllocator->storeInFreedChunks(ptrToStore, sizeToStore, freedChunks);
EXPECT_EQ(2u, freedChunks.size());
EXPECT_EQ(ptrExpected, freedChunks[1].ptr);
EXPECT_EQ(expectedSize, freedChunks[1].size);
}
TEST(HeapAllocatorTest, GivenStoredChunkNotAdjacentToIncomingChunkWhenStoreIsCalledThenNewFreeChunkIsCreated) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
std::vector<HeapChunk> freedChunks;
freedChunks.emplace_back(pLowerBound, 4096);
pLowerBound += 4096;
freedChunks.emplace_back(pLowerBound, 9 * 4096);
pLowerBound += 9 * 4096;
pLowerBound += 9 * 4096;
uint64_t ptrToStore = pLowerBound;
size_t sizeToStore = 4096;
EXPECT_EQ(2u, freedChunks.size());
heapAllocator->storeInFreedChunks(ptrToStore, sizeToStore, freedChunks);
EXPECT_EQ(3u, freedChunks.size());
EXPECT_EQ(ptrToStore, freedChunks[2].ptr);
EXPECT_EQ(sizeToStore, freedChunks[2].size);
}
TEST(HeapAllocatorTest, WhenAllocatingThenEntryIsAddedToMap) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize = 4096;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr);
EXPECT_LE(ptrBase, ptr);
size_t ptrSize2 = sizeThreshold + 4096;
ptr = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr);
EXPECT_LE(ptrBase, ptr);
}
TEST(HeapAllocatorTest, WhenFreeingThenEntryIsRemovedFromMapAndSpaceMadeAvailable) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024u * 4096u;
auto pLeftBound = ptrBase;
auto pRightBound = pLeftBound + size;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize = 4096;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr);
EXPECT_LE(ptrBase, ptr);
size_t ptrSize2 = sizeThreshold + 4096;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound + sizeThreshold + 4096u);
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound - 4096u);
EXPECT_EQ(heapAllocator->getavailableSize(), size - (sizeThreshold + 4096u) - 4096u);
heapAllocator->free(ptr, ptrSize);
heapAllocator->free(ptr2, ptrSize2);
EXPECT_EQ(heapAllocator->getavailableSize(), size);
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound);
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound);
}
TEST(HeapAllocatorTest, WhenAllocatingMultipleThenEachAllocationIsDistinct) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
size_t allocSize = 4096;
size_t doubleAllocSize = 4096 * 2;
for (uint32_t i = 0u; i < 2u; i++) {
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
doubleAllocSize = allocSize * 2;
auto pLeftBound = ptrBase;
auto pRightBound = pLeftBound + size;
uint64_t ptr1 = heapAllocator->allocate(allocSize);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
uint64_t ptr2 = heapAllocator->allocate(allocSize);
EXPECT_NE(0llu, ptr2);
uint64_t ptr3 = heapAllocator->allocate(doubleAllocSize);
EXPECT_NE(0llu, ptr3);
uint64_t ptr4 = heapAllocator->allocate(allocSize);
EXPECT_NE(0llu, ptr4);
EXPECT_NE(ptr1, ptr2);
EXPECT_NE(ptr1, ptr3);
EXPECT_NE(ptr1, ptr4);
EXPECT_NE(ptr2, ptr3);
EXPECT_NE(ptr2, ptr4);
EXPECT_NE(ptr3, ptr4);
size_t totalAllocationSize = 3 * allocSize + 2 * allocSize;
EXPECT_LE(heapAllocator->getavailableSize(), size - totalAllocationSize);
if (i == 0u) {
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound - totalAllocationSize);
} else if (i == 1u) {
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound + totalAllocationSize);
}
allocSize += sizeThreshold;
}
}
TEST(HeapAllocatorTest, GivenNoSpaceLeftWhenAllocatingThenZeroIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize = 4096;
uint64_t ptr1 = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
size_t ptrSize2 = 1023 * 4096;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr2);
EXPECT_EQ(heapAllocator->getLeftBound(), heapAllocator->getRightBound());
EXPECT_EQ(0u, heapAllocator->getavailableSize());
size_t ptrSize3 = 8192;
uint64_t ptr3 = heapAllocator->allocate(ptrSize3);
EXPECT_EQ(0llu, ptr3);
}
TEST(HeapAllocatorTest, GivenReverseOrderWhenFreeingThenHeapAllocatorStateIsCorrect) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
auto pLeftBound = ptrBase;
auto pRightBound = pLeftBound + size;
size_t ptr1Size = 4096;
uint64_t ptr1 = heapAllocator->allocate(ptr1Size);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
size_t ptrSize2 = sizeThreshold + 4096;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr2);
size_t ptrSize3 = 8192;
uint64_t ptr3 = heapAllocator->allocate(ptrSize3);
EXPECT_NE(0llu, ptr3);
heapAllocator->free(ptr3, ptrSize3);
heapAllocator->free(ptr2, ptrSize2);
heapAllocator->free(ptr1, ptr1Size);
EXPECT_EQ(heapAllocator->getavailableSize(), size);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound);
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound);
}
TEST(HeapAllocatorTest, GivenNoMemoryLeftWhenAllocatingThenZeroIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 0;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize = 4096;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_EQ(0llu, ptr);
EXPECT_EQ(0u, heapAllocator->getavailableSize());
}
TEST(HeapAllocatorTest, GivenSizeGreaterThanMemoryLeftWhenAllocatingThenZeroIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 11 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, 3 * 4096);
size_t remainingSize = size;
// first small succeeds
size_t ptrSize = 4096;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr);
// second small suceeds
size_t ptrSize1 = 4096;
uint64_t ptr1 = heapAllocator->allocate(ptrSize1);
// free first to store on free list
heapAllocator->free(ptr, ptrSize);
remainingSize -= 4096;
EXPECT_NE(0llu, ptr1);
EXPECT_EQ(remainingSize, heapAllocator->getavailableSize());
// first big succeeds
size_t ptrSize2 = 4 * 4096;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr2);
// second big succeeds
size_t ptrSize3 = 4 * 4096;
uint64_t ptr3 = heapAllocator->allocate(ptrSize3);
EXPECT_NE(0llu, ptr3);
// free first big to store on free list
heapAllocator->free(ptr2, 4 * 4096);
remainingSize -= 4 * 4096;
EXPECT_EQ(remainingSize, heapAllocator->getavailableSize());
// third small fails
size_t ptrSize4 = 2 * 4096;
uint64_t ptr4 = heapAllocator->allocate(ptrSize4);
EXPECT_EQ(0llu, ptr4);
// third big fails
size_t ptrSize5 = 5 * 4096;
uint64_t ptr5 = heapAllocator->allocate(ptrSize5);
EXPECT_EQ(0llu, ptr5);
}
TEST(HeapAllocatorTest, GivenNullWhenFreeingThenNothingHappens) {
uint64_t ptrBase = 0x100000llu;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, sizeThreshold, sizeThreshold);
heapAllocator->free(0llu, 0);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
}
TEST(HeapAllocatorTest, WhenFreeingThenMemoryAvailableForAllocation) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
auto pLeftBound = ptrBase;
auto pRightBound = pLeftBound + size;
size_t ptrSize = 8192;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr);
EXPECT_LE(ptrBase, ptr);
size_t ptrSize1 = 8192;
uint64_t ptr1 = heapAllocator->allocate(ptrSize1);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
size_t ptrSize2 = 8192;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr2);
heapAllocator->free(ptr1, ptrSize1);
EXPECT_EQ(1u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
size_t ptrSize3 = 8192;
uint64_t ptr3 = heapAllocator->allocate(ptrSize3);
EXPECT_NE(0llu, ptr3);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
heapAllocator->free(ptr2, ptrSize2);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
heapAllocator->free(ptr3, ptrSize3);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
heapAllocator->free(ptr, ptrSize);
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound);
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound);
}
TEST(HeapAllocatorTest, WhenFreeingChunkThenMemoryAvailableForAllocation) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
auto pLeftBound = ptrBase;
auto pRightBound = pLeftBound + size;
size_t sizeAllocated = 0;
size_t ptrSize = 8192;
uint64_t ptr = heapAllocator->allocate(ptrSize);
EXPECT_NE(0llu, ptr);
EXPECT_LE(ptrBase, ptr);
sizeAllocated += 8192;
size_t ptrSize1 = 4 * 4096;
uint64_t ptr1 = heapAllocator->allocate(ptrSize1);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
sizeAllocated += 4 * 4096;
size_t ptrSize2 = 8192;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_NE(0llu, ptr2);
sizeAllocated += 8192;
EXPECT_EQ(size - sizeAllocated, heapAllocator->getavailableSize());
heapAllocator->free(ptr1, ptrSize1);
sizeAllocated -= 4 * 4096;
EXPECT_EQ(size - sizeAllocated, heapAllocator->getavailableSize());
EXPECT_EQ(1u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
size_t ptrSize3 = 3 * 4096;
uint64_t ptr3 = heapAllocator->allocate(ptrSize3);
EXPECT_NE(0llu, ptr3);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
sizeAllocated += 4 * 4096; // 4*4096 because this was chunk that was stored on free list
EXPECT_EQ(size - sizeAllocated, heapAllocator->getavailableSize());
heapAllocator->free(ptr2, ptrSize2);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
heapAllocator->free(ptr3, ptrSize3);
EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size());
EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size());
heapAllocator->free(ptr, ptrSize);
EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound);
EXPECT_EQ(heapAllocator->getRightBound(), pRightBound);
EXPECT_EQ(size, heapAllocator->getavailableSize());
}
TEST(HeapAllocatorTest, GivenSmallAllocationGreaterThanAvailableSizeWhenAllocatingThenZeroIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize1 = size - 4096;
uint64_t ptr1 = heapAllocator->allocate(ptrSize1);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
EXPECT_EQ(4096u, heapAllocator->getavailableSize());
size_t ptrSize2 = 8192;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_EQ(0llu, ptr2);
}
TEST(HeapAllocatorTest, GivenBigAllocationGreaterThanAvailableSizeWhenAllocatingThenZeroIsReturned) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, sizeThreshold);
size_t ptrSize1 = 8192;
uint64_t ptr1 = heapAllocator->allocate(ptrSize1);
EXPECT_NE(0llu, ptr1);
EXPECT_LE(ptrBase, ptr1);
EXPECT_EQ(size - 8192u, heapAllocator->getavailableSize());
size_t ptrSize2 = size - 4096;
uint64_t ptr2 = heapAllocator->allocate(ptrSize2);
EXPECT_EQ(0llu, ptr2);
}
TEST(HeapAllocatorTest, WhenMemoryIsAllocatedThenAllocationsDoNotOverlap) {
std::ranlux24 generator(1);
const uint32_t maxIndex = 2000;
std::unique_ptr<uint64_t[]> ptrs(new uint64_t[maxIndex]);
std::unique_ptr<size_t[]> sizes(new size_t[maxIndex]);
memset(ptrs.get(), 0, sizeof(uint64_t) * maxIndex);
memset(sizes.get(), 0, sizeof(size_t) * maxIndex);
uint16_t *freeIndexes = new uint16_t[maxIndex];
std::unique_ptr<uint16_t[]> indexes(new uint16_t[maxIndex]);
memset(freeIndexes, 0, sizeof(uint16_t) * maxIndex);
memset(indexes.get(), 0, sizeof(uint16_t) * maxIndex);
// Generate random unique indexes
for (uint32_t i = 0; i < maxIndex; i++) {
uint16_t index = (generator() + 1) % maxIndex;
if (freeIndexes[index] == 0) {
indexes[i] = index;
freeIndexes[index] = 1;
}
}
delete[] freeIndexes;
uint64_t allocatorSize = 1024llu * 1024llu; // 1 MB
void *pBasePtr = alignedMalloc(static_cast<size_t>(allocatorSize), 4096);
uint64_t basePtr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(pBasePtr));
constexpr size_t reqAlignment = 4;
size_t bigAllocationThreshold = (512 + 256) * reqAlignment;
memset(pBasePtr, 0, static_cast<size_t>(allocatorSize));
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(basePtr, allocatorSize, bigAllocationThreshold);
heapAllocator->allocationAlignment = reqAlignment;
for (uint32_t i = 0; i < maxIndex; i++) {
if (indexes[i] != 0) {
size_t sizeToAllocate = (indexes[i] % 1024) * reqAlignment;
ASSERT_LT(sizeToAllocate, allocatorSize);
sizes[i] = sizeToAllocate;
ptrs[i] = heapAllocator->allocate(sizes[i]);
if (ptrs[i] == 0llu)
break;
uint8_t *pTemp = reinterpret_cast<uint8_t *>(ptrs[i]);
for (uint32_t j = 0; j < sizes[i] / 4096; j++) {
*pTemp += 1;
pTemp += 4096;
}
uint32_t indexToFree = indexes[i] % (i * 2 + 1);
if (ptrs[indexToFree]) {
memset(reinterpret_cast<void *>(ptrs[indexToFree]), 0, sizes[indexToFree]);
heapAllocator->free(ptrs[indexToFree], sizes[indexToFree]);
ptrs[indexToFree] = 0llu;
sizes[indexToFree] = 0;
}
}
}
uint8_t *pTemp = reinterpret_cast<uint8_t *>(pBasePtr);
for (uint32_t i = 0; i < allocatorSize / reqAlignment; i++) {
if (*pTemp > 1) {
EXPECT_TRUE(false) << "Heap from Allocator corrupted at page offset " << i << std::endl;
}
}
for (uint32_t i = 0; i < maxIndex; i++) {
if (ptrs[i] != 0) {
heapAllocator->free(ptrs[i], sizes[i]);
}
}
//at this point we should be able to allocate full size
size_t totalSize = (size_t)(allocatorSize - reqAlignment);
auto finalPtr = heapAllocator->allocate(totalSize);
EXPECT_NE(0llu, finalPtr);
heapAllocator->free(finalPtr, totalSize);
alignedFree(pBasePtr);
}
TEST(HeapAllocatorTest, GivenLargeAllocationsWhenFreeingThenSpaceIsDefragmented) {
uint64_t ptrBase = 0x100000llu;
uint64_t basePtr = 0x100000llu;
size_t size = 1024 * 4096;
size_t threshold = 4096;
size_t allocSize = 2 * MemoryConstants::pageSize;
size_t doubleallocSize = 2 * allocSize;
size_t tripleallocSize = 3 * allocSize;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, threshold);
std::vector<HeapChunk> &freedChunks = heapAllocator->getFreedChunksBig();
// 0, 1, 2 - can be merged to one
// 6,7,8,10 - can be merged to one
uint64_t ptrs[12];
ptrs[0] = heapAllocator->allocate(allocSize);
ptrs[1] = heapAllocator->allocate(allocSize);
ptrs[2] = heapAllocator->allocate(allocSize);
ptrs[3] = heapAllocator->allocate(tripleallocSize);
ptrs[4] = 0llu;
ptrs[5] = 0llu;
ptrs[6] = heapAllocator->allocate(allocSize);
ptrs[7] = heapAllocator->allocate(allocSize);
ptrs[8] = heapAllocator->allocate(doubleallocSize);
ptrs[9] = 0llu;
ptrs[10] = heapAllocator->allocate(allocSize);
ptrs[11] = heapAllocator->allocate(allocSize);
heapAllocator->free(ptrs[0], allocSize);
heapAllocator->free(ptrs[10], allocSize);
heapAllocator->free(ptrs[2], allocSize);
heapAllocator->free(ptrs[6], allocSize);
heapAllocator->free(ptrs[1], allocSize);
heapAllocator->free(ptrs[7], allocSize);
heapAllocator->free(ptrs[8], doubleallocSize);
// 0,1 merged on free,
// 2
// 6,7 - merged on free
// 8, 10 - merged on free
EXPECT_EQ(4u, freedChunks.size());
heapAllocator->defragment();
ASSERT_EQ(2u, freedChunks.size());
EXPECT_EQ(basePtr, freedChunks[0].ptr);
EXPECT_EQ(3 * allocSize, freedChunks[0].size);
EXPECT_EQ((basePtr + 6 * allocSize), freedChunks[1].ptr);
EXPECT_EQ(5 * allocSize, freedChunks[1].size);
}
TEST(HeapAllocatorTest, GivenSmallAllocationsWhenFreeingThenSpaceIsDefragmented) {
uint64_t ptrBase = 0x100000llu;
uint64_t basePtr = 0x100000;
size_t size = 1024 * 4096;
uint64_t upperLimitPtr = basePtr + size;
size_t threshold = 2 * MemoryConstants::pageSize;
size_t allocSize = MemoryConstants::pageSize;
size_t doubleallocSize = 2 * allocSize;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, threshold);
std::vector<HeapChunk> &freedChunks = heapAllocator->getFreedChunksSmall();
// 0, 1, 2 - can be merged to one
// 6,7,8,10 - can be merged to one
uint64_t ptrs[12];
ptrs[0] = heapAllocator->allocate(allocSize);
ptrs[1] = heapAllocator->allocate(allocSize);
ptrs[2] = heapAllocator->allocate(allocSize);
ptrs[3] = heapAllocator->allocate(doubleallocSize);
ptrs[4] = 0llu;
ptrs[5] = 0llu;
ptrs[6] = heapAllocator->allocate(allocSize);
ptrs[7] = heapAllocator->allocate(allocSize);
ptrs[8] = heapAllocator->allocate(doubleallocSize);
ptrs[9] = 0llu;
ptrs[10] = heapAllocator->allocate(allocSize);
ptrs[11] = heapAllocator->allocate(allocSize);
heapAllocator->free(ptrs[0], allocSize);
heapAllocator->free(ptrs[2], allocSize);
heapAllocator->free(ptrs[8], doubleallocSize);
heapAllocator->free(ptrs[1], allocSize);
heapAllocator->free(ptrs[6], allocSize);
heapAllocator->free(ptrs[7], allocSize);
heapAllocator->free(ptrs[10], allocSize);
// 0,1 merged on free,
// 2
// 6, - merged on free
// 7, 8, 10 - merged on free
EXPECT_EQ(4u, freedChunks.size());
heapAllocator->defragment();
ASSERT_EQ(2u, freedChunks.size());
EXPECT_EQ((upperLimitPtr - 3 * allocSize), freedChunks[0].ptr);
EXPECT_EQ(3 * allocSize, freedChunks[0].size);
EXPECT_EQ((upperLimitPtr - 10 * allocSize), freedChunks[1].ptr);
EXPECT_EQ(5 * allocSize, freedChunks[1].size);
}
TEST(HeapAllocatorTest, Given10SmallAllocationsWhenFreedInTheSameOrderThenLastChunkFreedReturnsWholeSpaceToFreeRange) {
uint64_t ptrBase = 0llu;
size_t size = 1024 * 4096;
size_t threshold = 2 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, threshold);
std::vector<HeapChunk> &freedChunks = heapAllocator->getFreedChunksSmall();
uint64_t ptrs[10];
size_t sizes[10];
for (uint32_t i = 0; i < 10; i++) {
sizes[i] = 4096;
ptrs[i] = heapAllocator->allocate(sizes[i]);
}
EXPECT_EQ(0u, freedChunks.size());
for (uint32_t i = 0; i < 10; i++) {
heapAllocator->free(ptrs[i], sizes[i]);
// After free chunk gets merged to existing one on freed list
if (i < 9) {
EXPECT_EQ(1u, freedChunks.size());
}
}
// Last chunk released merges freed chunk to free range
EXPECT_EQ(0u, freedChunks.size());
}
TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallSplittedAndReleasedThenItDoesNotGoToFreedBigChunksList) {
uint64_t ptrBase = 0llu;
uintptr_t basePtr = 0;
// Size for 10 small allocs plus one single 2 page plus some space
size_t size = (10 + 2 + 1) * 4096;
uintptr_t upperLimitPtr = basePtr + size;
size_t threshold = 4 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, threshold);
std::vector<HeapChunk> &freedChunksSmall = heapAllocator->getFreedChunksSmall();
std::vector<HeapChunk> &freedChunksBig = heapAllocator->getFreedChunksBig();
uint64_t ptrs[10];
size_t sizes[10];
// size smaller than threshold
size_t sizeOfSmallAlloc = 2 * 4096;
uint64_t smallAlloc = 0llu;
for (uint32_t i = 0; i < 10; i++) {
sizes[i] = 4096;
ptrs[i] = heapAllocator->allocate(sizes[i]);
}
EXPECT_EQ(0u, freedChunksSmall.size());
EXPECT_EQ(0u, freedChunksBig.size());
// Release 8 chunks
for (uint32_t i = 0; i < 8; i++) {
heapAllocator->free(ptrs[i], sizes[i]);
}
// Allocate small chunk, should be taken from freed list
smallAlloc = heapAllocator->allocate(sizeOfSmallAlloc);
EXPECT_NE(0llu, smallAlloc);
EXPECT_LE(upperLimitPtr - (8 * 4096), smallAlloc);
EXPECT_EQ(1u, freedChunksSmall.size());
heapAllocator->free(smallAlloc, sizeOfSmallAlloc);
// It should not go to freedBig list
EXPECT_EQ(0u, freedChunksBig.size());
// It should merge to freedSmall chunk
EXPECT_EQ(1u, freedChunksSmall.size());
// Release last 2 allocs
for (uint32_t i = 8; i < 10; i++) {
heapAllocator->free(ptrs[i], sizes[i]);
}
// In the end both lists should be empty
EXPECT_EQ(0u, freedChunksSmall.size());
EXPECT_EQ(0u, freedChunksBig.size());
}
TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallNotSplittedAndReleasedThenItDoesNotGoToFreedBigChunksList) {
uint64_t ptrBase = 0llu;
uintptr_t basePtr = 0;
// Size for 10 small allocs plus one single 3 page plus some space
size_t size = (10 + 3 + 1) * 4096;
uint64_t upperLimitPtr = basePtr + size;
size_t threshold = 4 * 4096;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, threshold);
std::vector<HeapChunk> &freedChunksSmall = heapAllocator->getFreedChunksSmall();
std::vector<HeapChunk> &freedChunksBig = heapAllocator->getFreedChunksBig();
uint64_t ptrs[10];
size_t sizes[10];
// size smaller than threshold
size_t sizeOfSmallAlloc = 3 * 4096;
uint64_t smallAlloc = 0llu;
for (uint32_t i = 0; i < 10; i++) {
sizes[i] = 4096;
ptrs[i] = heapAllocator->allocate(sizes[i]);
}
EXPECT_EQ(0u, freedChunksSmall.size());
EXPECT_EQ(0u, freedChunksBig.size());
// Release 8 chunks
for (uint32_t i = 0; i < 5; i++) {
heapAllocator->free(ptrs[i], sizes[i]);
}
// Allocate small chunk, should be taken from freed list
smallAlloc = heapAllocator->allocate(sizeOfSmallAlloc);
EXPECT_NE(0llu, smallAlloc);
EXPECT_LE(upperLimitPtr - (5 * 4096), smallAlloc);
EXPECT_EQ(0u, freedChunksSmall.size());
heapAllocator->free(smallAlloc, sizeOfSmallAlloc);
// It should not go to freedBig list
EXPECT_EQ(0u, freedChunksBig.size());
// It should go to freedSmall chunk
EXPECT_EQ(1u, freedChunksSmall.size());
// Release remaining allocs
for (uint32_t i = 5; i < 10; i++) {
heapAllocator->free(ptrs[i], sizes[i]);
if (i < 9) {
// chunks should be merged to freedSmall chunk on list
EXPECT_EQ(1u, freedChunksSmall.size());
}
}
// In the end both lists should be empty
EXPECT_EQ(0u, freedChunksSmall.size());
EXPECT_EQ(0u, freedChunksBig.size());
}
|