MapperJITLinkMemoryManager.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------------- MapperJITLinkMemoryManager.h -*- C++ -*---------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Implements JITLinkMemoryManager using MemoryMapper
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
  18. #define LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
  19. #include "llvm/ADT/IntervalMap.h"
  20. #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
  21. #include "llvm/ExecutionEngine/Orc/MemoryMapper.h"
  22. namespace llvm {
  23. namespace orc {
  24. class MapperJITLinkMemoryManager : public jitlink::JITLinkMemoryManager {
  25. public:
  26. MapperJITLinkMemoryManager(size_t ReservationGranularity,
  27. 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)...);
  32. if (!Mapper)
  33. return Mapper.takeError();
  34. return std::make_unique<MapperJITLinkMemoryManager>(ReservationGranularity,
  35. std::move(*Mapper));
  36. }
  37. void allocate(const jitlink::JITLinkDylib *JD, jitlink::LinkGraph &G,
  38. OnAllocatedFunction OnAllocated) override;
  39. // synchronous overload
  40. using JITLinkMemoryManager::allocate;
  41. void deallocate(std::vector<FinalizedAlloc> Allocs,
  42. OnDeallocatedFunction OnDeallocated) override;
  43. // synchronous overload
  44. using JITLinkMemoryManager::deallocate;
  45. private:
  46. class InFlightAlloc;
  47. std::mutex Mutex;
  48. // We reserve multiples of this from the executor address space
  49. size_t ReservationUnits;
  50. // Ranges that have been reserved in executor but not yet allocated
  51. using AvailableMemoryMap = IntervalMap<ExecutorAddr, bool>;
  52. AvailableMemoryMap::Allocator AMAllocator;
  53. IntervalMap<ExecutorAddr, bool> AvailableMemory;
  54. // Ranges that have been reserved in executor and already allocated
  55. DenseMap<ExecutorAddr, ExecutorAddrDiff> UsedMemory;
  56. std::unique_ptr<MemoryMapper> Mapper;
  57. };
  58. } // end namespace orc
  59. } // end namespace llvm
  60. #endif // LLVM_EXECUTIONENGINE_ORC_MAPPERJITLINKMEMORYMANAGER_H
  61. #ifdef __GNUC__
  62. #pragma GCC diagnostic pop
  63. #endif