[C++] Use 'nullptr'.
[oota-llvm.git] / include / llvm / ExecutionEngine / SectionMemoryManager.h
1 //===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- 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 contains the declaration of a section-based memory manager used by
11 // the MCJIT execution engine and RuntimeDyld.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
16 #define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
17
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ExecutionEngine/RuntimeDyld.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Memory.h"
22
23 namespace llvm {
24
25 /// This is a simple memory manager which implements the methods called by
26 /// the RuntimeDyld class to allocate memory for section-based loading of
27 /// objects, usually those generated by the MCJIT execution engine.
28 ///
29 /// This memory manager allocates all section memory as read-write.  The
30 /// RuntimeDyld will copy JITed section memory into these allocated blocks
31 /// and perform any necessary linking and relocations.
32 ///
33 /// Any client using this memory manager MUST ensure that section-specific
34 /// page permissions have been applied before attempting to execute functions
35 /// in the JITed object.  Permissions can be applied either by calling
36 /// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
37 /// directly.  Clients of MCJIT should call MCJIT::finalizeObject.
38 class SectionMemoryManager : public RTDyldMemoryManager {
39   SectionMemoryManager(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
40   void operator=(const SectionMemoryManager&) LLVM_DELETED_FUNCTION;
41
42 public:
43   SectionMemoryManager() { }
44   virtual ~SectionMemoryManager();
45
46   /// \brief Allocates a memory block of (at least) the given size suitable for
47   /// executable code.
48   ///
49   /// The value of \p Alignment must be a power of two.  If \p Alignment is zero
50   /// a default alignment of 16 will be used.
51   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
52                                unsigned SectionID,
53                                StringRef SectionName) override;
54
55   /// \brief Allocates a memory block of (at least) the given size suitable for
56   /// executable code.
57   ///
58   /// The value of \p Alignment must be a power of two.  If \p Alignment is zero
59   /// a default alignment of 16 will be used.
60   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
61                                unsigned SectionID, StringRef SectionName,
62                                bool isReadOnly) override;
63
64   /// \brief Update section-specific memory permissions and other attributes.
65   ///
66   /// This method is called when object loading is complete and section page
67   /// permissions can be applied.  It is up to the memory manager implementation
68   /// to decide whether or not to act on this method.  The memory manager will
69   /// typically allocate all sections as read-write and then apply specific
70   /// permissions when this method is called.  Code sections cannot be executed
71   /// until this function has been called.  In addition, any cache coherency
72   /// operations needed to reliably use the memory are also performed.
73   ///
74   /// \returns true if an error occurred, false otherwise.
75   bool finalizeMemory(std::string *ErrMsg = nullptr) override;
76
77   /// \brief Invalidate instruction cache for code sections.
78   ///
79   /// Some platforms with separate data cache and instruction cache require
80   /// explicit cache flush, otherwise JIT code manipulations (like resolved
81   /// relocations) will get to the data cache but not to the instruction cache.
82   ///
83   /// This method is called from finalizeMemory.
84   virtual void invalidateInstructionCache();
85
86 private:
87   struct MemoryGroup {
88       SmallVector<sys::MemoryBlock, 16> AllocatedMem;
89       SmallVector<sys::MemoryBlock, 16> FreeMem;
90       sys::MemoryBlock Near;
91   };
92
93   uint8_t *allocateSection(MemoryGroup &MemGroup, uintptr_t Size,
94                            unsigned Alignment);
95
96   error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
97                                          unsigned Permissions);
98
99   MemoryGroup CodeMem;
100   MemoryGroup RWDataMem;
101   MemoryGroup RODataMem;
102 };
103
104 }
105
106 #endif // LLVM_EXECUTION_ENGINE_SECTION_MEMORY_MANAGER_H
107