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 was developed by Reid Spencer and is distributed under the
00006 // University of Illinois Open Source 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 #include "llvm/System/IncludeFile.h"
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     void *base() const { return Address; }
00031     unsigned size() const { return Size; }
00032   private:
00033     void *Address;    ///< Address of first byte of memory area
00034     unsigned Size;    ///< Size, in bytes of the memory area
00035     friend class Memory;
00036   };
00037 
00038   /// This class provides various memory handling functions that manipulate
00039   /// MemoryBlock instances.
00040   /// @since 1.4
00041   /// @brief An abstraction for memory operations.
00042   class Memory {
00043     /// @name Functions
00044     /// @{
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(unsigned 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 }
00072 }
00073 
00074 FORCE_DEFINING_FILE_TO_BE_LINKED(SystemMemory)
00075 
00076 #endif