1 //===--------------- MapperJITLinkMemoryManager.h -*- 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 // Implements JITLinkMemoryManager using MemoryMapper
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
14 #define LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
16 #include "llvm/ADT/IntervalMap.h"
17 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
18 #include "llvm/ExecutionEngine/Orc/MemoryMapper.h"
23 class MapperJITLinkMemoryManager : public jitlink::JITLinkMemoryManager {
25 MapperJITLinkMemoryManager(size_t ReservationGranularity,
26 std::unique_ptr<MemoryMapper> Mapper);
28 template <class MemoryMapperType, class... Args>
29 static Expected<std::unique_ptr<MapperJITLinkMemoryManager>>
30 CreateWithMapper(size_t ReservationGranularity, Args &&...A) {
31 auto Mapper = MemoryMapperType::Create(std::forward<Args>(A)...);
33 return Mapper.takeError();
35 return std::make_unique<MapperJITLinkMemoryManager>(ReservationGranularity,
39 void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
40 OnAllocatedFunction OnAllocated) override;
41 // synchronous overload
42 using JITLinkMemoryManager::allocate;
44 void deallocate(std::vector<FinalizedAlloc> Allocs,
45 OnDeallocatedFunction OnDeallocated) override;
46 // synchronous overload
47 using JITLinkMemoryManager::deallocate;
54 // We reserve multiples of this from the executor address space
55 size_t ReservationUnits;
57 // Ranges that have been reserved in executor but not yet allocated
58 using AvailableMemoryMap = IntervalMap<ExecutorAddr, bool>;
59 AvailableMemoryMap::Allocator AMAllocator;
60 IntervalMap<ExecutorAddr, bool> AvailableMemory;
62 // Ranges that have been reserved in executor and already allocated
63 DenseMap<ExecutorAddr, ExecutorAddrDiff> UsedMemory;
65 std::unique_ptr<MemoryMapper> Mapper;
68 } // end namespace orc
69 } // end namespace llvm
71 #endif // LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H