v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
allocation-observer.h
Go to the documentation of this file.
1// Copyright 2020 the V8 project 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 V8_HEAP_ALLOCATION_OBSERVER_H_
6#define V8_HEAP_ALLOCATION_OBSERVER_H_
7
8#include <cstdint>
9#include <unordered_set>
10#include <vector>
11
12#include "src/common/globals.h"
13
14namespace v8 {
15namespace internal {
16
17// Observer for allocations that is aware of LAB-based allocation.
19 public:
20 static constexpr intptr_t kNotUsingFixedStepSize = -1;
21 explicit AllocationObserver(intptr_t step_size) : step_size_(step_size) {}
22 virtual ~AllocationObserver() = default;
25
26 protected:
27 // Called when at least `step_size_` bytes have been allocated. `soon_object`
28 // points to the uninitialized memory that has just been allocated and is the
29 // result for a request of `size` bytes.
30 //
31 // Some caveats:
32 // 1. `soon_object` will be nullptr in cases zwhere the allocation returns a
33 // filler object, which is e.g. needed at page boundaries.
34 // 2. `soon_object` may actually be the first object in an
35 // allocation-folding group. In such a case size is the size of the group
36 // rather than the first object.
37 // 3. `size` is the requested size at the time of allocation. Right-trimming
38 // may change the object size dynamically.
39 virtual void Step(int bytes_allocated, Address soon_object, size_t size) = 0;
40
41 // Subclasses can override this method to make step size dynamic.
42 virtual intptr_t GetNextStepSize() {
44 return step_size_;
45 }
46
47 private:
48 const intptr_t step_size_;
49
50 friend class AllocationCounter;
51};
52
53// A global allocation counter observers can be added to.
54class AllocationCounter final {
55 public:
56 AllocationCounter() = default;
57
58 // Adds an observer. May be called from `AllocationObserver::Step()`.
60
61 // Removes an observer. May be called from `AllocationObserver::Step()`.
63
64 // Advances forward by `allocated` bytes. Does not invoke any observers.
65 V8_EXPORT_PRIVATE void AdvanceAllocationObservers(size_t allocated);
66
67 // Invokes observers via `AllocationObserver::Step()` and computes new step
68 // sizes. Does not advance the current allocation counter.
70 size_t object_size,
71 size_t aligned_object_size);
72
73 bool IsStepInProgress() const { return step_in_progress_; }
74
75 size_t NextBytes() const {
76 if (observers_.empty()) return SIZE_MAX;
78 }
79
80#if DEBUG
81 bool HasAllocationObservers() const {
82 return !observers_.empty() || !pending_added_.empty() ||
83 !pending_removed_.empty();
84 }
85#endif // DEBUG
86
87 private:
89 AllocationObserverCounter(AllocationObserver* observer, size_t prev_counter,
90 size_t next_counter)
91 : observer_(observer),
92 prev_counter_(prev_counter),
93 next_counter_(next_counter) {}
94
98 };
99
100 std::vector<AllocationObserverCounter> observers_;
101 std::vector<AllocationObserverCounter> pending_added_;
102 std::unordered_set<AllocationObserver*> pending_removed_;
103
105 size_t next_counter_ = 0;
106 bool step_in_progress_ = false;
107};
108
120
121} // namespace internal
122} // namespace v8
123
124#endif // V8_HEAP_ALLOCATION_OBSERVER_H_
std::unordered_set< AllocationObserver * > pending_removed_
V8_EXPORT_PRIVATE void AdvanceAllocationObservers(size_t allocated)
V8_EXPORT_PRIVATE void RemoveAllocationObserver(AllocationObserver *observer)
std::vector< AllocationObserverCounter > pending_added_
std::vector< AllocationObserverCounter > observers_
V8_EXPORT_PRIVATE void AddAllocationObserver(AllocationObserver *observer)
V8_EXPORT_PRIVATE void InvokeAllocationObservers(Address soon_object, size_t object_size, size_t aligned_object_size)
AllocationObserver & operator=(const AllocationObserver &)=delete
static constexpr intptr_t kNotUsingFixedStepSize
virtual void Step(int bytes_allocated, Address soon_object, size_t size)=0
AllocationObserver(const AllocationObserver &)=delete
virtual ~AllocationObserver()=default
PauseAllocationObserversScope & operator=(const PauseAllocationObserversScope &)=delete
PauseAllocationObserversScope(const PauseAllocationObserversScope &)=delete
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define V8_EXPORT_PRIVATE
Definition macros.h:460
AllocationObserverCounter(AllocationObserver *observer, size_t prev_counter, size_t next_counter)
#define V8_NODISCARD
Definition v8config.h:693