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