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

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

Issue 169523004: Add partitionAllocGetSize() for determining actual size of allocation (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: adjust test sizes, and reword/fix 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.h ('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 556 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 567
568 // Can we free null? 568 // Can we free null?
569 partitionFreeGeneric(genericAllocator.root(), 0); 569 partitionFreeGeneric(genericAllocator.root(), 0);
570 570
571 // Do we correctly get a null for a failed allocation? 571 // Do we correctly get a null for a failed allocation?
572 EXPECT_EQ(0, partitionAllocGenericFlags(genericAllocator.root(), WTF::Partit ionAllocReturnNull, 3u * 1024 * 1024 * 1024)); 572 EXPECT_EQ(0, partitionAllocGenericFlags(genericAllocator.root(), WTF::Partit ionAllocReturnNull, 3u * 1024 * 1024 * 1024));
573 573
574 TestShutdown(); 574 TestShutdown();
575 } 575 }
576 576
577 // Test that we can fetch the real allocated size after an allocation.
578 TEST(WTF_PartitionAlloc, GenericAllocGetSize)
579 {
580 TestSetup();
581
582 void* ptr;
583 size_t requestedSize, actualSize;
584
585 EXPECT_TRUE(partitionAllocSupportsGetSize());
586
587 // Allocate something small.
588 requestedSize = 511 - kExtraAllocSize;
589 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
590 EXPECT_TRUE(ptr);
591 actualSize = partitionAllocGetSize(ptr);
592 EXPECT_LT(requestedSize, actualSize);
593 partitionFreeGeneric(genericAllocator.root(), ptr);
594
595 // Allocate a size that should be a perfect match for a bucket, because it
596 // is an exact power of 2.
597 requestedSize = (256 * 1024) - kExtraAllocSize;
598 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
599 EXPECT_TRUE(ptr);
600 actualSize = partitionAllocGetSize(ptr);
601 EXPECT_EQ(requestedSize, actualSize);
602 partitionFreeGeneric(genericAllocator.root(), ptr);
603
604 // Allocate a size that is a system page smaller than a bucket. GetSize()
605 // should return a larger size than we asked for now.
606 requestedSize = (256 * 1024) - WTF::kSystemPageSize - kExtraAllocSize;
607 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
608 EXPECT_TRUE(ptr);
609 actualSize = partitionAllocGetSize(ptr);
610 EXPECT_EQ(requestedSize + WTF::kSystemPageSize, actualSize);
611 // Check that we can write at the end of the reported size too.
612 char* charPtr = reinterpret_cast<char*>(ptr);
613 *(charPtr + (actualSize - 1)) = 'A';
614 partitionFreeGeneric(genericAllocator.root(), ptr);
615
616 // Allocate something very large, and uneven.
617 requestedSize = 512 * 1024 * 1024 - 1;
618 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize);
619 EXPECT_TRUE(ptr);
620 actualSize = partitionAllocGetSize(ptr);
621 EXPECT_LT(requestedSize, actualSize);
622 partitionFreeGeneric(genericAllocator.root(), ptr);
623
624 TestShutdown();
625 }
626
577 // Test the realloc() contract. 627 // Test the realloc() contract.
578 TEST(WTF_PartitionAlloc, Realloc) 628 TEST(WTF_PartitionAlloc, Realloc)
579 { 629 {
580 TestSetup(); 630 TestSetup();
581 631
582 // realloc(0, size) should be equivalent to malloc(). 632 // realloc(0, size) should be equivalent to malloc().
583 void* ptr = partitionReallocGeneric(genericAllocator.root(), 0, kTestAllocSi ze); 633 void* ptr = partitionReallocGeneric(genericAllocator.root(), 0, kTestAllocSi ze);
584 memset(ptr, 'A', kTestAllocSize); 634 memset(ptr, 'A', kTestAllocSize);
585 WTF::PartitionPage* page = WTF::partitionPointerToPage(WTF::partitionCookieF reePointerAdjust(ptr)); 635 WTF::PartitionPage* page = WTF::partitionPointerToPage(WTF::partitionCookieF reePointerAdjust(ptr));
586 // realloc(ptr, 0) should be equivalent to free(). 636 // realloc(ptr, 0) should be equivalent to free().
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 EXPECT_EQ(32u, WTF::countLeadingZerosSizet(0)); 1036 EXPECT_EQ(32u, WTF::countLeadingZerosSizet(0));
987 EXPECT_EQ(31u, WTF::countLeadingZerosSizet(1)); 1037 EXPECT_EQ(31u, WTF::countLeadingZerosSizet(1));
988 EXPECT_EQ(1u, WTF::countLeadingZerosSizet(1 << 30)); 1038 EXPECT_EQ(1u, WTF::countLeadingZerosSizet(1 << 30));
989 EXPECT_EQ(0u, WTF::countLeadingZerosSizet(1 << 31)); 1039 EXPECT_EQ(0u, WTF::countLeadingZerosSizet(1 << 31));
990 #endif 1040 #endif
991 } 1041 }
992 1042
993 } // namespace 1043 } // namespace
994 1044
995 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) 1045 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
OLDNEW
« no previous file with comments | « Source/wtf/PartitionAlloc.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698