allocation_guard.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/allocator/partition_allocator/allocation_guard.h"
  5. #include "base/allocator/partition_allocator/partition_alloc_base/immediate_crash.h"
  6. #include "base/allocator/partition_allocator/partition_alloc_config.h"
  7. #if defined(PA_HAS_ALLOCATION_GUARD)
  8. namespace partition_alloc {
  9. namespace {
  10. thread_local bool g_disallow_allocations;
  11. } // namespace
  12. ScopedDisallowAllocations::ScopedDisallowAllocations() {
  13. if (g_disallow_allocations)
  14. PA_IMMEDIATE_CRASH();
  15. g_disallow_allocations = true;
  16. }
  17. ScopedDisallowAllocations::~ScopedDisallowAllocations() {
  18. g_disallow_allocations = false;
  19. }
  20. ScopedAllowAllocations::ScopedAllowAllocations() {
  21. // Save the previous value, as ScopedAllowAllocations is used in all
  22. // partitions, not just the malloc() ones(s).
  23. saved_value_ = g_disallow_allocations;
  24. g_disallow_allocations = false;
  25. }
  26. ScopedAllowAllocations::~ScopedAllowAllocations() {
  27. g_disallow_allocations = saved_value_;
  28. }
  29. } // namespace partition_alloc
  30. #endif // defined(PA_HAS_ALLOCATION_GUARD)