Commit 29da00b9 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Convert partition_allocator code to use base::NoDestruct.

base: :LazyInstance is deprecated.
Change-Id: I25a4ee2fbc47ca4d5ce775738316e129b3538327
Reviewed-on: https://chromium-review.googlesource.com/c/1284531Reviewed-by: default avatarChris Palmer <palmer@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600171}
parent 094e26dc
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include "base/allocator/partition_allocator/page_allocator.h" #include "base/allocator/partition_allocator/page_allocator.h"
#include "base/allocator/partition_allocator/spin_lock.h" #include "base/allocator/partition_allocator/spin_lock.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
#include "base/rand_util.h" #include "base/rand_util.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -32,8 +32,10 @@ struct RandomContext { ...@@ -32,8 +32,10 @@ struct RandomContext {
uint32_t d; uint32_t d;
}; };
static LazyInstance<RandomContext>::Leaky s_RandomContext = RandomContext* GetRandomContext() {
LAZY_INSTANCE_INITIALIZER; static NoDestructor<RandomContext> s_RandomContext;
return s_RandomContext.get();
}
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
...@@ -68,7 +70,7 @@ uint32_t RandomValue(RandomContext* x) { ...@@ -68,7 +70,7 @@ uint32_t RandomValue(RandomContext* x) {
} // namespace } // namespace
void SetRandomPageBaseSeed(int64_t seed) { void SetRandomPageBaseSeed(int64_t seed) {
RandomContext* x = s_RandomContext.Pointer(); RandomContext* x = GetRandomContext();
subtle::SpinLock::Guard guard(x->lock); subtle::SpinLock::Guard guard(x->lock);
// Set RNG to initial state. // Set RNG to initial state.
x->initialized = true; x->initialized = true;
...@@ -77,12 +79,11 @@ void SetRandomPageBaseSeed(int64_t seed) { ...@@ -77,12 +79,11 @@ void SetRandomPageBaseSeed(int64_t seed) {
} }
void* GetRandomPageBase() { void* GetRandomPageBase() {
uintptr_t random = uintptr_t random = static_cast<uintptr_t>(RandomValue(GetRandomContext()));
static_cast<uintptr_t>(RandomValue(s_RandomContext.Pointer()));
#if defined(ARCH_CPU_64_BITS) #if defined(ARCH_CPU_64_BITS)
random <<= 32ULL; random <<= 32ULL;
random |= static_cast<uintptr_t>(RandomValue(s_RandomContext.Pointer())); random |= static_cast<uintptr_t>(RandomValue(GetRandomContext()));
// The kASLRMask and kASLROffset constants will be suitable for the // The kASLRMask and kASLROffset constants will be suitable for the
// OS and build configuration. // OS and build configuration.
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
#include "base/allocator/partition_allocator/address_space_randomization.h" #include "base/allocator/partition_allocator/address_space_randomization.h"
#include "base/allocator/partition_allocator/page_allocator_internal.h" #include "base/allocator/partition_allocator/page_allocator_internal.h"
#include "base/allocator/partition_allocator/spin_lock.h" #include "base/allocator/partition_allocator/spin_lock.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
#include "base/numerics/checked_math.h" #include "base/numerics/checked_math.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -33,7 +33,10 @@ namespace base { ...@@ -33,7 +33,10 @@ namespace base {
namespace { namespace {
// We may reserve/release address space on different threads. // We may reserve/release address space on different threads.
LazyInstance<subtle::SpinLock>::Leaky s_reserveLock = LAZY_INSTANCE_INITIALIZER; subtle::SpinLock& GetReserveLock() {
static NoDestructor<subtle::SpinLock> s_reserveLock;
return *s_reserveLock;
}
// We only support a single block of reserved address space. // We only support a single block of reserved address space.
void* s_reservation_address = nullptr; void* s_reservation_address = nullptr;
...@@ -223,7 +226,7 @@ void DiscardSystemPages(void* address, size_t length) { ...@@ -223,7 +226,7 @@ void DiscardSystemPages(void* address, size_t length) {
bool ReserveAddressSpace(size_t size) { bool ReserveAddressSpace(size_t size) {
// To avoid deadlock, call only SystemAllocPages. // To avoid deadlock, call only SystemAllocPages.
subtle::SpinLock::Guard guard(s_reserveLock.Get()); subtle::SpinLock::Guard guard(GetReserveLock());
if (s_reservation_address == nullptr) { if (s_reservation_address == nullptr) {
void* mem = SystemAllocPages(nullptr, size, PageInaccessible, void* mem = SystemAllocPages(nullptr, size, PageInaccessible,
PageTag::kChromium, false); PageTag::kChromium, false);
...@@ -241,7 +244,7 @@ bool ReserveAddressSpace(size_t size) { ...@@ -241,7 +244,7 @@ bool ReserveAddressSpace(size_t size) {
void ReleaseReservation() { void ReleaseReservation() {
// To avoid deadlock, call only FreePages. // To avoid deadlock, call only FreePages.
subtle::SpinLock::Guard guard(s_reserveLock.Get()); subtle::SpinLock::Guard guard(GetReserveLock());
if (s_reservation_address != nullptr) { if (s_reservation_address != nullptr) {
FreePages(s_reservation_address, s_reservation_size); FreePages(s_reservation_address, s_reservation_size);
s_reservation_address = nullptr; s_reservation_address = nullptr;
......
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
#include "base/allocator/partition_allocator/partition_oom.h" #include "base/allocator/partition_allocator/partition_oom.h"
#include "base/allocator/partition_allocator/partition_page.h" #include "base/allocator/partition_allocator/partition_page.h"
#include "base/allocator/partition_allocator/spin_lock.h" #include "base/allocator/partition_allocator/spin_lock.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/no_destructor.h"
namespace base { namespace base {
...@@ -57,8 +57,10 @@ PartitionRootGeneric::~PartitionRootGeneric() = default; ...@@ -57,8 +57,10 @@ PartitionRootGeneric::~PartitionRootGeneric() = default;
PartitionAllocatorGeneric::PartitionAllocatorGeneric() = default; PartitionAllocatorGeneric::PartitionAllocatorGeneric() = default;
PartitionAllocatorGeneric::~PartitionAllocatorGeneric() = default; PartitionAllocatorGeneric::~PartitionAllocatorGeneric() = default;
static LazyInstance<subtle::SpinLock>::Leaky g_initialized_lock = subtle::SpinLock& GetLock() {
LAZY_INSTANCE_INITIALIZER; static NoDestructor<subtle::SpinLock> s_initialized_lock;
return *s_initialized_lock;
}
static bool g_initialized = false; static bool g_initialized = false;
void (*internal::PartitionRootBase::gOomHandlingFunction)() = nullptr; void (*internal::PartitionRootBase::gOomHandlingFunction)() = nullptr;
...@@ -69,7 +71,7 @@ PartitionAllocHooks::FreeHook* PartitionAllocHooks::free_hook_ = nullptr; ...@@ -69,7 +71,7 @@ PartitionAllocHooks::FreeHook* PartitionAllocHooks::free_hook_ = nullptr;
static void PartitionAllocBaseInit(internal::PartitionRootBase* root) { static void PartitionAllocBaseInit(internal::PartitionRootBase* root) {
DCHECK(!root->initialized); DCHECK(!root->initialized);
{ {
subtle::SpinLock::Guard guard(g_initialized_lock.Get()); subtle::SpinLock::Guard guard(GetLock());
if (!g_initialized) { if (!g_initialized) {
g_initialized = true; g_initialized = true;
// We mark the sentinel bucket/page as free to make sure it is skipped by // We mark the sentinel bucket/page as free to make sure it is skipped by
......
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