read-only-heap.h 4.78 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_HEAP_READ_ONLY_HEAP_H_
#define V8_HEAP_READ_ONLY_HEAP_H_

8
#include <memory>
9
#include <utility>
10
#include <vector>
11

12
#include "src/base/macros.h"
13
#include "src/base/optional.h"
14
#include "src/objects/heap-object.h"
15
#include "src/objects/objects.h"
16
#include "src/roots/roots.h"
17 18

namespace v8 {
19 20 21

class SharedMemoryStatistics;

22 23
namespace internal {

24
class BasicMemoryChunk;
25
class Isolate;
26
class Page;
27
class ReadOnlyArtifacts;
28
class ReadOnlyDeserializer;
29
class ReadOnlyPage;
30
class ReadOnlySpace;
31 32

// This class transparently manages read-only space, roots and cache creation
33
// and destruction.
34
class ReadOnlyHeap final {
35
 public:
36 37 38
  static constexpr size_t kEntriesCount =
      static_cast<size_t>(RootIndex::kReadOnlyRootsCount);

39 40 41 42 43 44 45
  // If necessary creates read-only heap and initializes its artifacts (if the
  // deserializer is provided). Then attaches the read-only heap to the isolate.
  // If the deserializer is not provided, then the read-only heap will be only
  // finish initializing when initial heap object creation in the Isolate is
  // completed, which is signalled by calling OnCreateHeapObjectsComplete. When
  // V8_SHARED_RO_HEAP is enabled, a lock will be held until that method is
  // called.
46
  // TODO(v8:7464): Ideally we'd create this without needing a heap.
47
  static void SetUp(Isolate* isolate, ReadOnlyDeserializer* des);
48
  // Indicates that the isolate has been set up and all read-only space objects
49 50 51
  // have been created and will not be written to. This should only be called if
  // a deserializer was not previously provided to Setup. When V8_SHARED_RO_HEAP
  // is enabled, this releases the ReadOnlyHeap creation lock.
52 53 54
  void OnCreateHeapObjectsComplete(Isolate* isolate);
  // Indicates that the current isolate no longer requires the read-only heap
  // and it may be safely disposed of.
55
  void OnHeapTearDown();
56 57 58 59
  // If the read-only heap is shared, then populate |statistics| with its stats,
  // otherwise the read-only heap stats are set to 0.
  static void PopulateReadOnlySpaceStatistics(
      SharedMemoryStatistics* statistics);
60

61 62
  // Returns whether the address is within the read-only space.
  V8_EXPORT_PRIVATE static bool Contains(Address address);
63
  // Returns whether the object resides in the read-only space.
64
  V8_EXPORT_PRIVATE static bool Contains(HeapObject object);
65 66 67
  // Gets read-only roots from an appropriate root list: shared read-only root
  // list if the shared read-only heap has been initialized or the isolate
  // specific roots table.
68 69
  V8_EXPORT_PRIVATE inline static ReadOnlyRoots GetReadOnlyRoots(
      HeapObject object);
70

71 72 73 74 75 76 77
  // Extends the read-only object cache with new zero smi and returns a
  // reference to it.
  Object* ExtendReadOnlyObjectCache();
  // Returns a read-only cache entry at a particular index.
  Object cached_read_only_object(size_t i) const;
  bool read_only_object_cache_is_initialized() const;

78 79 80
  ReadOnlySpace* read_only_space() const { return read_only_space_; }

 private:
81
  // Creates a new read-only heap and attaches it to the provided isolate.
82 83 84
  static ReadOnlyHeap* CreateAndAttachToIsolate(
      Isolate* isolate, std::shared_ptr<ReadOnlyArtifacts> artifacts);
  // Runs the read-only deserializer and calls InitFromIsolate to complete
85 86 87 88
  // read-only heap initialization.
  void DeseralizeIntoIsolate(Isolate* isolate, ReadOnlyDeserializer* des);
  // Initializes read-only heap from an already set-up isolate, copying
  // read-only roots from the isolate. This then seals the space off from
89 90
  // further writes, marks it as read-only and detaches it from the heap
  // (unless sharing is disabled).
91 92 93
  void InitFromIsolate(Isolate* isolate);

  bool init_complete_ = false;
94 95 96
  ReadOnlySpace* read_only_space_ = nullptr;
  std::vector<Object> read_only_object_cache_;

97
#ifdef V8_SHARED_RO_HEAP
98 99
#ifdef DEBUG
  // The checksum of the blob the read-only heap was deserialized from, if any.
100
  base::Optional<uint32_t> read_only_blob_checksum_;
101 102
#endif  // DEBUG

103
  Address read_only_roots_[kEntriesCount];
104 105

  V8_EXPORT_PRIVATE static ReadOnlyHeap* shared_ro_heap_;
106
#endif  // V8_SHARED_RO_HEAP
107

108 109 110 111
  explicit ReadOnlyHeap(ReadOnlySpace* ro_space) : read_only_space_(ro_space) {}
  DISALLOW_COPY_AND_ASSIGN(ReadOnlyHeap);
};

112
// This class enables iterating over all read-only heap objects.
113
class V8_EXPORT_PRIVATE ReadOnlyHeapObjectIterator {
114
 public:
115 116
  explicit ReadOnlyHeapObjectIterator(ReadOnlyHeap* ro_heap);
  explicit ReadOnlyHeapObjectIterator(ReadOnlySpace* ro_space);
117

118
  HeapObject Next();
119 120 121

 private:
  ReadOnlySpace* const ro_space_;
122
  std::vector<ReadOnlyPage*>::const_iterator current_page_;
123 124 125
  Address current_addr_;
};

126 127 128 129
}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_READ_ONLY_HEAP_H_