xref: /aosp_15_r20/external/cronet/base/allocator/partition_allocator/src/partition_alloc/starscan/snapshot.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
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 PARTITION_ALLOC_STARSCAN_SNAPSHOT_H_
6 #define PARTITION_ALLOC_STARSCAN_SNAPSHOT_H_
7 
8 #include <memory>
9 #include <utility>
10 
11 #include "partition_alloc/internal_allocator_forward.h"
12 #include "partition_alloc/starscan/pcscan_internal.h"
13 #include "partition_alloc/starscan/raceful_worklist.h"
14 
15 namespace partition_alloc::internal {
16 
17 class StarScanSnapshot final : public InternalPartitionAllocated {
18  public:
19   using SuperPageBase = uintptr_t;
20   using SuperPagesWorklist = RacefulWorklist<SuperPageBase>;
21 
22   class ViewBase {
23    public:
24     template <typename Function>
25     void VisitConcurrently(Function);
26 
27     template <typename Function>
28     void VisitNonConcurrently(Function);
29 
30    protected:
ViewBase(SuperPagesWorklist & worklist)31     explicit ViewBase(SuperPagesWorklist& worklist) : worklist_(worklist) {}
32 
33    private:
34     SuperPagesWorklist& worklist_;
35   };
36 
37   class ClearingView : public ViewBase {
38    public:
39     inline explicit ClearingView(StarScanSnapshot& snapshot);
40   };
41   class ScanningView : public ViewBase {
42    public:
43     inline explicit ScanningView(StarScanSnapshot& snapshot);
44   };
45   class SweepingView : public ViewBase {
46    public:
47     inline explicit SweepingView(StarScanSnapshot& snapshot);
48   };
49   class UnprotectingView : public ViewBase {
50    public:
51     inline explicit UnprotectingView(StarScanSnapshot& snapshot);
52   };
53 
54   static std::unique_ptr<StarScanSnapshot> Create(const PCScanInternal&);
55 
56   StarScanSnapshot(const StarScanSnapshot&) = delete;
57   StarScanSnapshot& operator=(const StarScanSnapshot&) = delete;
58 
59   ~StarScanSnapshot();
60 
61  private:
62   explicit StarScanSnapshot(const PCScanInternal&);
63 
64   SuperPagesWorklist clear_worklist_;
65   SuperPagesWorklist scan_worklist_;
66   SuperPagesWorklist unprotect_worklist_;
67   SuperPagesWorklist sweep_worklist_;
68 };
69 
70 template <typename Function>
VisitConcurrently(Function f)71 void StarScanSnapshot::ViewBase::VisitConcurrently(Function f) {
72   SuperPagesWorklist::RandomizedView view(worklist_);
73   view.Visit(std::move(f));
74 }
75 
76 template <typename Function>
VisitNonConcurrently(Function f)77 void StarScanSnapshot::ViewBase::VisitNonConcurrently(Function f) {
78   worklist_.VisitNonConcurrently(std::move(f));
79 }
80 
ClearingView(StarScanSnapshot & snapshot)81 StarScanSnapshot::ClearingView::ClearingView(StarScanSnapshot& snapshot)
82     : StarScanSnapshot::ViewBase(snapshot.clear_worklist_) {}
83 
ScanningView(StarScanSnapshot & snapshot)84 StarScanSnapshot::ScanningView::ScanningView(StarScanSnapshot& snapshot)
85     : StarScanSnapshot::ViewBase(snapshot.scan_worklist_) {}
86 
SweepingView(StarScanSnapshot & snapshot)87 StarScanSnapshot::SweepingView::SweepingView(StarScanSnapshot& snapshot)
88     : StarScanSnapshot::ViewBase(snapshot.sweep_worklist_) {}
89 
UnprotectingView(StarScanSnapshot & snapshot)90 StarScanSnapshot::UnprotectingView::UnprotectingView(StarScanSnapshot& snapshot)
91     : StarScanSnapshot::ViewBase(snapshot.unprotect_worklist_) {}
92 
93 }  // namespace partition_alloc::internal
94 
95 #endif  // PARTITION_ALLOC_STARSCAN_SNAPSHOT_H_
96