xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/Shared/AllocationActions.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- AllocationActions.h -- JITLink allocation support calls  -*- 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 // Structures for making memory allocation support calls.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
14 #define LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
15 
16 #include "llvm/ADT/FunctionExtras.h"
17 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
18 #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Memory.h"
21 
22 #include <vector>
23 
24 namespace llvm {
25 namespace orc {
26 namespace shared {
27 
28 /// A pair of WrapperFunctionCalls, one to be run at finalization time, one to
29 /// be run at deallocation time.
30 ///
31 /// AllocActionCallPairs should be constructed for paired operations (e.g.
32 /// __register_ehframe and __deregister_ehframe for eh-frame registration).
33 /// See comments for AllocActions for execution ordering.
34 ///
35 /// For unpaired operations one or the other member can be left unused, as
36 /// AllocationActionCalls with an FnAddr of zero will be skipped.
37 struct AllocActionCallPair {
38   WrapperFunctionCall Finalize;
39   WrapperFunctionCall Dealloc;
40 };
41 
42 /// A vector of allocation actions to be run for this allocation.
43 ///
44 /// Finalize allocations will be run in order at finalize time. Dealloc
45 /// actions will be run in reverse order at deallocation time.
46 using AllocActions = std::vector<AllocActionCallPair>;
47 
48 /// Returns the number of deallocaton actions in the given AllocActions array.
49 ///
50 /// This can be useful if clients want to pre-allocate room for deallocation
51 /// actions with the rest of their memory.
numDeallocActions(const AllocActions & AAs)52 inline size_t numDeallocActions(const AllocActions &AAs) {
53   return llvm::count_if(
54       AAs, [](const AllocActionCallPair &P) { return !!P.Dealloc; });
55 }
56 
57 using OnRunFinalizeActionsCompleteFn =
58     unique_function<void(Expected<std::vector<WrapperFunctionCall>>)>;
59 
60 /// Run finalize actions.
61 ///
62 /// If any finalize action fails then the corresponding dealloc actions will be
63 /// run in reverse order (not including the deallocation action for the failed
64 /// finalize action), and the error for the failing action will be returned.
65 ///
66 /// If all finalize actions succeed then a vector of deallocation actions will
67 /// be returned. The dealloc actions should be run by calling
68 /// runDeallocationActions. If this function succeeds then the AA argument will
69 /// be cleared before the function returns.
70 LLVM_ABI void runFinalizeActions(AllocActions &AAs,
71                                  OnRunFinalizeActionsCompleteFn OnComplete);
72 
73 using OnRunDeallocActionsComeleteFn = unique_function<void(Error)>;
74 
75 /// Run deallocation actions.
76 /// Dealloc actions will be run in reverse order (from last element of DAs to
77 /// first).
78 LLVM_ABI void runDeallocActions(ArrayRef<WrapperFunctionCall> DAs,
79                                 OnRunDeallocActionsComeleteFn OnComplete);
80 
81 using SPSAllocActionCallPair =
82     SPSTuple<SPSWrapperFunctionCall, SPSWrapperFunctionCall>;
83 
84 template <>
85 class SPSSerializationTraits<SPSAllocActionCallPair,
86                              AllocActionCallPair> {
87   using AL = SPSAllocActionCallPair::AsArgList;
88 
89 public:
size(const AllocActionCallPair & AAP)90   static size_t size(const AllocActionCallPair &AAP) {
91     return AL::size(AAP.Finalize, AAP.Dealloc);
92   }
93 
serialize(SPSOutputBuffer & OB,const AllocActionCallPair & AAP)94   static bool serialize(SPSOutputBuffer &OB,
95                         const AllocActionCallPair &AAP) {
96     return AL::serialize(OB, AAP.Finalize, AAP.Dealloc);
97   }
98 
deserialize(SPSInputBuffer & IB,AllocActionCallPair & AAP)99   static bool deserialize(SPSInputBuffer &IB,
100                           AllocActionCallPair &AAP) {
101     return AL::deserialize(IB, AAP.Finalize, AAP.Dealloc);
102   }
103 };
104 
105 } // end namespace shared
106 } // end namespace orc
107 } // end namespace llvm
108 
109 #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
110