extended_api.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/extended_api.h"
  5. #include "base/allocator/allocator_shim_default_dispatch_to_partition_alloc.h"
  6. #include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
  7. #include "base/allocator/partition_allocator/thread_cache.h"
  8. namespace partition_alloc::internal {
  9. #if defined(PA_THREAD_CACHE_SUPPORTED)
  10. namespace {
  11. void DisableThreadCacheForRootIfEnabled(ThreadSafePartitionRoot* root) {
  12. // Some platforms don't have a thread cache, or it could already have been
  13. // disabled.
  14. if (!root || !root->flags.with_thread_cache)
  15. return;
  16. ThreadCacheRegistry::Instance().PurgeAll();
  17. root->flags.with_thread_cache = false;
  18. // Doesn't destroy the thread cache object(s). For background threads, they
  19. // will be collected (and free cached memory) at thread destruction
  20. // time. For the main thread, we leak it.
  21. }
  22. void EnablePartitionAllocThreadCacheForRootIfDisabled(
  23. ThreadSafePartitionRoot* root) {
  24. if (!root)
  25. return;
  26. root->flags.with_thread_cache = true;
  27. }
  28. #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  29. void DisablePartitionAllocThreadCacheForProcess() {
  30. auto* regular_allocator = ::base::internal::PartitionAllocMalloc::Allocator();
  31. auto* aligned_allocator =
  32. ::base::internal::PartitionAllocMalloc::AlignedAllocator();
  33. DisableThreadCacheForRootIfEnabled(regular_allocator);
  34. if (aligned_allocator != regular_allocator)
  35. DisableThreadCacheForRootIfEnabled(aligned_allocator);
  36. DisableThreadCacheForRootIfEnabled(
  37. ::base::internal::PartitionAllocMalloc::OriginalAllocator());
  38. }
  39. #endif // defined(USE_PARTITION_ALLOC_AS_MALLOC)
  40. } // namespace
  41. #endif // defined(PA_THREAD_CACHE_SUPPORTED)
  42. void SwapOutProcessThreadCacheForTesting(ThreadSafePartitionRoot* root) {
  43. #if defined(PA_THREAD_CACHE_SUPPORTED)
  44. #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  45. DisablePartitionAllocThreadCacheForProcess();
  46. #else
  47. PA_CHECK(!ThreadCache::IsValid(ThreadCache::Get()));
  48. #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  49. ThreadCache::SwapForTesting(root);
  50. EnablePartitionAllocThreadCacheForRootIfDisabled(root);
  51. #endif // defined(PA_THREAD_CACHE_SUPPORTED)
  52. }
  53. void SwapInProcessThreadCacheForTesting(ThreadSafePartitionRoot* root) {
  54. #if defined(PA_THREAD_CACHE_SUPPORTED)
  55. // First, disable the test thread cache we have.
  56. DisableThreadCacheForRootIfEnabled(root);
  57. #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  58. auto* regular_allocator = ::base::internal::PartitionAllocMalloc::Allocator();
  59. EnablePartitionAllocThreadCacheForRootIfDisabled(regular_allocator);
  60. ThreadCache::SwapForTesting(regular_allocator);
  61. #else
  62. ThreadCache::SwapForTesting(nullptr);
  63. #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  64. #endif // defined(PA_THREAD_CACHE_SUPPORTED)
  65. }
  66. } // namespace partition_alloc::internal