AllocationActions.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- AllocationActions.h -- JITLink allocation support calls -*- 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. // Structures for making memory allocation support calls.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
  18. #define LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
  19. #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
  20. #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
  21. #include "llvm/Support/Memory.h"
  22. #include <vector>
  23. namespace llvm {
  24. namespace orc {
  25. namespace shared {
  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. /// A vector of allocation actions to be run for this allocation.
  40. ///
  41. /// Finalize allocations will be run in order at finalize time. Dealloc
  42. /// actions will be run in reverse order at deallocation time.
  43. using AllocActions = std::vector<AllocActionCallPair>;
  44. /// Returns the number of deallocaton actions in the given AllocActions array.
  45. ///
  46. /// This can be useful if clients want to pre-allocate room for deallocation
  47. /// actions with the rest of their memory.
  48. inline size_t numDeallocActions(const AllocActions &AAs) {
  49. return llvm::count_if(
  50. AAs, [](const AllocActionCallPair &P) { return !!P.Dealloc; });
  51. }
  52. /// Run finalize actions.
  53. ///
  54. /// If any finalize action fails then the corresponding dealloc actions will be
  55. /// run in reverse order (not including the deallocation action for the failed
  56. /// finalize action), and the error for the failing action will be returned.
  57. ///
  58. /// If all finalize actions succeed then a vector of deallocation actions will
  59. /// be returned. The dealloc actions should be run by calling
  60. /// runDeallocationActions. If this function succeeds then the AA argument will
  61. /// be cleared before the function returns.
  62. Expected<std::vector<WrapperFunctionCall>>
  63. runFinalizeActions(AllocActions &AAs);
  64. /// Run deallocation actions.
  65. /// Dealloc actions will be run in reverse order (from last element of DAs to
  66. /// first).
  67. Error runDeallocActions(ArrayRef<WrapperFunctionCall> DAs);
  68. using SPSAllocActionCallPair =
  69. SPSTuple<SPSWrapperFunctionCall, SPSWrapperFunctionCall>;
  70. template <>
  71. class SPSSerializationTraits<SPSAllocActionCallPair,
  72. AllocActionCallPair> {
  73. using AL = SPSAllocActionCallPair::AsArgList;
  74. public:
  75. static size_t size(const AllocActionCallPair &AAP) {
  76. return AL::size(AAP.Finalize, AAP.Dealloc);
  77. }
  78. static bool serialize(SPSOutputBuffer &OB,
  79. const AllocActionCallPair &AAP) {
  80. return AL::serialize(OB, AAP.Finalize, AAP.Dealloc);
  81. }
  82. static bool deserialize(SPSInputBuffer &IB,
  83. AllocActionCallPair &AAP) {
  84. return AL::deserialize(IB, AAP.Finalize, AAP.Dealloc);
  85. }
  86. };
  87. } // end namespace shared
  88. } // end namespace orc
  89. } // end namespace llvm
  90. #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_ALLOCATIONACTIONS_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif