1 // Copyright 2018 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 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_PROCESS_HEAP_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_PROCESS_HEAP_H_
7 
8 #include <atomic>
9 #include "third_party/blink/renderer/platform/platform_export.h"
10 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
11 #include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
12 
13 namespace blink {
14 
15 class CrossThreadPersistentRegion;
16 
17 class PLATFORM_EXPORT ProcessHeap {
18   STATIC_ONLY(ProcessHeap);
19 
20  public:
21   static void Init();
22 
23   static CrossThreadPersistentRegion& GetCrossThreadPersistentRegion();
24   static CrossThreadPersistentRegion& GetCrossThreadWeakPersistentRegion();
25 
26   // Access to the CrossThreadPersistentRegion from multiple threads has to be
27   // prevented as allocation, freeing, and iteration of nodes may otherwise
28   // cause data races.
29   //
30   // Examples include:
31   // - Iteration of strong cross-thread Persistents.
32   // - Iteration and processing of weak cross-thread Persistents. The lock
33   //   needs to span both operations as iteration of weak persistents only
34   //   registers memory regions that are then processed afterwards.
35   // - Marking phase in garbage collection: The whole phase requires locking
36   //   as CrossThreadWeakPersistents may be converted to CrossThreadPersistent
37   //   which must observe GC as an atomic operation.
38   static Mutex& CrossThreadPersistentMutex();
39 
IncreaseTotalAllocatedObjectSize(size_t delta)40   static void IncreaseTotalAllocatedObjectSize(size_t delta) {
41     total_allocated_object_size_.fetch_add(delta, std::memory_order_relaxed);
42   }
DecreaseTotalAllocatedObjectSize(size_t delta)43   static void DecreaseTotalAllocatedObjectSize(size_t delta) {
44     total_allocated_object_size_.fetch_sub(delta, std::memory_order_relaxed);
45   }
TotalAllocatedObjectSize()46   static size_t TotalAllocatedObjectSize() {
47     return total_allocated_object_size_.load(std::memory_order_relaxed);
48   }
IncreaseTotalAllocatedSpace(size_t delta)49   static void IncreaseTotalAllocatedSpace(size_t delta) {
50     total_allocated_space_.fetch_add(delta, std::memory_order_relaxed);
51   }
DecreaseTotalAllocatedSpace(size_t delta)52   static void DecreaseTotalAllocatedSpace(size_t delta) {
53     total_allocated_space_.fetch_sub(delta, std::memory_order_relaxed);
54   }
TotalAllocatedSpace()55   static size_t TotalAllocatedSpace() {
56     return total_allocated_space_.load(std::memory_order_relaxed);
57   }
58   static void ResetHeapCounters();
59 
60  private:
61   static std::atomic_size_t total_allocated_space_;
62   static std::atomic_size_t total_allocated_object_size_;
63 
64   friend class ThreadState;
65 };
66 
67 }  // namespace blink
68 
69 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_PROCESS_HEAP_H_
70