014633c8c55071ede98ccebb59aef6e75bf9f346
[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 <string>
18
19 namespace llvm {
20 namespace sys {
21
22   /// This class encapsulates the notion of a memory block which has an address
23   /// and a size. It is used by the Memory class (a friend) as the result of
24   /// various memory allocation operations.
25   /// @see Memory
26   /// @brief Memory block abstraction.
27   class MemoryBlock {
28   public:
29     void *base() const { return Address; }
30     unsigned size() const { return Size; }
31   private:
32     void *Address;    ///< Address of first byte of memory area
33     unsigned Size;    ///< Size, in bytes of the memory area
34     friend class Memory;
35   };
36
37   /// This class provides various memory handling functions that manipulate
38   /// MemoryBlock instances.
39   /// @since 1.4
40   /// @brief An abstraction for memory operations.
41   class Memory {
42   public:
43     /// This method allocates a block of Read/Write/Execute memory that is
44     /// suitable for executing dynamically generated code (e.g. JIT). An
45     /// attempt to allocate \p NumBytes bytes of virtual memory is made.
46     /// \p NearBlock may point to an existing allocation in which case
47     /// an attempt is made to allocate more memory near the existing block.
48     ///
49     /// On success, this returns a non-null memory block, otherwise it returns
50     /// a null memory block and fills in *ErrMsg.
51     /// 
52     /// @brief Allocate Read/Write/Execute memory.
53     static MemoryBlock AllocateRWX(unsigned NumBytes,
54                                    const MemoryBlock *NearBlock,
55                                    std::string *ErrMsg = 0);
56
57     /// This method releases a block of Read/Write/Execute memory that was
58     /// allocated with the AllocateRWX method. It should not be used to
59     /// release any memory block allocated any other way.
60     ///
61     /// On success, this returns false, otherwise it returns true and fills
62     /// in *ErrMsg.
63     /// @throws std::string if an error occurred.
64     /// @brief Release Read/Write/Execute memory.
65     static bool ReleaseRWX(MemoryBlock &block, std::string *ErrMsg = 0);
66     
67     
68     /// InvalidateInstructionCache - Before the JIT can run a block of code
69     /// that has been emitted it must invalidate the instruction cache on some
70     /// platforms.
71     static void InvalidateInstructionCache(const void *Addr, size_t Len);
72   };
73 }
74 }
75
76 #endif