|
| 1 | +//===- SimpleNativeMemoryMap.h -- Mem via standard host OS APIs -*- 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 | +// SimpleNativeMemoryMap and related APIs. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef ORC_RT_SIMPLENATIVEMEMORYMAP_H |
| 14 | +#define ORC_RT_SIMPLENATIVEMEMORYMAP_H |
| 15 | + |
| 16 | +#include "orc-rt/AllocAction.h" |
| 17 | +#include "orc-rt/Error.h" |
| 18 | +#include "orc-rt/MemoryFlags.h" |
| 19 | +#include "orc-rt/ResourceManager.h" |
| 20 | +#include "orc-rt/SPSWrapperFunction.h" |
| 21 | +#include "orc-rt/move_only_function.h" |
| 22 | + |
| 23 | +#include <map> |
| 24 | +#include <mutex> |
| 25 | +#include <unordered_map> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace orc_rt { |
| 29 | + |
| 30 | +/// JIT'd memory management backend. |
| 31 | +/// |
| 32 | +/// Intances can: |
| 33 | +/// 1. Reserve address space. |
| 34 | +/// 2. Finalize memory regions within reserved memory (copying content, |
| 35 | +/// applying permissions, running finalize actions, and recording |
| 36 | +/// deallocate actions). |
| 37 | +/// 3. Deallocate memory regions within reserved memory (running |
| 38 | +/// deallocate actions and making memory available for future |
| 39 | +/// finalize calls (if the system permits this). |
| 40 | +/// 4. Release address space, deallocating any not-yet-deallocated finalized |
| 41 | +/// regions, and returning the address space to the system for reuse (if |
| 42 | +/// the system permits). |
| 43 | +class SimpleNativeMemoryMap : public ResourceManager { |
| 44 | +public: |
| 45 | + /// Reserves a slab of contiguous address space for allocation. |
| 46 | + /// |
| 47 | + /// Returns the base address of the allocated memory. |
| 48 | + using OnReserveCompleteFn = move_only_function<void(Expected<void *>)>; |
| 49 | + void reserve(OnReserveCompleteFn &&OnComplete, size_t Size); |
| 50 | + |
| 51 | + /// Release a slab of contiguous address space back to the system. |
| 52 | + using OnReleaseCompleteFn = move_only_function<void(Error)>; |
| 53 | + void release(OnReleaseCompleteFn &&OnComplete, void *Addr); |
| 54 | + |
| 55 | + struct FinalizeRequest { |
| 56 | + struct Segment { |
| 57 | + enum class ContentType : uint8_t { Uninitialized, ZeroFill, Regular }; |
| 58 | + |
| 59 | + Segment() = default; |
| 60 | + Segment(void *Address, size_t Size, AllocGroup G, ContentType C) |
| 61 | + : Address(Address), Size(Size), G(G), C(C) {} |
| 62 | + |
| 63 | + void *Address = nullptr; |
| 64 | + size_t Size = 0; |
| 65 | + AllocGroup G; |
| 66 | + ContentType C = ContentType::Uninitialized; |
| 67 | + char *data() { return reinterpret_cast<char *>(Address); } |
| 68 | + size_t size() const { return Size; } |
| 69 | + }; |
| 70 | + |
| 71 | + std::vector<Segment> Segments; |
| 72 | + std::vector<AllocActionPair> AAPs; |
| 73 | + }; |
| 74 | + |
| 75 | + /// Writes content into the requested ranges, applies permissions, and |
| 76 | + /// performs allocation actions. |
| 77 | + using OnFinalizeCompleteFn = move_only_function<void(Expected<void *>)>; |
| 78 | + void finalize(OnFinalizeCompleteFn &&OnComplete, FinalizeRequest FR); |
| 79 | + |
| 80 | + /// Runs deallocation actions and resets memory permissions for the requested |
| 81 | + /// memory. |
| 82 | + using OnDeallocateCompleteFn = move_only_function<void(Error)>; |
| 83 | + void deallocate(OnDeallocateCompleteFn &&OnComplete, void *Base); |
| 84 | + |
| 85 | + void detach(ResourceManager::OnCompleteFn OnComplete) override; |
| 86 | + void shutdown(ResourceManager::OnCompleteFn OnComplete) override; |
| 87 | + |
| 88 | +private: |
| 89 | + struct SlabInfo { |
| 90 | + SlabInfo(size_t Size) : Size(Size) {} |
| 91 | + size_t Size; |
| 92 | + std::unordered_map<void *, std::vector<AllocAction>> DeallocActions; |
| 93 | + }; |
| 94 | + |
| 95 | + void shutdownNext(OnCompleteFn OnComplete, std::vector<void *> Bases); |
| 96 | + Error makeBadSlabError(void *Base, const char *Op); |
| 97 | + SlabInfo *findSlabInfoFor(void *Base); |
| 98 | + Error recordDeallocActions(void *Base, |
| 99 | + std::vector<AllocAction> DeallocActions); |
| 100 | + |
| 101 | + std::mutex M; |
| 102 | + std::map<void *, SlabInfo> Slabs; |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace orc_rt |
| 106 | + |
| 107 | +ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_reserve_sps_wrapper( |
| 108 | + orc_rt_SessionRef Session, void *CallCtx, |
| 109 | + orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes); |
| 110 | + |
| 111 | +ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_release_sps_wrapper( |
| 112 | + orc_rt_SessionRef Session, void *CallCtx, |
| 113 | + orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes); |
| 114 | + |
| 115 | +ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_finalize_sps_wrapper( |
| 116 | + orc_rt_SessionRef Session, void *CallCtx, |
| 117 | + orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes); |
| 118 | + |
| 119 | +ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_deallocate_sps_wrapper( |
| 120 | + orc_rt_SessionRef Session, void *CallCtx, |
| 121 | + orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes); |
| 122 | + |
| 123 | +#endif // ORC_RT_SIMPLENATIVEMEMORYMAP_H |
0 commit comments