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( ...@@ -302,8 +302,7 @@ bool PartitionRoot<thread_safe>::ReallocDirectMappedInPlace(
size_t raw_size) { size_t raw_size) {
PA_DCHECK(page->bucket->is_direct_mapped()); PA_DCHECK(page->bucket->is_direct_mapped());
raw_size = internal::PartitionCookieSizeAdjustAdd(raw_size); raw_size = internal::PartitionSizeAdjustAdd(raw_size);
raw_size = internal::PartitionTagSizeAdjustAdd(raw_size);
// Note that the new size might be a bucketed size; this function is called // Note that the new size might be a bucketed size; this function is called
// whenever we're reallocating a direct mapped allocation. // whenever we're reallocating a direct mapped allocation.
...@@ -389,8 +388,7 @@ void* PartitionRoot<thread_safe>::ReallocFlags(int flags, ...@@ -389,8 +388,7 @@ void* PartitionRoot<thread_safe>::ReallocFlags(int flags,
&actual_old_size, ptr); &actual_old_size, ptr);
} }
if (LIKELY(!overridden)) { if (LIKELY(!overridden)) {
auto* page = Page::FromPointer(internal::PartitionTagFreePointerAdjust( auto* page = Page::FromPointer(internal::PartitionFreePointerAdjust(ptr));
internal::PartitionCookieFreePointerAdjust(ptr)));
bool success = false; bool success = false;
{ {
internal::ScopedGuard<thread_safe> guard{lock_}; internal::ScopedGuard<thread_safe> guard{lock_};
...@@ -422,8 +420,7 @@ void* PartitionRoot<thread_safe>::ReallocFlags(int flags, ...@@ -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 // 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 // the same size as the one we've already got, so re-use the allocation
// after updating statistics (and cookies, if present). // after updating statistics (and cookies, if present).
size_t new_raw_size = internal::PartitionCookieSizeAdjustAdd(new_size); size_t new_raw_size = internal::PartitionSizeAdjustAdd(new_size);
new_raw_size = internal::PartitionTagSizeAdjustAdd(new_raw_size);
page->set_raw_size(new_raw_size); page->set_raw_size(new_raw_size);
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
// Write a new trailing cookie when it is possible to keep track of // Write a new trailing cookie when it is possible to keep track of
......
...@@ -167,6 +167,24 @@ class BASE_EXPORT PartitionAllocHooks { ...@@ -167,6 +167,24 @@ class BASE_EXPORT PartitionAllocHooks {
namespace internal { 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> template <bool thread_safe>
class LOCKABLE MaybeSpinLock { class LOCKABLE MaybeSpinLock {
public: public:
...@@ -517,21 +535,22 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFromBucket(Bucket* bucket, ...@@ -517,21 +535,22 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFromBucket(Bucket* bucket,
// c: new_slot_size // c: new_slot_size
// Note, empty space occurs if the slot size is larger than needed to // Note, empty space occurs if the slot size is larger than needed to
// accommodate the request. // accommodate the request.
size_t size_with_no_extras = internal::PartitionTagSizeAdjustSubtract( char* char_ret = static_cast<char*>(ret);
internal::PartitionCookieSizeAdjustSubtract(new_slot_size)); size_t size_with_no_extras =
char* char_ret = static_cast<char*>(ret) + internal::kPartitionTagSize; internal::PartitionSizeAdjustSubtract(new_slot_size);
// The value given to the application is actually just after the cookie. // The value given to the application is just after the tag and cookie.
ret = char_ret + internal::kCookieSize; char_ret += internal::kPartitionTagSize + internal::kCookieSize;
ret = char_ret;
// Fill the region kUninitializedByte or 0, and surround it with 2 cookies. // Surround the region with 2 cookies.
internal::PartitionCookieWriteValue(char_ret); internal::PartitionCookieWriteValue(char_ret - internal::kCookieSize);
internal::PartitionCookieWriteValue(char_ret + size_with_no_extras);
// Fill the region kUninitializedByte or 0.
if (!zero_fill) { if (!zero_fill) {
memset(ret, kUninitializedByte, size_with_no_extras); memset(ret, kUninitializedByte, size_with_no_extras);
} else if (!is_already_zeroed) { } else if (!is_already_zeroed) {
memset(ret, 0, size_with_no_extras); memset(ret, 0, size_with_no_extras);
} }
internal::PartitionCookieWriteValue(char_ret + internal::kCookieSize +
size_with_no_extras);
#else #else
if (!ret) if (!ret)
return nullptr; return nullptr;
...@@ -566,8 +585,7 @@ ALWAYS_INLINE void PartitionRoot<thread_safe>::Free(void* ptr) { ...@@ -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. // TODO(tasak): clear partition tag. Temporarily set the tag to be 0.
internal::PartitionTagSetValue(ptr, 0); internal::PartitionTagSetValue(ptr, 0);
ptr = internal::PartitionCookieFreePointerAdjust(ptr); ptr = internal::PartitionFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
Page* page = Page::FromPointer(ptr); Page* page = Page::FromPointer(ptr);
// TODO(palmer): See if we can afford to make this a CHECK. // TODO(palmer): See if we can afford to make this a CHECK.
PA_DCHECK(IsValidPage(page)); PA_DCHECK(IsValidPage(page));
...@@ -687,12 +705,9 @@ PartitionAllocGetPageForSize(void* ptr) { ...@@ -687,12 +705,9 @@ PartitionAllocGetPageForSize(void* ptr) {
// partition page. // partition page.
template <bool thread_safe> template <bool thread_safe>
ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) { ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) {
ptr = internal::PartitionCookieFreePointerAdjust(ptr); ptr = internal::PartitionFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr); auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr);
size_t size = size_t size = internal::PartitionSizeAdjustSubtract(page->bucket->slot_size);
internal::PartitionCookieSizeAdjustSubtract(page->bucket->slot_size);
size = internal::PartitionTagSizeAdjustSubtract(size);
return size; return size;
} }
...@@ -703,8 +718,7 @@ ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) { ...@@ -703,8 +718,7 @@ ALWAYS_INLINE size_t PartitionAllocGetSize(void* ptr) {
template <bool thread_safe> template <bool thread_safe>
ALWAYS_INLINE size_t PartitionAllocGetSlotOffset(void* ptr) { ALWAYS_INLINE size_t PartitionAllocGetSlotOffset(void* ptr) {
PA_DCHECK(IsManagedByPartitionAllocAndNotDirectMapped(ptr)); PA_DCHECK(IsManagedByPartitionAllocAndNotDirectMapped(ptr));
ptr = internal::PartitionCookieFreePointerAdjust(ptr); ptr = internal::PartitionFreePointerAdjust(ptr);
ptr = internal::PartitionTagFreePointerAdjust(ptr);
auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr); auto* page = internal::PartitionAllocGetPageForSize<thread_safe>(ptr);
size_t slot_size = page->bucket->slot_size; size_t slot_size = page->bucket->slot_size;
...@@ -763,8 +777,7 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFlags( ...@@ -763,8 +777,7 @@ ALWAYS_INLINE void* PartitionRoot<thread_safe>::AllocFlags(
} }
} }
size_t requested_size = size; size_t requested_size = size;
size = internal::PartitionCookieSizeAdjustAdd(size); size = internal::PartitionSizeAdjustAdd(size);
size = internal::PartitionTagSizeAdjustAdd(size);
#if ENABLE_CHECKED_PTR #if ENABLE_CHECKED_PTR
PA_CHECK(size >= requested_size); PA_CHECK(size >= requested_size);
#endif #endif
...@@ -810,8 +823,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) { ...@@ -810,8 +823,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) {
return size; return size;
#else #else
PA_DCHECK(PartitionRoot<thread_safe>::initialized); PA_DCHECK(PartitionRoot<thread_safe>::initialized);
size = internal::PartitionCookieSizeAdjustAdd(size); size = internal::PartitionSizeAdjustAdd(size);
size = internal::PartitionTagSizeAdjustAdd(size);
auto* bucket = SizeToBucket(size); auto* bucket = SizeToBucket(size);
if (LIKELY(!bucket->is_direct_mapped())) { if (LIKELY(!bucket->is_direct_mapped())) {
size = bucket->slot_size; size = bucket->slot_size;
...@@ -820,8 +832,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) { ...@@ -820,8 +832,7 @@ ALWAYS_INLINE size_t PartitionRoot<thread_safe>::ActualSize(size_t size) {
} else { } else {
size = Bucket::get_direct_map_size(size); size = Bucket::get_direct_map_size(size);
} }
size = internal::PartitionCookieSizeAdjustSubtract(size); size = internal::PartitionSizeAdjustSubtract(size);
size = internal::PartitionTagSizeAdjustSubtract(size);
return size; return size;
#endif #endif
} }
...@@ -845,6 +856,7 @@ struct BASE_EXPORT PartitionAllocator { ...@@ -845,6 +856,7 @@ struct BASE_EXPORT PartitionAllocator {
private: private:
PartitionRoot<thread_safe> partition_root_; PartitionRoot<thread_safe> partition_root_;
}; };
} // namespace internal } // namespace internal
using PartitionAllocator = internal::PartitionAllocator<internal::ThreadSafe>; using PartitionAllocator = internal::PartitionAllocator<internal::ThreadSafe>;
......
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