]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h
MFV 2.0-rc2
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / ExecutionEngine / JITLink / JITLinkMemoryManager.h
1 //===-- JITLinkMemoryManager.h - JITLink mem manager interface --*- 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 // Contains the JITLinkMemoryManager interface.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_JITLINKMEMORYMANAGER_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_JITLINKMEMORYMANAGER_H
15
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Memory.h"
20 #include "llvm/Support/MSVCErrorWorkarounds.h"
21
22 #include <cstdint>
23 #include <future>
24
25 namespace llvm {
26 namespace jitlink {
27
28 /// Manages allocations of JIT memory.
29 ///
30 /// Instances of this class may be accessed concurrently from multiple threads
31 /// and their implemetations should include any necessary synchronization.
32 class JITLinkMemoryManager {
33 public:
34   using ProtectionFlags = sys::Memory::ProtectionFlags;
35
36   class SegmentRequest {
37   public:
38     SegmentRequest() = default;
39     SegmentRequest(uint64_t Alignment, size_t ContentSize,
40                    uint64_t ZeroFillSize)
41         : Alignment(Alignment), ContentSize(ContentSize),
42           ZeroFillSize(ZeroFillSize) {
43       assert(isPowerOf2_32(Alignment) && "Alignment must be power of 2");
44     }
45     uint64_t getAlignment() const { return Alignment; }
46     size_t getContentSize() const { return ContentSize; }
47     uint64_t getZeroFillSize() const { return ZeroFillSize; }
48   private:
49     uint64_t Alignment = 0;
50     size_t ContentSize = 0;
51     uint64_t ZeroFillSize = 0;
52   };
53
54   using SegmentsRequestMap = DenseMap<unsigned, SegmentRequest>;
55
56   /// Represents an allocation created by the memory manager.
57   ///
58   /// An allocation object is responsible for allocating and owning jit-linker
59   /// working and target memory, and for transfering from working to target
60   /// memory.
61   ///
62   class Allocation {
63   public:
64     using FinalizeContinuation = std::function<void(Error)>;
65
66     virtual ~Allocation();
67
68     /// Should return the address of linker working memory for the segment with
69     /// the given protection flags.
70     virtual MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) = 0;
71
72     /// Should return the final address in the target process where the segment
73     /// will reside.
74     virtual JITTargetAddress getTargetMemory(ProtectionFlags Seg) = 0;
75
76     /// Should transfer from working memory to target memory, and release
77     /// working memory.
78     virtual void finalizeAsync(FinalizeContinuation OnFinalize) = 0;
79
80     /// Calls finalizeAsync and waits for completion.
81     Error finalize() {
82       std::promise<MSVCPError> FinalizeResultP;
83       auto FinalizeResultF = FinalizeResultP.get_future();
84       finalizeAsync(
85           [&](Error Err) { FinalizeResultP.set_value(std::move(Err)); });
86       return FinalizeResultF.get();
87     }
88
89     /// Should deallocate target memory.
90     virtual Error deallocate() = 0;
91   };
92
93   virtual ~JITLinkMemoryManager();
94
95   /// Create an Allocation object.
96   virtual Expected<std::unique_ptr<Allocation>>
97   allocate(const SegmentsRequestMap &Request) = 0;
98 };
99
100 /// A JITLinkMemoryManager that allocates in-process memory.
101 class InProcessMemoryManager : public JITLinkMemoryManager {
102 public:
103   Expected<std::unique_ptr<Allocation>>
104   allocate(const SegmentsRequestMap &Request) override;
105 };
106
107 } // end namespace jitlink
108 } // end namespace llvm
109
110 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H