Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: Source/wtf/PartitionAllocTest.cpp

Issue 172533004: Add partitionAllocActualSize() for predicting actual size of allocation (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: addressing review comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/wtf/PartitionAlloc.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 586
587 TestShutdown(); 587 TestShutdown();
588 } 588 }
589 589
590 // Test that we can fetch the real allocated size after an allocation. 590 // Test that we can fetch the real allocated size after an allocation.
591 TEST(WTF_PartitionAlloc, GenericAllocGetSize) 591 TEST(WTF_PartitionAlloc, GenericAllocGetSize)
592 { 592 {
593 TestSetup(); 593 TestSetup();
594 594
595 void* ptr; 595 void* ptr;
596 size_t requestedSize, actualSize; 596 size_t requestedSize, actualSize, predictedSize;
597 597
598 EXPECT_TRUE(partitionAllocSupportsGetSize()); 598 EXPECT_TRUE(partitionAllocSupportsGetSize());
599 599
600 // Allocate something small. 600 // Allocate something small.
601 requestedSize = 511 - kExtraAllocSize; 601 requestedSize = 511 - kExtraAllocSize;
602 predictedSize = partitionAllocActualSize(genericAllocator.root(), requestedS ize);
602 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize); 603 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
603 EXPECT_TRUE(ptr); 604 EXPECT_TRUE(ptr);
604 actualSize = partitionAllocGetSize(ptr); 605 actualSize = partitionAllocGetSize(ptr);
606 EXPECT_EQ(predictedSize, actualSize);
605 EXPECT_LT(requestedSize, actualSize); 607 EXPECT_LT(requestedSize, actualSize);
606 partitionFreeGeneric(genericAllocator.root(), ptr); 608 partitionFreeGeneric(genericAllocator.root(), ptr);
607 609
608 // Allocate a size that should be a perfect match for a bucket, because it 610 // Allocate a size that should be a perfect match for a bucket, because it
609 // is an exact power of 2. 611 // is an exact power of 2.
610 requestedSize = (256 * 1024) - kExtraAllocSize; 612 requestedSize = (256 * 1024) - kExtraAllocSize;
613 predictedSize = partitionAllocActualSize(genericAllocator.root(), requestedS ize);
611 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize); 614 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
612 EXPECT_TRUE(ptr); 615 EXPECT_TRUE(ptr);
613 actualSize = partitionAllocGetSize(ptr); 616 actualSize = partitionAllocGetSize(ptr);
617 EXPECT_EQ(predictedSize, actualSize);
614 EXPECT_EQ(requestedSize, actualSize); 618 EXPECT_EQ(requestedSize, actualSize);
615 partitionFreeGeneric(genericAllocator.root(), ptr); 619 partitionFreeGeneric(genericAllocator.root(), ptr);
616 620
617 // Allocate a size that is a system page smaller than a bucket. GetSize() 621 // Allocate a size that is a system page smaller than a bucket. GetSize()
618 // should return a larger size than we asked for now. 622 // should return a larger size than we asked for now.
619 requestedSize = (256 * 1024) - WTF::kSystemPageSize - kExtraAllocSize; 623 requestedSize = (256 * 1024) - WTF::kSystemPageSize - kExtraAllocSize;
624 predictedSize = partitionAllocActualSize(genericAllocator.root(), requestedS ize);
620 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize); 625 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
621 EXPECT_TRUE(ptr); 626 EXPECT_TRUE(ptr);
622 actualSize = partitionAllocGetSize(ptr); 627 actualSize = partitionAllocGetSize(ptr);
628 EXPECT_EQ(predictedSize, actualSize);
623 EXPECT_EQ(requestedSize + WTF::kSystemPageSize, actualSize); 629 EXPECT_EQ(requestedSize + WTF::kSystemPageSize, actualSize);
624 // Check that we can write at the end of the reported size too. 630 // Check that we can write at the end of the reported size too.
625 char* charPtr = reinterpret_cast<char*>(ptr); 631 char* charPtr = reinterpret_cast<char*>(ptr);
626 *(charPtr + (actualSize - 1)) = 'A'; 632 *(charPtr + (actualSize - 1)) = 'A';
627 partitionFreeGeneric(genericAllocator.root(), ptr); 633 partitionFreeGeneric(genericAllocator.root(), ptr);
628 634
629 // Allocate something very large, and uneven. 635 // Allocate something very large, and uneven.
630 requestedSize = 512 * 1024 * 1024 - 1; 636 requestedSize = 512 * 1024 * 1024 - 1;
637 predictedSize = partitionAllocActualSize(genericAllocator.root(), requestedS ize);
631 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize); 638 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
632 EXPECT_TRUE(ptr); 639 EXPECT_TRUE(ptr);
633 actualSize = partitionAllocGetSize(ptr); 640 actualSize = partitionAllocGetSize(ptr);
641 EXPECT_EQ(predictedSize, actualSize);
634 EXPECT_LT(requestedSize, actualSize); 642 EXPECT_LT(requestedSize, actualSize);
635 partitionFreeGeneric(genericAllocator.root(), ptr); 643 partitionFreeGeneric(genericAllocator.root(), ptr);
636 644
645 // Too large allocation.
646 requestedSize = INT_MAX;
647 predictedSize = partitionAllocActualSize(genericAllocator.root(), requestedS ize);
648 EXPECT_EQ(requestedSize, predictedSize);
649
637 TestShutdown(); 650 TestShutdown();
638 } 651 }
639 652
640 // Test the realloc() contract. 653 // Test the realloc() contract.
641 TEST(WTF_PartitionAlloc, Realloc) 654 TEST(WTF_PartitionAlloc, Realloc)
642 { 655 {
643 TestSetup(); 656 TestSetup();
644 657
645 // realloc(0, size) should be equivalent to malloc(). 658 // realloc(0, size) should be equivalent to malloc().
646 void* ptr = partitionReallocGeneric(genericAllocator.root(), 0, kTestAllocSi ze); 659 void* ptr = partitionReallocGeneric(genericAllocator.root(), 0, kTestAllocSi ze);
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
1111 EXPECT_EQ(32u, WTF::countLeadingZerosSizet(0)); 1124 EXPECT_EQ(32u, WTF::countLeadingZerosSizet(0));
1112 EXPECT_EQ(31u, WTF::countLeadingZerosSizet(1)); 1125 EXPECT_EQ(31u, WTF::countLeadingZerosSizet(1));
1113 EXPECT_EQ(1u, WTF::countLeadingZerosSizet(1 << 30)); 1126 EXPECT_EQ(1u, WTF::countLeadingZerosSizet(1 << 30));
1114 EXPECT_EQ(0u, WTF::countLeadingZerosSizet(1 << 31)); 1127 EXPECT_EQ(0u, WTF::countLeadingZerosSizet(1 << 31));
1115 #endif 1128 #endif
1116 } 1129 }
1117 1130
1118 } // namespace 1131 } // namespace
1119 1132
1120 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 1133 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
OLDNEW
« no previous file with comments | « Source/wtf/PartitionAlloc.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698