Commit 528a3ca5 authored by Bartek Nowierski's avatar Bartek Nowierski Committed by Commit Bot

Combine tag&cookie adjustment functions into one.

Improve readability of AllocFromBucket while at it.

Bug: 1092288
Change-Id: I58d6096cdeb98e36dc65db2aeb3c4b3465589736
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2277659
Auto-Submit: Bartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarBenoit L <lizeb@chromium.org>
Commit-Queue: Bartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784459}
parent d8f55c90
......@@ -302,8 +302,7 @@ bool PartitionRoot<thread_safe>::ReallocDirectMappedInPlace(
size_t raw_size) {
PA_DCHECK(page->bucket->is_direct_mapped());
raw_size = internal::PartitionCookieSizeAdjustAdd(raw_size);
raw_size = internal::PartitionTagSizeAdjustAdd(raw_size);
raw_size = internal::PartitionSizeAdjustAdd(raw_size);
// Note that the new size might be a bucketed size; this function is called
// whenever we're reallocating a direct mapped allocation.
......@@ -389,8 +388,7 @@ void* PartitionRoot<thread_safe>::ReallocFlags(int flags,
&actual_old_size, ptr);
}
if (LIKELY(!overridden)) {
auto* page = Page::FromPointer(internal::PartitionTagFreePointerAdjust(
internal::PartitionCookieFreePointerAdjust(ptr)));
auto* page = Page::FromPointer(internal::PartitionFreePointerAdjust(ptr));
bool success = false;
{
internal::ScopedGuard<thread_safe> guard{lock_};
......@@ -422,8 +420,7 @@ void* PartitionRoot<thread_safe>::ReallocFlags(int flags,
// Trying to allocate a block of size |new_size| would give us a block of
// the same size as the one we've already got, so re-use the allocation
// after updating statistics (and cookies, if present).
size_t new_raw_size = internal::PartitionCookieSizeAdjustAdd(new_size);
new_raw_size = internal::PartitionTagSizeAdjustAdd(new_raw_size);
size_t new_raw_size = internal::PartitionSizeAdjustAdd(new_size);
page->set_raw_size(new_raw_size);
#if DCHECK_IS_ON()
// Write a new trailing cookie when it is possible to keep track of
......
......@@ -167,6 +167,24 @@ class BASE_EXPORT PartitionAllocHooks {
namespace internal {
ALWAYS_INLINE void* PartitionFreePointerAdjust(void* ptr) {
ptr = PartitionTagFreePointerAdjust(ptr);
ptr = PartitionCookieFreePointerAdjust(ptr);
return ptr;
}
ALWAYS_INLINE size_t PartitionSizeAdjustAdd(size_t size) {
size = PartitionTagSizeAdjustAdd(size);
size = PartitionCookieSizeAdjustAdd(size);
return size;
}
ALWAYS_INLINE size_t PartitionSizeAdjustSubtract(size_t size) {
size = PartitionTagSizeAdjustSubtract(size);
size = PartitionCookieSizeAdjustSubtract(size);
return size;
}
template <bool thread_safe>
class LOCKABLE MaybeSpinLock {
public:
......@@ -517,21 +535,22 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFromBucket(Bucket* bucket,
// c: new_slot_size
// Note, empty space occurs if the slot size is larger than needed to
// accommodate the request.
size_t size_with_no_extras = internal::PartitionTagSizeAdjustSubtract(
internal::PartitionCookieSizeAdjustSubtract(new_slot_size));
char* char_ret = static_cast<char*>(ret) + internal::kPartitionTagSize;
// The value given to the application is actually just after the cookie.
ret = char_ret + internal::kCookieSize;
// Fill the region kUninitializedByte or 0, and surround it with 2 cookies.
internal::PartitionCookieWriteValue(char_ret);
char* char_ret = static_cast<char*>(ret);
size_t size_with_no_extras =
internal::PartitionSizeAdjustSubtract(new_slot_size);
// The value given to the application is just after the tag and cookie.
char_ret += internal::kPartitionTagSize + internal::kCookieSize;
ret = char_ret;
// Surround the region with 2 cookies.
internal::PartitionCookieWriteValue(char_ret - internal::kCookieSize);
internal::PartitionCookieWriteValue(char_ret + size_with_no_extras);
// Fill the region kUninitializedByte or 0.
if (!zero_fill) {
memset(ret, kUninitializedByte, size_with_no_extras);
} else if (!is_already_zeroed) {
memset(ret, 0, size_with_no_extras);
}
internal::PartitionCookieWriteValue(char_ret + internal::kCookieSize +
size_with_no_extras);
#else
if (!ret)
return nullptr;
......@@ -566,8 +585,7 @@ ALWAYS_INLINE void PartitionRoot<thread_safe>::Free(void* ptr) {
// TODO(tasak): clear partition tag. Temporarily set the tag to be 0.
internal::PartitionTagSetValue(ptr, 0);
ptr = internal::PartitionCookieFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
ptr = internal::PartitionFreePointerAdjust(ptr);
Page* page = Page::FromPointer(ptr);
// TODO(palmer): See if we can afford to make this a CHECK.
PA_DCHECK(IsValidPage(page));
......@@ -687,12 +705,9 @@ PartitionAllocGetPageForSize(void* ptr) {
// partition page.
template <bool thread_safe>
ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) {
ptr = internal::PartitionCookieFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
ptr = internal::PartitionFreePointerAdjust(ptr);
auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr);
size_t size =
internal::PartitionCookieSizeAdjustSubtract(page->bucket->slot_size);
size = internal::PartitionTagSizeAdjustSubtract(size);
size_t size = internal::PartitionSizeAdjustSubtract(page->bucket->slot_size);
return size;
}
......@@ -703,8 +718,7 @@ ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) {
template <bool thread_safe>
ALWAYS_INLINE size_t PartitionAllocGetSlotOffset(void* ptr) {
PA_DCHECK(IsManagedByPartitionAllocAndNotDirectMapped(ptr));
ptr = internal::PartitionCookieFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
ptr = internal::PartitionFreePointerAdjust(ptr);
auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr);
size_t slot_size = page->bucket->slot_size;
......@@ -763,8 +777,7 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFlags(
}
}
size_t requested_size = size;
size = internal::PartitionCookieSizeAdjustAdd(size);
size = internal::PartitionTagSizeAdjustAdd(size);
size = internal::PartitionSizeAdjustAdd(size);
#if ENABLE_CHECKED_PTR
PA_CHECK(size >= requested_size);
#endif
......@@ -810,8 +823,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) {
return size;
#else
PA_DCHECK(PartitionRoot<thread_safe>::initialized);
size = internal::PartitionCookieSizeAdjustAdd(size);
size = internal::PartitionTagSizeAdjustAdd(size);
size = internal::PartitionSizeAdjustAdd(size);
auto* bucket = SizeToBucket(size);
if (LIKELY(!bucket->is_direct_mapped())) {
size = bucket->slot_size;
......@@ -820,8 +832,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) {
} else {
size = Bucket::get_direct_map_size(size);
}
size = internal::PartitionCookieSizeAdjustSubtract(size);
size = internal::PartitionTagSizeAdjustSubtract(size);
size = internal::PartitionSizeAdjustSubtract(size);
return size;
#endif
}
......@@ -845,6 +856,7 @@ struct BASE_EXPORT PartitionAllocator {
private:
PartitionRoot<thread_safe> partition_root_;
};
} // namespace internal
using PartitionAllocator = internal::PartitionAllocator<internal::ThreadSafe>;
......
......@@ -180,11 +180,9 @@ class PartitionAllocTest : public testing::Test {
void* ptr = allocator.root()->Alloc(size, type_name);
EXPECT_TRUE(ptr);
if (!i)
first = PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr));
first = PartitionFreePointerAdjust(ptr);
else if (i == num_slots - 1)
last = PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr));
last = PartitionFreePointerAdjust(ptr);
}
EXPECT_EQ(PartitionRoot<ThreadSafe>::Page::FromPointer(first),
PartitionRoot<ThreadSafe>::Page::FromPointer(last));
......@@ -205,7 +203,7 @@ class PartitionAllocTest : public testing::Test {
for (size_t i = 0; i < kMaxFreeableSpans; ++i) {
void* ptr = allocator.root()->Alloc(size, type_name);
auto* page = PartitionRoot<base::internal::ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
auto* bucket = page->bucket;
EXPECT_EQ(1, bucket->active_pages_head->num_allocated_slots);
allocator.root()->Free(ptr);
......@@ -739,7 +737,7 @@ TEST_F(PartitionAllocTest, GenericAllocSizes) {
// Should be freeable at this point.
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_NE(-1, page->empty_cache_index);
allocator.root()->Free(ptr2);
......@@ -758,11 +756,10 @@ TEST_F(PartitionAllocTest, GenericAllocSizes) {
EXPECT_TRUE(ptr4);
page = PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
PartitionRoot<ThreadSafe>::Page* page2 =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr3)));
PartitionFreePointerAdjust(ptr3));
EXPECT_NE(page, page2);
allocator.root()->Free(ptr);
......@@ -958,13 +955,11 @@ TEST_F(PartitionAllocTest, Realloc) {
memset(ptr, 'A', kTestAllocSize);
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
// realloc(ptr, 0) should be equivalent to free().
void* ptr2 = allocator.root()->Realloc(ptr, 0, type_name);
EXPECT_EQ(nullptr, ptr2);
EXPECT_EQ(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)),
page->freelist_head);
EXPECT_EQ(PartitionFreePointerAdjust(ptr), page->freelist_head);
// Test that growing an allocation with realloc() copies everything from the
// old allocation.
......@@ -1032,7 +1027,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
size_t total_slots =
(page->bucket->num_system_pages_per_slot_span * kSystemPageSize) /
(big_size + kExtraAllocSize);
......@@ -1067,8 +1062,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
PartitionRoot<ThreadSafe>::Page* page2 =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr5)));
PartitionFreePointerAdjust(ptr5));
EXPECT_EQ(1, page2->num_allocated_slots);
// Churn things a little whilst there's a partial page freelist.
......@@ -1097,7 +1091,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
ptr = allocator.root()->Alloc(medium_size, type_name);
EXPECT_TRUE(ptr);
page = PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
total_slots =
(page->bucket->num_system_pages_per_slot_span * kSystemPageSize) /
......@@ -1116,7 +1110,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
ptr = allocator.root()->Alloc(small_size, type_name);
EXPECT_TRUE(ptr);
page = PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
total_slots =
(page->bucket->num_system_pages_per_slot_span * kSystemPageSize) /
......@@ -1136,7 +1130,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
ptr = allocator.root()->Alloc(very_small_size, type_name);
EXPECT_TRUE(ptr);
page = PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
total_slots =
(page->bucket->num_system_pages_per_slot_span * kSystemPageSize) /
......@@ -1156,7 +1150,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
ptr = allocator.root()->Alloc(page_and_a_half_size, type_name);
EXPECT_TRUE(ptr);
page = PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
EXPECT_TRUE(page->freelist_head);
total_slots =
......@@ -1170,7 +1164,7 @@ TEST_F(PartitionAllocTest, PartialPageFreelists) {
ptr = allocator.root()->Alloc(pageSize, type_name);
EXPECT_TRUE(ptr);
page = PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
EXPECT_TRUE(page->freelist_head);
total_slots =
......@@ -1194,7 +1188,7 @@ TEST_F(PartitionAllocTest, PageRefilling) {
EXPECT_NE(page2, bucket->active_pages_head);
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(1, page->num_allocated_slots);
// Work out a pointer into page2 and free it; and then page1 and free it.
......@@ -1345,7 +1339,7 @@ TEST_F(PartitionAllocTest, FreeCache) {
EXPECT_TRUE(ptr);
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
EXPECT_EQ(nullptr, bucket->empty_pages_head);
EXPECT_EQ(1, page->num_allocated_slots);
size_t expected_committed_size = kPartitionPageSize;
......@@ -1397,11 +1391,10 @@ TEST_F(PartitionAllocTest, LostFreePagesBug) {
PartitionPage<base::internal::ThreadSafe>* page =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(PartitionCookieFreePointerAdjust(ptr)));
PartitionFreePointerAdjust(ptr));
PartitionPage<base::internal::ThreadSafe>* page2 =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr2)));
PartitionFreePointerAdjust(ptr2));
PartitionBucket<base::internal::ThreadSafe>* bucket = page->bucket;
EXPECT_EQ(nullptr, bucket->empty_pages_head);
......@@ -1914,16 +1907,13 @@ TEST_F(PartitionAllocTest, PreferActiveOverEmpty) {
PartitionPage<base::internal::ThreadSafe>* page1 =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
PartitionPage<base::internal::ThreadSafe>* page2 =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr3)));
PartitionFreePointerAdjust(ptr3));
PartitionPage<base::internal::ThreadSafe>* page3 =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr6)));
PartitionFreePointerAdjust(ptr6));
EXPECT_NE(page1, page2);
EXPECT_NE(page2, page3);
PartitionBucket<base::internal::ThreadSafe>* bucket = page1->bucket;
......@@ -1965,8 +1955,7 @@ TEST_F(PartitionAllocTest, PurgeDiscardable) {
allocator.root()->Free(ptr2);
PartitionPage<base::internal::ThreadSafe>* page =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
EXPECT_EQ(2u, page->num_unprovisioned_slots);
{
MockPartitionStatsDumper dumper;
......@@ -2158,8 +2147,7 @@ TEST_F(PartitionAllocTest, PurgeDiscardable) {
ptr1[kSystemPageSize * 3] = 'A';
PartitionPage<base::internal::ThreadSafe>* page =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
allocator.root()->Free(ptr2);
allocator.root()->Free(ptr4);
allocator.root()->Free(ptr1);
......@@ -2225,8 +2213,7 @@ TEST_F(PartitionAllocTest, PurgeDiscardable) {
ptr1[kSystemPageSize * 3] = 'A';
PartitionPage<base::internal::ThreadSafe>* page =
PartitionPage<base::internal::ThreadSafe>::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
allocator.root()->Free(ptr4);
allocator.root()->Free(ptr3);
EXPECT_EQ(0u, page->num_unprovisioned_slots);
......@@ -2469,8 +2456,7 @@ TEST_F(PartitionAllocTest, TagBasic) {
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
EXPECT_TRUE(page);
char* char_ptr1 = reinterpret_cast<char*>(ptr1);
......@@ -2528,8 +2514,7 @@ TEST_F(PartitionAllocTest, TagForDirectMap) {
PartitionRoot<ThreadSafe>::Page* page =
PartitionRoot<ThreadSafe>::Page::FromPointer(
PartitionTagFreePointerAdjust(
PartitionCookieFreePointerAdjust(ptr1)));
PartitionFreePointerAdjust(ptr1));
EXPECT_TRUE(page);
constexpr PartitionTag kTag1 = 0xBADA;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment