1// Copyright 2011 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_HANDLES_LOCAL_HANDLES_H_
6#define V8_HANDLES_LOCAL_HANDLES_H_
7
8#include "include/v8-internal.h"
9#include "src/base/functional.h"
10#include "src/base/macros.h"
11#include "src/handles/handles.h"
12#include "src/heap/local-heap.h"
13
14namespace v8 {
15namespace internal {
16
17class RootVisitor;
18
19class LocalHandles {
20 public:
21  LocalHandles();
22  ~LocalHandles();
23
24  void Iterate(RootVisitor* visitor);
25
26#ifdef DEBUG
27  bool Contains(Address* location);
28#endif
29
30 private:
31  HandleScopeData scope_;
32  std::vector<Address*> blocks_;
33
34  V8_EXPORT_PRIVATE Address* AddBlock();
35  V8_EXPORT_PRIVATE void RemoveUnusedBlocks();
36
37#ifdef ENABLE_HANDLE_ZAPPING
38  V8_EXPORT_PRIVATE static void ZapRange(Address* start, Address* end);
39#endif
40
41  friend class LocalHandleScope;
42};
43
44class V8_NODISCARD LocalHandleScope {
45 public:
46  explicit inline LocalHandleScope(LocalIsolate* local_isolate);
47  explicit inline LocalHandleScope(LocalHeap* local_heap);
48  inline ~LocalHandleScope();
49  LocalHandleScope(const LocalHandleScope&) = delete;
50  LocalHandleScope& operator=(const LocalHandleScope&) = delete;
51
52  template <typename T>
53  Handle<T> CloseAndEscape(Handle<T> handle_value);
54
55  V8_INLINE static Address* GetHandle(LocalHeap* local_heap, Address value);
56
57 private:
58  // Prevent heap allocation or illegal handle scopes.
59  void* operator new(size_t size) = delete;
60  void operator delete(void* size_t) = delete;
61
62  // Close the handle scope resetting limits to a previous state.
63  static inline void CloseScope(LocalHeap* local_heap, Address* prev_next,
64                                Address* prev_limit);
65  V8_EXPORT_PRIVATE static void CloseMainThreadScope(LocalHeap* local_heap,
66                                                     Address* prev_next,
67                                                     Address* prev_limit);
68
69  V8_EXPORT_PRIVATE void OpenMainThreadScope(LocalHeap* local_heap);
70
71  V8_EXPORT_PRIVATE static Address* GetMainThreadHandle(LocalHeap* local_heap,
72                                                        Address value);
73
74  LocalHeap* local_heap_;
75  Address* prev_limit_;
76  Address* prev_next_;
77};
78
79}  // namespace internal
80}  // namespace v8
81
82#endif  // V8_HANDLES_LOCAL_HANDLES_H_
83