69251dd2bfbd6ec7747421fdfa401660797a7a97
[oota-llvm.git] / include / llvm / System / Memory.h
1 //===- llvm/System/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
14 #ifndef LLVM_SYSTEM_MEMORY_H
15 #define LLVM_SYSTEM_MEMORY_H
16
17 #include "llvm/System/DataTypes.h"
18 #include <string>
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   /// @brief Memory block abstraction.
28   class MemoryBlock {
29   public:
30     MemoryBlock() { }
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   private:
35     void *Address;    ///< Address of first byte of memory area
36     size_t Size;      ///< Size, in bytes of the memory area
37     friend class Memory;
38   };
39
40   /// This class provides various memory handling functions that manipulate
41   /// MemoryBlock instances.
42   /// @since 1.4
43   /// @brief An abstraction for memory operations.
44   class Memory {
45   public:
46     /// This method allocates a block of Read/Write/Execute memory that is
47     /// suitable for executing dynamically generated code (e.g. JIT). An
48     /// attempt to allocate \p NumBytes bytes of virtual memory is made.
49     /// \p NearBlock may point to an existing allocation in which case
50     /// an attempt is made to allocate more memory near the existing block.
51     ///
52     /// On success, this returns a non-null memory block, otherwise it returns
53     /// a null memory block and fills in *ErrMsg.
54     /// 
55     /// @brief Allocate Read/Write/Execute memory.
56     static MemoryBlock AllocateRWX(size_t NumBytes,
57                                    const MemoryBlock *NearBlock,
58                                    std::string *ErrMsg = 0);
59
60     /// This method releases a block of Read/Write/Execute memory that was
61     /// allocated with the AllocateRWX method. It should not be used to
62     /// release any memory block allocated any other way.
63     ///
64     /// On success, this returns false, otherwise it returns true and fills
65     /// in *ErrMsg.
66     /// @throws std::string if an error occurred.
67     /// @brief Release Read/Write/Execute memory.
68     static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = 0);
69     
70     
71     /// InvalidateInstructionCache - Before the JIT can run a block of code
72     /// that has been emitted it must invalidate the instruction cache on some
73     /// platforms.
74     static void InvalidateInstructionCache(const void *Addr, size_t Len);
75
76     /// setExecutable - Before the JIT can run a block of code, it has to be
77     /// given read and executable privilege. Return true if it is already r-x
78     /// or the system is able to change its previlege.
79     static bool setExecutable (MemoryBlock &M, std::string *ErrMsg = 0);
80
81     /// setWritable - When adding to a block of code, the JIT may need
82     /// to mark a block of code as RW since the protections are on page
83     /// boundaries, and the JIT internal allocations are not page aligned.
84     static bool setWritable (MemoryBlock &M, std::string *ErrMsg = 0);
85
86     /// setRangeExecutable - Mark the page containing a range of addresses 
87     /// as executable.
88     static bool setRangeExecutable(const void *Addr, size_t Size);
89
90     /// setRangeWritable - Mark the page containing a range of addresses 
91     /// as writable.
92     static bool setRangeWritable(const void *Addr, size_t Size);
93   };
94 }
95 }
96
97 #endif