LLVM  9.0.0svn
Memory.h
Go to the documentation of this file.
1 //===- llvm/Support/Memory.h - Memory Support -------------------*- 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 // This file declares the llvm::sys::Memory class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_MEMORY_H
14 #define LLVM_SUPPORT_MEMORY_H
15 
16 #include "llvm/Support/DataTypes.h"
17 #include <string>
18 #include <system_error>
19 
20 namespace llvm {
21 
22 // Forward declare raw_ostream: it is used for debug dumping below.
23 class raw_ostream;
24 
25 namespace sys {
26 
27  /// This class encapsulates the notion of a memory block which has an address
28  /// and a size. It is used by the Memory class (a friend) as the result of
29  /// various memory allocation operations.
30  /// @see Memory
31  /// Memory block abstraction.
32  class MemoryBlock {
33  public:
34  MemoryBlock() : Address(nullptr), Size(0) { }
35  MemoryBlock(void *addr, size_t size) : Address(addr), Size(size) { }
36  void *base() const { return Address; }
37  size_t size() const { return Size; }
38 
39  private:
40  void *Address; ///< Address of first byte of memory area
41  size_t Size; ///< Size, in bytes of the memory area
42  unsigned Flags = 0;
43  friend class Memory;
44  };
45 
46  /// This class provides various memory handling functions that manipulate
47  /// MemoryBlock instances.
48  /// @since 1.4
49  /// An abstraction for memory operations.
50  class Memory {
51  public:
53  MF_READ = 0x1000000,
54  MF_WRITE = 0x2000000,
55  MF_EXEC = 0x4000000,
56  MF_RWE_MASK = 0x7000000,
57  MF_HUGE_HINT = 0x0000001
58  };
59 
60  /// This method allocates a block of memory that is suitable for loading
61  /// dynamically generated code (e.g. JIT). An attempt to allocate
62  /// \p NumBytes bytes of virtual memory is made.
63  /// \p NearBlock may point to an existing allocation in which case
64  /// an attempt is made to allocate more memory near the existing block.
65  /// The actual allocated address is not guaranteed to be near the requested
66  /// address.
67  /// \p Flags is used to set the initial protection flags for the block
68  /// of the memory.
69  /// \p EC [out] returns an object describing any error that occurs.
70  ///
71  /// This method may allocate more than the number of bytes requested. The
72  /// actual number of bytes allocated is indicated in the returned
73  /// MemoryBlock.
74  ///
75  /// The start of the allocated block must be aligned with the
76  /// system allocation granularity (64K on Windows, page size on Linux).
77  /// If the address following \p NearBlock is not so aligned, it will be
78  /// rounded up to the next allocation granularity boundary.
79  ///
80  /// \r a non-null MemoryBlock if the function was successful,
81  /// otherwise a null MemoryBlock is with \p EC describing the error.
82  ///
83  /// Allocate mapped memory.
84  static MemoryBlock allocateMappedMemory(size_t NumBytes,
85  const MemoryBlock *const NearBlock,
86  unsigned Flags,
87  std::error_code &EC);
88 
89  /// This method releases a block of memory that was allocated with the
90  /// allocateMappedMemory method. It should not be used to release any
91  /// memory block allocated any other way.
92  /// \p Block describes the memory to be released.
93  ///
94  /// \r error_success if the function was successful, or an error_code
95  /// describing the failure if an error occurred.
96  ///
97  /// Release mapped memory.
98  static std::error_code releaseMappedMemory(MemoryBlock &Block);
99 
100  /// This method sets the protection flags for a block of memory to the
101  /// state specified by /p Flags. The behavior is not specified if the
102  /// memory was not allocated using the allocateMappedMemory method.
103  /// \p Block describes the memory block to be protected.
104  /// \p Flags specifies the new protection state to be assigned to the block.
105  /// \p ErrMsg [out] returns a string describing any error that occurred.
106  ///
107  /// If \p Flags is MF_WRITE, the actual behavior varies
108  /// with the operating system (i.e. MF_READ | MF_WRITE on Windows) and the
109  /// target architecture (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
110  ///
111  /// \r error_success if the function was successful, or an error_code
112  /// describing the failure if an error occurred.
113  ///
114  /// Set memory protection state.
115  static std::error_code protectMappedMemory(const MemoryBlock &Block,
116  unsigned Flags);
117 
118  /// InvalidateInstructionCache - Before the JIT can run a block of code
119  /// that has been emitted it must invalidate the instruction cache on some
120  /// platforms.
121  static void InvalidateInstructionCache(const void *Addr, size_t Len);
122  };
123 
124  /// Owning version of MemoryBlock.
126  public:
127  OwningMemoryBlock() = default;
128  explicit OwningMemoryBlock(MemoryBlock M) : M(M) {}
130  M = Other.M;
131  Other.M = MemoryBlock();
132  }
134  M = Other.M;
135  Other.M = MemoryBlock();
136  return *this;
137  }
140  }
141  void *base() const { return M.base(); }
142  size_t size() const { return M.size(); }
143  MemoryBlock getMemoryBlock() const { return M; }
144  private:
145  MemoryBlock M;
146  };
147 
148 #ifndef NDEBUG
149  /// Debugging output for Memory::ProtectionFlags.
151 
152  /// Debugging output for MemoryBlock.
154 #endif // ifndef NDEBUG
155  } // end namespace sys
156  } // end namespace llvm
157 
158 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:50
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
void * base() const
Definition: Memory.h:141
void * base() const
Definition: Memory.h:36
OwningMemoryBlock(MemoryBlock M)
Definition: Memory.h:128
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:851
MemoryBlock(void *addr, size_t size)
Definition: Memory.h:35
OwningMemoryBlock & operator=(OwningMemoryBlock &&Other)
Definition: Memory.h:133
This class encapsulates the notion of a memory block which has an address and a size.
Definition: Memory.h:32
Owning version of MemoryBlock.
Definition: Memory.h:125
OwningMemoryBlock(OwningMemoryBlock &&Other)
Definition: Memory.h:129
raw_ostream & operator<<(raw_ostream &OS, const Memory::ProtectionFlags &PF)
Debugging output for Memory::ProtectionFlags.
Definition: Memory.cpp:35
MemoryBlock getMemoryBlock() const
Definition: Memory.h:143
size_t size() const
Definition: Memory.h:142
size_t size() const
Definition: Memory.h:37
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45