Coverage Report

Created: 2025-12-12 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hermes/include/hermes/VM/HeapRuntime.h
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#ifndef HERMES_VM_HEAPRUNTIME_H
9
#define HERMES_VM_HEAPRUNTIME_H
10
11
#include "hermes/VM/StorageProvider.h"
12
13
namespace hermes {
14
namespace vm {
15
/// Helper class to create a runtime object instantiated on the heap and manage
16
/// its lifetime via an aliased shared_ptr. It can be used with any
17
/// StorageProvider, and will consume the entirety of the next available segment
18
/// from the provider.
19
template <typename RT>
20
class HeapRuntime {
21
 public:
22
113
  ~HeapRuntime() {
23
113
    runtime_->~RT();
24
113
    sp_->deleteStorage(runtime_);
25
113
  }
26
27
  /// Allocate a segment and create an aliased shared_ptr that points to the
28
  /// start of the segment, but manages the lifetime of a HeapRuntime<RT>. The
29
  /// caller must instantiate an RT in the returned storage, and its destructor
30
  /// will automatically be called when the HeapRuntime is destroyed.
31
113
  static std::shared_ptr<RT> create(std::shared_ptr<StorageProvider> sp) {
32
113
    auto hrt = std::shared_ptr<HeapRuntime>(new HeapRuntime(std::move(sp)));
33
113
    return std::shared_ptr<RT>(hrt, hrt->runtime_);
34
113
  }
35
36
 private:
37
113
  HeapRuntime(std::shared_ptr<StorageProvider> sp) : sp_{std::move(sp)} {
38
113
    auto ptrOrError = sp_->newStorage("hermes-rt");
39
113
    if (!ptrOrError)
40
0
      hermes_fatal("Cannot initialize Runtime storage.", ptrOrError.getError());
41
113
    static_assert(sizeof(RT) < AlignedStorage::size(), "Segments too small.");
42
113
    runtime_ = static_cast<RT *>(*ptrOrError);
43
113
  }
44
45
  std::shared_ptr<StorageProvider> sp_;
46
  RT *runtime_;
47
};
48
} // namespace vm
49
} // namespace hermes
50
#endif // HERMES_VM_HEAPRUNTIME_H