LLVM API Documentation

Memory.h

Go to the documentation of this file.
00001 //===- llvm/System/Memory.h - Memory Support --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the llvm::sys::Memory class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SYSTEM_MEMORY_H
00015 #define LLVM_SYSTEM_MEMORY_H
00016 
00017 #include <string>
00018 
00019 namespace llvm {
00020 namespace sys {
00021 
00022   /// This class encapsulates the notion of a memory block which has an address
00023   /// and a size. It is used by the Memory class (a friend) as the result of
00024   /// various memory allocation operations.
00025   /// @see Memory
00026   /// @brief Memory block abstraction.
00027   class MemoryBlock {
00028   public:
00029     void *base() const { return Address; }
00030     unsigned size() const { return Size; }
00031   private:
00032     void *Address;    ///< Address of first byte of memory area
00033     unsigned Size;    ///< Size, in bytes of the memory area
00034     friend class Memory;
00035   };
00036 
00037   /// This class provides various memory handling functions that manipulate
00038   /// MemoryBlock instances.
00039   /// @since 1.4
00040   /// @brief An abstraction for memory operations.
00041   class Memory {
00042   public:
00043     /// This method allocates a block of Read/Write/Execute memory that is
00044     /// suitable for executing dynamically generated code (e.g. JIT). An
00045     /// attempt to allocate \p NumBytes bytes of virtual memory is made.
00046     /// \p NearBlock may point to an existing allocation in which case
00047     /// an attempt is made to allocate more memory near the existing block.
00048     ///
00049     /// On success, this returns a non-null memory block, otherwise it returns
00050     /// a null memory block and fills in *ErrMsg.
00051     /// 
00052     /// @brief Allocate Read/Write/Execute memory.
00053     static MemoryBlock AllocateRWX(unsigned NumBytes,
00054                                    const MemoryBlock *NearBlock,
00055                                    std::string *ErrMsg = 0);
00056 
00057     /// This method releases a block of Read/Write/Execute memory that was
00058     /// allocated with the AllocateRWX method. It should not be used to
00059     /// release any memory block allocated any other way.
00060     ///
00061     /// On success, this returns false, otherwise it returns true and fills
00062     /// in *ErrMsg.
00063     /// @throws std::string if an error occurred.
00064     /// @brief Release Read/Write/Execute memory.
00065     static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = 0);
00066     
00067     
00068     /// InvalidateInstructionCache - Before the JIT can run a block of code
00069     /// that has been emitted it must invalidate the instruction cache on some
00070     /// platforms.
00071     static void InvalidateInstructionCache(const void *Addr, size_t Len);
00072 
00073     /// setExecutable - Before the JIT can run a block of code, it has to be
00074     /// given read and executable privilege. Return true if it is already r-x
00075     /// or the system is able to change its previlege.
00076     static bool setExecutable (MemoryBlock &M, std::string *ErrMsg = 0);
00077 
00078     /// setWritable - When adding to a block of code, the JIT may need
00079     /// to mark a block of code as RW since the protections are on page
00080     /// boundaries, and the JIT internal allocations are not page aligned.
00081     static bool setWritable (MemoryBlock &M, std::string *ErrMsg = 0);
00082 
00083     /// setRangeExecutable - Mark the page containing a range of addresses 
00084     /// as executable.
00085     static bool setRangeExecutable(const void *Addr, size_t Size);
00086 
00087     /// setRangeWritable - Mark the page containing a range of addresses 
00088     /// as writable.
00089     static bool setRangeWritable(const void *Addr, size_t Size);
00090   };
00091 }
00092 }
00093 
00094 #endif