1 //===----------- ExecutorSharedMemoryMapperService.h ------------*- 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 #ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE_H
10 #define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
14 #include "llvm/ExecutionEngine/Orc/TargetProcess/ExecutorBootstrapService.h"
15 
16 #include <atomic>
17 #include <mutex>
18 
19 #if defined(_WIN32)
20 #include <windows.h>
21 #endif
22 
23 namespace llvm {
24 namespace orc {
25 namespace rt_bootstrap {
26 
27 class ExecutorSharedMemoryMapperService final
28     : public ExecutorBootstrapService {
29 public:
30   ~ExecutorSharedMemoryMapperService(){};
31 
32   Expected<std::pair<ExecutorAddr, std::string>> reserve(uint64_t Size);
33   Expected<ExecutorAddr> initialize(ExecutorAddr Reservation,
34                                     tpctypes::SharedMemoryFinalizeRequest &FR);
35 
36   Error deinitialize(const std::vector<ExecutorAddr> &Bases);
37   Error release(const std::vector<ExecutorAddr> &Bases);
38 
39   Error shutdown() override;
40   void addBootstrapSymbols(StringMap<ExecutorAddr> &M) override;
41 
42 private:
43   struct Allocation {
44     std::vector<shared::WrapperFunctionCall> DeinitializationActions;
45   };
46   using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
47 
48   struct Reservation {
49     size_t Size;
50     std::vector<ExecutorAddr> Allocations;
51 #if defined(_WIN32)
52     HANDLE SharedMemoryFile;
53 #endif
54   };
55   using ReservationMap = DenseMap<void *, Reservation>;
56 
57   static llvm::orc::shared::CWrapperFunctionResult
58   reserveWrapper(const char *ArgData, size_t ArgSize);
59 
60   static llvm::orc::shared::CWrapperFunctionResult
61   initializeWrapper(const char *ArgData, size_t ArgSize);
62 
63   static llvm::orc::shared::CWrapperFunctionResult
64   deinitializeWrapper(const char *ArgData, size_t ArgSize);
65 
66   static llvm::orc::shared::CWrapperFunctionResult
67   releaseWrapper(const char *ArgData, size_t ArgSize);
68 
69 #if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
70   std::atomic<int> SharedMemoryCount{0};
71 #endif
72 
73   std::mutex Mutex;
74   ReservationMap Reservations;
75   AllocationMap Allocations;
76 };
77 
78 } // namespace rt_bootstrap
79 } // namespace orc
80 } // namespace llvm
81 #endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESS_EXECUTORSHAREDMEMORYMAPPERSERVICE_H
82