Commit d96ca641 authored by Chris Palmer's avatar Chris Palmer Committed by Commit Bot

Use the new PartitionAlloc API to zero-fill allocations.

Also embellish the PA API a bit to make accessing the new zero-fill
functionality more usable.

There are likely additional call sites that can make use of this functionality.

Bug: 864462
Change-Id: I8881c5774b6083b210f5160d4b4f2ac345f2467e
Reviewed-on: https://chromium-review.googlesource.com/1212443
Commit-Queue: Chris Palmer <palmer@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarHongchan Choi <hongchan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590081}
parent b8febb05
...@@ -114,6 +114,7 @@ struct BASE_EXPORT PartitionRoot : public internal::PartitionRootBase { ...@@ -114,6 +114,7 @@ struct BASE_EXPORT PartitionRoot : public internal::PartitionRootBase {
void Init(size_t num_buckets, size_t max_allocation); void Init(size_t num_buckets, size_t max_allocation);
ALWAYS_INLINE void* Alloc(size_t size, const char* type_name); ALWAYS_INLINE void* Alloc(size_t size, const char* type_name);
ALWAYS_INLINE void* AllocFlags(int flags, size_t size, const char* type_name);
void PurgeMemory(int flags); void PurgeMemory(int flags);
...@@ -145,6 +146,7 @@ struct BASE_EXPORT PartitionRootGeneric : public internal::PartitionRootBase { ...@@ -145,6 +146,7 @@ struct BASE_EXPORT PartitionRootGeneric : public internal::PartitionRootBase {
void Init(); void Init();
ALWAYS_INLINE void* Alloc(size_t size, const char* type_name); ALWAYS_INLINE void* Alloc(size_t size, const char* type_name);
ALWAYS_INLINE void* AllocFlags(int flags, size_t size, const char* type_name);
ALWAYS_INLINE void Free(void* ptr); ALWAYS_INLINE void Free(void* ptr);
NOINLINE void* Realloc(void* ptr, size_t new_size, const char* type_name); NOINLINE void* Realloc(void* ptr, size_t new_size, const char* type_name);
...@@ -262,6 +264,12 @@ class BASE_EXPORT PartitionAllocHooks { ...@@ -262,6 +264,12 @@ class BASE_EXPORT PartitionAllocHooks {
}; };
ALWAYS_INLINE void* PartitionRoot::Alloc(size_t size, const char* type_name) { ALWAYS_INLINE void* PartitionRoot::Alloc(size_t size, const char* type_name) {
return AllocFlags(0, size, type_name);
}
ALWAYS_INLINE void* PartitionRoot::AllocFlags(int flags,
size_t size,
const char* type_name) {
#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
void* result = malloc(size); void* result = malloc(size);
CHECK(result); CHECK(result);
...@@ -274,7 +282,7 @@ ALWAYS_INLINE void* PartitionRoot::Alloc(size_t size, const char* type_name) { ...@@ -274,7 +282,7 @@ ALWAYS_INLINE void* PartitionRoot::Alloc(size_t size, const char* type_name) {
DCHECK(index < this->num_buckets); DCHECK(index < this->num_buckets);
DCHECK(size == index << kBucketShift); DCHECK(size == index << kBucketShift);
internal::PartitionBucket* bucket = &this->buckets()[index]; internal::PartitionBucket* bucket = &this->buckets()[index];
void* result = AllocFromBucket(bucket, 0, size); void* result = AllocFromBucket(bucket, flags, size);
PartitionAllocHooks::AllocationHookIfEnabled(result, requested_size, PartitionAllocHooks::AllocationHookIfEnabled(result, requested_size,
type_name); type_name);
return result; return result;
...@@ -373,6 +381,12 @@ ALWAYS_INLINE void* PartitionRootGeneric::Alloc(size_t size, ...@@ -373,6 +381,12 @@ ALWAYS_INLINE void* PartitionRootGeneric::Alloc(size_t size,
return PartitionAllocGenericFlags(this, 0, size, type_name); return PartitionAllocGenericFlags(this, 0, size, type_name);
} }
ALWAYS_INLINE void* PartitionRootGeneric::AllocFlags(int flags,
size_t size,
const char* type_name) {
return PartitionAllocGenericFlags(this, flags, size, type_name);
}
ALWAYS_INLINE void PartitionRootGeneric::Free(void* ptr) { ALWAYS_INLINE void PartitionRootGeneric::Free(void* ptr) {
#if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
free(ptr); free(ptr);
......
...@@ -82,7 +82,7 @@ class AudioArray { ...@@ -82,7 +82,7 @@ class AudioArray {
// Again, check for integer overflow. // Again, check for integer overflow.
CHECK_GE(initial_size + extra_allocation_bytes, initial_size); CHECK_GE(initial_size + extra_allocation_bytes, initial_size);
T* allocation = static_cast<T*>(WTF::Partitions::FastMalloc( T* allocation = static_cast<T*>(WTF::Partitions::FastZeroedMalloc(
initial_size + extra_allocation_bytes, initial_size + extra_allocation_bytes,
WTF_HEAP_PROFILER_TYPE_NAME(AudioArray<T>))); WTF_HEAP_PROFILER_TYPE_NAME(AudioArray<T>)));
CHECK(allocation); CHECK(allocation);
...@@ -94,7 +94,6 @@ class AudioArray { ...@@ -94,7 +94,6 @@ class AudioArray {
aligned_data_ = aligned_data; aligned_data_ = aligned_data;
size_ = n; size_ = n;
is_allocation_good = true; is_allocation_good = true;
Zero();
} else { } else {
// always allocate extra after the first alignment failure. // always allocate extra after the first alignment failure.
extra_allocation_bytes = kAlignment; extra_allocation_bytes = kAlignment;
......
...@@ -113,10 +113,12 @@ class WTF_EXPORT Partitions { ...@@ -113,10 +113,12 @@ class WTF_EXPORT Partitions {
static void* FastMalloc(size_t n, const char* type_name) { static void* FastMalloc(size_t n, const char* type_name) {
return Partitions::FastMallocPartition()->Alloc(n, type_name); return Partitions::FastMallocPartition()->Alloc(n, type_name);
} }
static void* FastMallocFlags(int flags, size_t n, const char* type_name) {
return Partitions::FastMallocPartition()->AllocFlags(flags, n, type_name);
}
static void* FastZeroedMalloc(size_t n, const char* type_name) { static void* FastZeroedMalloc(size_t n, const char* type_name) {
void* result = FastMalloc(n, type_name); return Partitions::FastMallocPartition()->AllocFlags(
memset(result, 0, n); base::PartitionAllocZeroFill, n, type_name);
return result;
} }
static void* FastRealloc(void* p, size_t n, const char* type_name) { static void* FastRealloc(void* p, size_t n, const char* type_name) {
return Partitions::FastMallocPartition()->Realloc(p, n, type_name); return Partitions::FastMallocPartition()->Realloc(p, n, type_name);
......
...@@ -108,11 +108,12 @@ void ArrayBufferContents::CopyTo(ArrayBufferContents& other) { ...@@ -108,11 +108,12 @@ void ArrayBufferContents::CopyTo(ArrayBufferContents& other) {
void* ArrayBufferContents::AllocateMemoryWithFlags(size_t size, void* ArrayBufferContents::AllocateMemoryWithFlags(size_t size,
InitializationPolicy policy, InitializationPolicy policy,
int flags) { int flags) {
if (policy == kZeroInitialize) {
flags |= base::PartitionAllocZeroFill;
}
void* data = PartitionAllocGenericFlags( void* data = PartitionAllocGenericFlags(
Partitions::ArrayBufferPartition(), flags, size, Partitions::ArrayBufferPartition(), flags, size,
WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents)); WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents));
if (policy == kZeroInitialize && data)
memset(data, '\0', size);
return data; return data;
} }
......
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