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_CODE_OBJECT_REGISTRY_H_
6#define V8_HEAP_CODE_OBJECT_REGISTRY_H_
7
8#include <vector>
9
10#include "src/base/macros.h"
11#include "src/base/platform/mutex.h"
12#include "src/common/globals.h"
13
14namespace v8 {
15namespace internal {
16
17// The CodeObjectRegistry holds all start addresses of code objects of a given
18// MemoryChunk. Each MemoryChunk owns a separate CodeObjectRegistry. The
19// CodeObjectRegistry allows fast lookup from an inner pointer of a code object
20// to the actual code object.
21class V8_EXPORT_PRIVATE CodeObjectRegistry {
22 public:
23  void RegisterNewlyAllocatedCodeObject(Address code);
24  void RegisterAlreadyExistingCodeObject(Address code);
25  void Clear();
26  void Finalize();
27  bool Contains(Address code) const;
28  Address GetCodeObjectStartFromInnerAddress(Address address) const;
29
30 private:
31  // A vector of addresses, which may be sorted. This is set to 'mutable' so
32  // that it can be lazily sorted during GetCodeObjectStartFromInnerAddress.
33  mutable std::vector<Address> code_object_registry_;
34  mutable bool is_sorted_ = true;
35  mutable base::Mutex code_object_registry_mutex_;
36};
37
38}  // namespace internal
39}  // namespace v8
40
41#endif  // V8_HEAP_CODE_OBJECT_REGISTRY_H_
42