LLVM 17.0.0git
MemoryFlags.h
Go to the documentation of this file.
1//===-------- MemoryFlags.h - Memory allocation flags -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines types and operations related to memory protection and allocation
10// lifetimes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_MEMORYFLAGS_H
15#define LLVM_EXECUTIONENGINE_ORC_SHARED_MEMORYFLAGS_H
16
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/Memory.h"
22
23namespace llvm {
24namespace orc {
25
26/// Describes Read/Write/Exec permissions for memory.
27enum class MemProt {
28 None = 0,
29 Read = 1U << 0,
30 Write = 1U << 1,
31 Exec = 1U << 2,
32 LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ Exec)
33};
34
35/// Print a MemProt as an RWX triple.
37 return OS << (((MP & MemProt::Read) != MemProt::None) ? 'R' : '-')
38 << (((MP & MemProt::Write) != MemProt::None) ? 'W' : '-')
39 << (((MP & MemProt::Exec) != MemProt::None) ? 'X' : '-');
40}
41
42/// Convert a MemProt value to a corresponding sys::Memory::ProtectionFlags
43/// value.
45 std::underlying_type_t<sys::Memory::ProtectionFlags> PF = 0;
46 if ((MP & MemProt::Read) != MemProt::None)
48 if ((MP & MemProt::Write) != MemProt::None)
50 if ((MP & MemProt::Exec) != MemProt::None)
52 return static_cast<sys::Memory::ProtectionFlags>(PF);
53}
54
55/// Convert a sys::Memory::ProtectionFlags value to a corresponding MemProt
56/// value.
59 if (PF & sys::Memory::MF_READ)
60 MP |= MemProt::Read;
61 if (PF & sys::Memory::MF_WRITE)
62 MP |= MemProt::Write;
63 if (PF & sys::Memory::MF_EXEC)
64 MP |= MemProt::None;
65 return MP;
66}
67
68/// Describes a memory deallocation policy for memory to be allocated by a
69/// JITLinkMemoryManager.
70///
71/// All memory allocated by a call to JITLinkMemoryManager::allocate should be
72/// deallocated if a call is made to
73/// JITLinkMemoryManager::InFlightAllocation::abandon. The policies below apply
74/// to finalized allocations.
75enum class MemDeallocPolicy {
76 /// Standard memory should be deallocated when the deallocate method is called
77 /// for the finalized allocation.
79
80 /// Finalize memory should be overwritten and then deallocated after all
81 /// finalization functions have been run.
83};
84
85/// Print a MemDeallocPolicy.
87 return OS << (MDP == MemDeallocPolicy::Standard ? "standard" : "finalize");
88}
89
90/// A pair of memory protections and allocation policies.
91///
92/// Optimized for use as a small map key.
94 friend struct llvm::DenseMapInfo<AllocGroup>;
95
96 using underlying_type = uint8_t;
97 static constexpr unsigned BitsForProt = 3;
98 static constexpr unsigned BitsForDeallocPolicy = 1;
99 static constexpr unsigned MaxIdentifiers =
100 1U << (BitsForProt + BitsForDeallocPolicy);
101
102public:
103 static constexpr unsigned NumGroups = MaxIdentifiers;
104
105 /// Create a default AllocGroup. No memory protections, standard
106 /// deallocation policy.
107 AllocGroup() = default;
108
109 /// Create an AllocGroup from a MemProt only -- uses
110 /// MemoryDeallocationPolicy::Standard.
111 AllocGroup(MemProt MP) : Id(static_cast<underlying_type>(MP)) {}
112
113 /// Create an AllocGroup from a MemProt and a MemoryDeallocationPolicy.
115 : Id(static_cast<underlying_type>(MP) |
116 (static_cast<underlying_type>(MDP) << BitsForProt)) {}
117
118 /// Returns the MemProt for this group.
120 return static_cast<MemProt>(Id & ((1U << BitsForProt) - 1));
121 }
122
123 /// Returns the MemoryDeallocationPolicy for this group.
125 return static_cast<MemDeallocPolicy>(Id >> BitsForProt);
126 }
127
128 friend bool operator==(const AllocGroup &LHS, const AllocGroup &RHS) {
129 return LHS.Id == RHS.Id;
130 }
131
132 friend bool operator!=(const AllocGroup &LHS, const AllocGroup &RHS) {
133 return !(LHS == RHS);
134 }
135
136 friend bool operator<(const AllocGroup &LHS, const AllocGroup &RHS) {
137 return LHS.Id < RHS.Id;
138 }
139
140private:
141 AllocGroup(underlying_type RawId) : Id(RawId) {}
142 underlying_type Id = 0;
143};
144
145/// A specialized small-map for AllocGroups.
146///
147/// Iteration order is guaranteed to match key ordering.
148template <typename T> class AllocGroupSmallMap {
149private:
150 using ElemT = std::pair<AllocGroup, T>;
152
153 static bool compareKey(const ElemT &E, const AllocGroup &G) {
154 return E.first < G;
155 }
156
157public:
158 using iterator = typename VectorTy::iterator;
159
161 AllocGroupSmallMap(std::initializer_list<std::pair<AllocGroup, T>> Inits)
162 : Elems(Inits) {
164 }
165
166 iterator begin() { return Elems.begin(); }
167 iterator end() { return Elems.end(); }
169 auto I = lower_bound(Elems, G, compareKey);
170 return (I->first == G) ? I : end();
171 }
172
173 bool empty() const { return Elems.empty(); }
174 size_t size() const { return Elems.size(); }
175
177 auto I = lower_bound(Elems, G, compareKey);
178 if (I == Elems.end() || I->first != G)
179 I = Elems.insert(I, std::make_pair(G, T()));
180 return I->second;
181 }
182
183private:
184 VectorTy Elems;
185};
186
187/// Print an AllocGroup.
189 return OS << '(' << AG.getMemProt() << ", " << AG.getMemDeallocPolicy()
190 << ')';
191}
192
193} // end namespace orc
194
195template <> struct DenseMapInfo<orc::MemProt> {
196 static inline orc::MemProt getEmptyKey() { return orc::MemProt(~uint8_t(0)); }
198 return orc::MemProt(~uint8_t(0) - 1);
199 }
200 static unsigned getHashValue(const orc::MemProt &Val) {
201 using UT = std::underlying_type_t<orc::MemProt>;
202 return DenseMapInfo<UT>::getHashValue(static_cast<UT>(Val));
203 }
204 static bool isEqual(const orc::MemProt &LHS, const orc::MemProt &RHS) {
205 return LHS == RHS;
206 }
207};
208
209template <> struct DenseMapInfo<orc::AllocGroup> {
210 static inline orc::AllocGroup getEmptyKey() {
211 return orc::AllocGroup(~uint8_t(0));
212 }
214 return orc::AllocGroup(~uint8_t(0) - 1);
215 }
216 static unsigned getHashValue(const orc::AllocGroup &Val) {
218 }
219 static bool isEqual(const orc::AllocGroup &LHS, const orc::AllocGroup &RHS) {
220 return LHS == RHS;
221 }
222};
223
224} // end namespace llvm
225
226#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_MEMORYFLAGS_H
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines DenseMapInfo traits for DenseMap.
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define T
This file contains some templates that are useful if you are working with the STL at all.
Value * RHS
Value * LHS
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:809
A specialized small-map for AllocGroups.
Definition: MemoryFlags.h:148
iterator find(AllocGroup G)
Definition: MemoryFlags.h:168
T & operator[](AllocGroup G)
Definition: MemoryFlags.h:176
AllocGroupSmallMap(std::initializer_list< std::pair< AllocGroup, T > > Inits)
Definition: MemoryFlags.h:161
typename VectorTy::iterator iterator
Definition: MemoryFlags.h:158
A pair of memory protections and allocation policies.
Definition: MemoryFlags.h:93
MemDeallocPolicy getMemDeallocPolicy() const
Returns the MemoryDeallocationPolicy for this group.
Definition: MemoryFlags.h:124
static constexpr unsigned NumGroups
Definition: MemoryFlags.h:103
MemProt getMemProt() const
Returns the MemProt for this group.
Definition: MemoryFlags.h:119
friend bool operator==(const AllocGroup &LHS, const AllocGroup &RHS)
Definition: MemoryFlags.h:128
AllocGroup(MemProt MP)
Create an AllocGroup from a MemProt only – uses MemoryDeallocationPolicy::Standard.
Definition: MemoryFlags.h:111
AllocGroup()=default
Create a default AllocGroup.
friend bool operator<(const AllocGroup &LHS, const AllocGroup &RHS)
Definition: MemoryFlags.h:136
AllocGroup(MemProt MP, MemDeallocPolicy MDP)
Create an AllocGroup from a MemProt and a MemoryDeallocationPolicy.
Definition: MemoryFlags.h:114
friend bool operator!=(const AllocGroup &LHS, const AllocGroup &RHS)
Definition: MemoryFlags.h:132
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
MemProt
Describes Read/Write/Exec permissions for memory.
Definition: MemoryFlags.h:27
MemProt fromSysMemoryProtectionFlags(sys::Memory::ProtectionFlags PF)
Convert a sys::Memory::ProtectionFlags value to a corresponding MemProt value.
Definition: MemoryFlags.h:57
MemDeallocPolicy
Describes a memory deallocation policy for memory to be allocated by a JITLinkMemoryManager.
Definition: MemoryFlags.h:75
@ Standard
Standard memory should be deallocated when the deallocate method is called for the finalized allocati...
@ Finalize
Finalize memory should be overwritten and then deallocated after all finalization functions have been...
sys::Memory::ProtectionFlags toSysMemoryProtectionFlags(MemProt MP)
Convert a MemProt value to a corresponding sys::Memory::ProtectionFlags value.
Definition: MemoryFlags.h:44
raw_ostream & operator<<(raw_ostream &OS, const SymbolStringPtr &Sym)
Render a SymbolStringPtr.
Definition: DebugUtils.cpp:141
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1683
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1923
static bool isEqual(const orc::AllocGroup &LHS, const orc::AllocGroup &RHS)
Definition: MemoryFlags.h:219
static orc::AllocGroup getEmptyKey()
Definition: MemoryFlags.h:210
static orc::AllocGroup getTombstoneKey()
Definition: MemoryFlags.h:213
static unsigned getHashValue(const orc::AllocGroup &Val)
Definition: MemoryFlags.h:216
static orc::MemProt getEmptyKey()
Definition: MemoryFlags.h:196
static bool isEqual(const orc::MemProt &LHS, const orc::MemProt &RHS)
Definition: MemoryFlags.h:204
static orc::MemProt getTombstoneKey()
Definition: MemoryFlags.h:197
static unsigned getHashValue(const orc::MemProt &Val)
Definition: MemoryFlags.h:200
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:51
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:1477