1 //===-- JITLinkMemoryManager.h - JITLink mem manager interface --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Contains the JITLinkMemoryManager interface.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_JITLINKMEMORYMANAGER_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_JITLINKMEMORYMANAGER_H
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Memory.h"
25 /// Manages allocations of JIT memory.
27 /// Instances of this class may be accessed concurrently from multiple threads
28 /// and their implemetations should include any necessary synchronization.
29 class JITLinkMemoryManager
{
31 using ProtectionFlags
= sys::Memory::ProtectionFlags
;
33 class SegmentRequest
{
35 SegmentRequest() = default;
36 SegmentRequest(size_t ContentSize
, unsigned ContentAlign
,
37 uint64_t ZeroFillSize
, unsigned ZeroFillAlign
)
38 : ContentSize(ContentSize
), ZeroFillSize(ZeroFillSize
),
39 ContentAlign(ContentAlign
), ZeroFillAlign(ZeroFillAlign
) {}
40 size_t getContentSize() const { return ContentSize
; }
41 unsigned getContentAlignment() const { return ContentAlign
; }
42 uint64_t getZeroFillSize() const { return ZeroFillSize
; }
43 unsigned getZeroFillAlignment() const { return ZeroFillAlign
; }
46 size_t ContentSize
= 0;
47 uint64_t ZeroFillSize
= 0;
48 unsigned ContentAlign
= 0;
49 unsigned ZeroFillAlign
= 0;
52 using SegmentsRequestMap
= DenseMap
<unsigned, SegmentRequest
>;
54 /// Represents an allocation created by the memory manager.
56 /// An allocation object is responsible for allocating and owning jit-linker
57 /// working and target memory, and for transfering from working to target
62 using FinalizeContinuation
= std::function
<void(Error
)>;
64 virtual ~Allocation();
66 /// Should return the address of linker working memory for the segment with
67 /// the given protection flags.
68 virtual MutableArrayRef
<char> getWorkingMemory(ProtectionFlags Seg
) = 0;
70 /// Should return the final address in the target process where the segment
72 virtual JITTargetAddress
getTargetMemory(ProtectionFlags Seg
) = 0;
74 /// Should transfer from working memory to target memory, and release
76 virtual void finalizeAsync(FinalizeContinuation OnFinalize
) = 0;
78 /// Should deallocate target memory.
79 virtual Error
deallocate() = 0;
82 virtual ~JITLinkMemoryManager();
84 /// Create an Allocation object.
85 virtual Expected
<std::unique_ptr
<Allocation
>>
86 allocate(const SegmentsRequestMap
&Request
) = 0;
89 /// A JITLinkMemoryManager that allocates in-process memory.
90 class InProcessMemoryManager
: public JITLinkMemoryManager
{
92 Expected
<std::unique_ptr
<Allocation
>>
93 allocate(const SegmentsRequestMap
&Request
) override
;
96 } // end namespace jitlink
97 } // end namespace llvm
99 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H