]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h
Fix a memory leak in if_delgroups() introduced in r334118.
[FreeBSD/FreeBSD.git] / contrib / 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 <cstdint>
21
22 namespace llvm {
23 namespace jitlink {
24
25 /// Manages allocations of JIT memory.
26 ///
27 /// Instances of this class may be accessed concurrently from multiple threads
28 /// and their implemetations should include any necessary synchronization.
29 class JITLinkMemoryManager {
30 public:
31   using ProtectionFlags = sys::Memory::ProtectionFlags;
32
33   class SegmentRequest {
34   public:
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; }
44
45   private:
46     size_t ContentSize = 0;
47     uint64_t ZeroFillSize = 0;
48     unsigned ContentAlign = 0;
49     unsigned ZeroFillAlign = 0;
50   };
51
52   using SegmentsRequestMap = DenseMap<unsigned, SegmentRequest>;
53
54   /// Represents an allocation created by the memory manager.
55   ///
56   /// An allocation object is responsible for allocating and owning jit-linker
57   /// working and target memory, and for transfering from working to target
58   /// memory.
59   ///
60   class Allocation {
61   public:
62     using FinalizeContinuation = std::function<void(Error)>;
63
64     virtual ~Allocation();
65
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;
69
70     /// Should return the final address in the target process where the segment
71     /// will reside.
72     virtual JITTargetAddress getTargetMemory(ProtectionFlags Seg) = 0;
73
74     /// Should transfer from working memory to target memory, and release
75     /// working memory.
76     virtual void finalizeAsync(FinalizeContinuation OnFinalize) = 0;
77
78     /// Should deallocate target memory.
79     virtual Error deallocate() = 0;
80   };
81
82   virtual ~JITLinkMemoryManager();
83
84   /// Create an Allocation object.
85   virtual Expected<std::unique_ptr<Allocation>>
86   allocate(const SegmentsRequestMap &Request) = 0;
87 };
88
89 /// A JITLinkMemoryManager that allocates in-process memory.
90 class InProcessMemoryManager : public JITLinkMemoryManager {
91 public:
92   Expected<std::unique_ptr<Allocation>>
93   allocate(const SegmentsRequestMap &Request) override;
94 };
95
96 } // end namespace jitlink
97 } // end namespace llvm
98
99 #endif // LLVM_EXECUTIONENGINE_JITLINK_JITLINK_H