Memory.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===- llvm/Support/Memory.h - Memory Support --------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the llvm::sys::Memory class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_MEMORY_H
  14. #define LLVM_SUPPORT_MEMORY_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include <string>
  17. #include <system_error>
  18. namespace llvm {
  19. namespace sys {
  20. /// This class encapsulates the notion of a memory block which has an address
  21. /// and a size. It is used by the Memory class (a friend) as the result of
  22. /// various memory allocation operations.
  23. /// @see Memory
  24. /// @brief Memory block abstraction.
  25. class MemoryBlock {
  26. public:
  27. MemoryBlock() : Address(nullptr), Size(0) { }
  28. MemoryBlock(void *addr, size_t size) : Address(addr), Size(size) { }
  29. void *base() const { return Address; }
  30. size_t size() const { return Size; }
  31. private:
  32. void *Address; ///< Address of first byte of memory area
  33. size_t Size; ///< Size, in bytes of the memory area
  34. friend class Memory;
  35. };
  36. /// This class provides various memory handling functions that manipulate
  37. /// MemoryBlock instances.
  38. /// @since 1.4
  39. /// @brief An abstraction for memory operations.
  40. class Memory {
  41. public:
  42. enum ProtectionFlags {
  43. MF_READ = 0x1000000,
  44. MF_WRITE = 0x2000000,
  45. MF_EXEC = 0x4000000
  46. };
  47. /// This method allocates a block of memory that is suitable for loading
  48. /// dynamically generated code (e.g. JIT). An attempt to allocate
  49. /// \p NumBytes bytes of virtual memory is made.
  50. /// \p NearBlock may point to an existing allocation in which case
  51. /// an attempt is made to allocate more memory near the existing block.
  52. /// The actual allocated address is not guaranteed to be near the requested
  53. /// address.
  54. /// \p Flags is used to set the initial protection flags for the block
  55. /// of the memory.
  56. /// \p EC [out] returns an object describing any error that occurs.
  57. ///
  58. /// This method may allocate more than the number of bytes requested. The
  59. /// actual number of bytes allocated is indicated in the returned
  60. /// MemoryBlock.
  61. ///
  62. /// The start of the allocated block must be aligned with the
  63. /// system allocation granularity (64K on Windows, page size on Linux).
  64. /// If the address following \p NearBlock is not so aligned, it will be
  65. /// rounded up to the next allocation granularity boundary.
  66. ///
  67. /// \r a non-null MemoryBlock if the function was successful,
  68. /// otherwise a null MemoryBlock is with \p EC describing the error.
  69. ///
  70. /// @brief Allocate mapped memory.
  71. static MemoryBlock allocateMappedMemory(size_t NumBytes,
  72. const MemoryBlock *const NearBlock,
  73. unsigned Flags,
  74. std::error_code &EC);
  75. /// This method releases a block of memory that was allocated with the
  76. /// allocateMappedMemory method. It should not be used to release any
  77. /// memory block allocated any other way.
  78. /// \p Block describes the memory to be released.
  79. ///
  80. /// \r error_success if the function was successful, or an error_code
  81. /// describing the failure if an error occurred.
  82. ///
  83. /// @brief Release mapped memory.
  84. static std::error_code releaseMappedMemory(MemoryBlock &Block);
  85. /// This method sets the protection flags for a block of memory to the
  86. /// state specified by /p Flags. The behavior is not specified if the
  87. /// memory was not allocated using the allocateMappedMemory method.
  88. /// \p Block describes the memory block to be protected.
  89. /// \p Flags specifies the new protection state to be assigned to the block.
  90. /// \p ErrMsg [out] returns a string describing any error that occurred.
  91. ///
  92. /// If \p Flags is MF_WRITE, the actual behavior varies
  93. /// with the operating system (i.e. MF_READ | MF_WRITE on Windows) and the
  94. /// target architecture (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
  95. ///
  96. /// \r error_success if the function was successful, or an error_code
  97. /// describing the failure if an error occurred.
  98. ///
  99. /// @brief Set memory protection state.
  100. static std::error_code protectMappedMemory(const MemoryBlock &Block,
  101. unsigned Flags);
  102. /// This method allocates a block of Read/Write/Execute memory that is
  103. /// suitable for executing dynamically generated code (e.g. JIT). An
  104. /// attempt to allocate \p NumBytes bytes of virtual memory is made.
  105. /// \p NearBlock may point to an existing allocation in which case
  106. /// an attempt is made to allocate more memory near the existing block.
  107. ///
  108. /// On success, this returns a non-null memory block, otherwise it returns
  109. /// a null memory block and fills in *ErrMsg.
  110. ///
  111. /// @brief Allocate Read/Write/Execute memory.
  112. static MemoryBlock AllocateRWX(size_t NumBytes,
  113. const MemoryBlock *NearBlock,
  114. std::string *ErrMsg = nullptr);
  115. /// This method releases a block of Read/Write/Execute memory that was
  116. /// allocated with the AllocateRWX method. It should not be used to
  117. /// release any memory block allocated any other way.
  118. ///
  119. /// On success, this returns false, otherwise it returns true and fills
  120. /// in *ErrMsg.
  121. /// @brief Release Read/Write/Execute memory.
  122. static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = nullptr);
  123. /// InvalidateInstructionCache - Before the JIT can run a block of code
  124. /// that has been emitted it must invalidate the instruction cache on some
  125. /// platforms.
  126. static void InvalidateInstructionCache(const void *Addr, size_t Len);
  127. /// setExecutable - Before the JIT can run a block of code, it has to be
  128. /// given read and executable privilege. Return true if it is already r-x
  129. /// or the system is able to change its previlege.
  130. static bool setExecutable(MemoryBlock &M, std::string *ErrMsg = nullptr);
  131. /// setWritable - When adding to a block of code, the JIT may need
  132. /// to mark a block of code as RW since the protections are on page
  133. /// boundaries, and the JIT internal allocations are not page aligned.
  134. static bool setWritable(MemoryBlock &M, std::string *ErrMsg = nullptr);
  135. /// setRangeExecutable - Mark the page containing a range of addresses
  136. /// as executable.
  137. static bool setRangeExecutable(const void *Addr, size_t Size);
  138. /// setRangeWritable - Mark the page containing a range of addresses
  139. /// as writable.
  140. static bool setRangeWritable(const void *Addr, size_t Size);
  141. };
  142. }
  143. }
  144. #endif