Commit 45d5d199 authored by Benoit Lize's avatar Benoit Lize Committed by Chromium LUCI CQ

[PA] Use the main allocator for aligned allocations with small alignment

Some call sites in Chromium use (for instance) base::AlignedAlloc() with
a small-enough alignment to be fulfilled by the normal allocator. For
instance, with alignment = sizeof(void*), or 16 on 64 bit architectures.

In these cases, we can use the regular allocator directly, which is
faster, and can reduce memory fragmentation.

Bug: 998048
Change-Id: Idca581c5db744f15fbb34cf3a80caf1f6d941546
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2615058
Commit-Queue: Benoit L <lizeb@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#842002}
parent a121cdf9
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/allocator/buildflags.h" #include "base/allocator/buildflags.h"
#include "base/allocator/partition_allocator/memory_reclaimer.h" #include "base/allocator/partition_allocator/memory_reclaimer.h"
#include "base/allocator/partition_allocator/partition_alloc.h" #include "base/allocator/partition_allocator/partition_alloc.h"
#include "base/allocator/partition_allocator/partition_alloc_check.h"
#include "base/allocator/partition_allocator/partition_alloc_constants.h" #include "base/allocator/partition_allocator/partition_alloc_constants.h"
#include "base/allocator/partition_allocator/partition_alloc_features.h" #include "base/allocator/partition_allocator/partition_alloc_features.h"
#include "base/allocator/partition_allocator/partition_stats.h" #include "base/allocator/partition_allocator/partition_stats.h"
...@@ -153,6 +154,34 @@ ALWAYS_INLINE size_t MaybeAdjustSize(size_t size) { ...@@ -153,6 +154,34 @@ ALWAYS_INLINE size_t MaybeAdjustSize(size_t size) {
#endif // defined(OS_WIN) && defined(ARCH_CPU_X86) #endif // defined(OS_WIN) && defined(ARCH_CPU_X86)
} }
void* AllocateAlignedMemory(size_t alignment, size_t size) {
// Memory returned by the regular allocator *always* respects |kAlignment|,
// which is a power of two, and any valid alignment is also a power of two. So
// we can directly fulfill these requests with the main allocator.
//
// This has several advantages:
// - The thread cache is supported on the main partition
// - Reduced fragmentation
// - Better coverage for MiraclePtr variants requiring extras
//
// There are several call sites in Chromium where base::AlignedAlloc is called
// with a small alignment. Some may be due to overly-careful code, some are
// because the client code doesn't know the required alignment at compile
// time.
//
// Note that all "AlignedFree()" variants (_aligned_free() on Windows for
// instance) directly call PartitionFree(), so there is no risk of
// mismatch. (see below the default_dispatch definition).
if (alignment <= base::kAlignment) {
// This is mandated by |posix_memalign()| and friends, so should never fire.
PA_CHECK(base::bits::IsPowerOfTwo(alignment));
return Allocator()->AllocFlagsNoHooks(0, size);
}
return AlignedAllocator()->AlignedAllocFlags(base::PartitionAllocNoHooks,
alignment, size);
}
} // namespace } // namespace
namespace base { namespace base {
...@@ -181,16 +210,14 @@ void* PartitionMemalign(const AllocatorDispatch*, ...@@ -181,16 +210,14 @@ void* PartitionMemalign(const AllocatorDispatch*,
size_t alignment, size_t alignment,
size_t size, size_t size,
void* context) { void* context) {
return AlignedAllocator()->AlignedAllocFlags( return AllocateAlignedMemory(alignment, size);
base::PartitionAllocNoHooks, alignment, MaybeAdjustSize(size));
} }
void* PartitionAlignedAlloc(const AllocatorDispatch* dispatch, void* PartitionAlignedAlloc(const AllocatorDispatch* dispatch,
size_t size, size_t size,
size_t alignment, size_t alignment,
void* context) { void* context) {
return AlignedAllocator()->AlignedAllocFlags( return AllocateAlignedMemory(alignment, size);
base::PartitionAllocNoHooks, alignment, MaybeAdjustSize(size));
} }
// aligned_realloc documentation is // aligned_realloc documentation is
......
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