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