/src/hermes/include/hermes/VM/SegmentInfo.h
Line | Count | Source (jump to first uncovered line) |
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_SEGMENTINFO_H |
9 | | #define HERMES_VM_SEGMENTINFO_H |
10 | | |
11 | | #include "hermes/VM/AlignedStorage.h" |
12 | | |
13 | | /// Define this macro here because SegmentInfo is necessarily a dependency of |
14 | | /// anything using compressed pointers. |
15 | | #if defined(HERMESVM_ALLOW_COMPRESSED_POINTERS) && LLVM_PTR_SIZE == 8 && \ |
16 | | (defined(HERMESVM_GC_HADES) || defined(HERMESVM_GC_RUNTIME)) |
17 | | /// \macro HERMESVM_COMPRESSED_POINTERS |
18 | | /// \brief If defined, store pointers as 32 bits in GC-managed Hermes objects. |
19 | | #define HERMESVM_COMPRESSED_POINTERS |
20 | | #ifdef HERMESVM_ALLOW_CONTIGUOUS_HEAP |
21 | | #define HERMESVM_CONTIGUOUS_HEAP |
22 | | #endif |
23 | | #endif |
24 | | |
25 | | namespace hermes { |
26 | | namespace vm { |
27 | | |
28 | | /// The very beginning of a segment must contain this small structure, which can |
29 | | /// contain segment-specific information. See AlignedHeapSegment for details on |
30 | | /// how it is stored. |
31 | | struct SegmentInfo { |
32 | | /// Returns the index of the segment containing \p lowLim, which is required |
33 | | /// to be the start of its containing segment. (This can allow extra |
34 | | /// efficiency, in cases where the segment start has already been computed.) |
35 | 0 | static unsigned segmentIndexFromStart(const void *lowLim) { |
36 | 0 | assert(lowLim == AlignedStorage::start(lowLim) && "Precondition."); |
37 | 0 | return get(lowLim)->index; |
38 | 0 | } |
39 | | |
40 | | /// Returns the index of the segment containing \p ptr. |
41 | 0 | static unsigned segmentIndex(const void *ptr) { |
42 | 0 | return segmentIndexFromStart(AlignedStorage::start(ptr)); |
43 | 0 | } |
44 | | |
45 | | /// Requires that \p lowLim is the start address of a segment, and sets |
46 | | /// that segment's index to \p index. |
47 | 94 | static void setSegmentIndexFromStart(void *lowLim, unsigned index) { |
48 | 94 | assert(lowLim == AlignedStorage::start(lowLim) && "Precondition."); |
49 | 94 | get(lowLim)->index = index; |
50 | 94 | } |
51 | | |
52 | | private: |
53 | | /// Given the \p lowLim of some valid AlignedStorage's memory region, returns |
54 | | /// a pointer to the SegmentInfo laid out in that storage, |
55 | | /// assuming it exists. |
56 | 94 | static SegmentInfo *get(void *lowLim) { |
57 | 94 | assert(lowLim == AlignedStorage::start(lowLim) && "Precondition."); |
58 | 94 | return reinterpret_cast<SegmentInfo *>(lowLim); |
59 | 94 | } |
60 | | |
61 | 0 | static const SegmentInfo *get(const void *lowLim) { |
62 | 0 | assert(lowLim == AlignedStorage::start(lowLim) && "Precondition."); |
63 | 0 | return reinterpret_cast<const SegmentInfo *>(lowLim); |
64 | 0 | } |
65 | | |
66 | | unsigned index; |
67 | | }; |
68 | | |
69 | | } // namespace vm |
70 | | } // namespace hermes |
71 | | |
72 | | #endif |