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