Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 1 | //===-- JITMemoryManager.cpp - Memory Allocator for JIT'd code ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the DefaultJITMemoryManager class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 81ce3ed | 2009-07-23 00:49:59 +0000 | [diff] [blame] | 14 | #include "llvm/GlobalValue.h" |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 15 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 18 | #include "llvm/System/Memory.h" |
| 19 | #include <map> |
| 20 | #include <vector> |
Chuck Rose III | 3012ac6 | 2007-12-06 02:03:01 +0000 | [diff] [blame] | 21 | #include <cassert> |
Dan Gohman | de551f9 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 22 | #include <climits> |
Duncan Sands | 4520dd2 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 24 | #include <cstdlib> |
| 25 | #include <cstring> |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | |
| 29 | JITMemoryManager::~JITMemoryManager() {} |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Memory Block Implementation. |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace { |
| 36 | /// MemoryRangeHeader - For a range of memory, this is the header that we put |
| 37 | /// on the block of memory. It is carefully crafted to be one word of memory. |
| 38 | /// Allocated blocks have just this header, free'd blocks have FreeRangeHeader |
| 39 | /// which starts with this. |
| 40 | struct FreeRangeHeader; |
| 41 | struct MemoryRangeHeader { |
| 42 | /// ThisAllocated - This is true if this block is currently allocated. If |
| 43 | /// not, this can be converted to a FreeRangeHeader. |
| 44 | unsigned ThisAllocated : 1; |
| 45 | |
| 46 | /// PrevAllocated - Keep track of whether the block immediately before us is |
| 47 | /// allocated. If not, the word immediately before this header is the size |
| 48 | /// of the previous block. |
| 49 | unsigned PrevAllocated : 1; |
| 50 | |
| 51 | /// BlockSize - This is the size in bytes of this memory block, |
| 52 | /// including this header. |
Dan Gohman | de551f9 | 2009-04-01 18:45:54 +0000 | [diff] [blame] | 53 | uintptr_t BlockSize : (sizeof(intptr_t)*CHAR_BIT - 2); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | /// getBlockAfter - Return the memory block immediately after this one. |
| 57 | /// |
| 58 | MemoryRangeHeader &getBlockAfter() const { |
| 59 | return *(MemoryRangeHeader*)((char*)this+BlockSize); |
| 60 | } |
| 61 | |
| 62 | /// getFreeBlockBefore - If the block before this one is free, return it, |
| 63 | /// otherwise return null. |
| 64 | FreeRangeHeader *getFreeBlockBefore() const { |
| 65 | if (PrevAllocated) return 0; |
| 66 | intptr_t PrevSize = ((intptr_t *)this)[-1]; |
| 67 | return (FreeRangeHeader*)((char*)this-PrevSize); |
| 68 | } |
| 69 | |
| 70 | /// FreeBlock - Turn an allocated block into a free block, adjusting |
| 71 | /// bits in the object headers, and adding an end of region memory block. |
| 72 | FreeRangeHeader *FreeBlock(FreeRangeHeader *FreeList); |
| 73 | |
| 74 | /// TrimAllocationToSize - If this allocated block is significantly larger |
| 75 | /// than NewSize, split it into two pieces (where the former is NewSize |
| 76 | /// bytes, including the header), and add the new block to the free list. |
| 77 | FreeRangeHeader *TrimAllocationToSize(FreeRangeHeader *FreeList, |
| 78 | uint64_t NewSize); |
| 79 | }; |
| 80 | |
| 81 | /// FreeRangeHeader - For a memory block that isn't already allocated, this |
| 82 | /// keeps track of the current block and has a pointer to the next free block. |
| 83 | /// Free blocks are kept on a circularly linked list. |
| 84 | struct FreeRangeHeader : public MemoryRangeHeader { |
| 85 | FreeRangeHeader *Prev; |
| 86 | FreeRangeHeader *Next; |
| 87 | |
| 88 | /// getMinBlockSize - Get the minimum size for a memory block. Blocks |
| 89 | /// smaller than this size cannot be created. |
| 90 | static unsigned getMinBlockSize() { |
| 91 | return sizeof(FreeRangeHeader)+sizeof(intptr_t); |
| 92 | } |
| 93 | |
| 94 | /// SetEndOfBlockSizeMarker - The word at the end of every free block is |
| 95 | /// known to be the size of the free block. Set it for this block. |
| 96 | void SetEndOfBlockSizeMarker() { |
| 97 | void *EndOfBlock = (char*)this + BlockSize; |
| 98 | ((intptr_t *)EndOfBlock)[-1] = BlockSize; |
| 99 | } |
| 100 | |
| 101 | FreeRangeHeader *RemoveFromFreeList() { |
| 102 | assert(Next->Prev == this && Prev->Next == this && "Freelist broken!"); |
| 103 | Next->Prev = Prev; |
| 104 | return Prev->Next = Next; |
| 105 | } |
| 106 | |
| 107 | void AddToFreeList(FreeRangeHeader *FreeList) { |
| 108 | Next = FreeList; |
| 109 | Prev = FreeList->Prev; |
| 110 | Prev->Next = this; |
| 111 | Next->Prev = this; |
| 112 | } |
| 113 | |
| 114 | /// GrowBlock - The block after this block just got deallocated. Merge it |
| 115 | /// into the current block. |
| 116 | void GrowBlock(uintptr_t NewSize); |
| 117 | |
| 118 | /// AllocateBlock - Mark this entire block allocated, updating freelists |
| 119 | /// etc. This returns a pointer to the circular free-list. |
| 120 | FreeRangeHeader *AllocateBlock(); |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /// AllocateBlock - Mark this entire block allocated, updating freelists |
| 126 | /// etc. This returns a pointer to the circular free-list. |
| 127 | FreeRangeHeader *FreeRangeHeader::AllocateBlock() { |
| 128 | assert(!ThisAllocated && !getBlockAfter().PrevAllocated && |
| 129 | "Cannot allocate an allocated block!"); |
| 130 | // Mark this block allocated. |
| 131 | ThisAllocated = 1; |
| 132 | getBlockAfter().PrevAllocated = 1; |
| 133 | |
| 134 | // Remove it from the free list. |
| 135 | return RemoveFromFreeList(); |
| 136 | } |
| 137 | |
| 138 | /// FreeBlock - Turn an allocated block into a free block, adjusting |
| 139 | /// bits in the object headers, and adding an end of region memory block. |
| 140 | /// If possible, coalesce this block with neighboring blocks. Return the |
| 141 | /// FreeRangeHeader to allocate from. |
| 142 | FreeRangeHeader *MemoryRangeHeader::FreeBlock(FreeRangeHeader *FreeList) { |
| 143 | MemoryRangeHeader *FollowingBlock = &getBlockAfter(); |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 144 | assert(ThisAllocated && "This block is already allocated!"); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 145 | assert(FollowingBlock->PrevAllocated && "Flags out of sync!"); |
| 146 | |
| 147 | FreeRangeHeader *FreeListToReturn = FreeList; |
| 148 | |
| 149 | // If the block after this one is free, merge it into this block. |
| 150 | if (!FollowingBlock->ThisAllocated) { |
| 151 | FreeRangeHeader &FollowingFreeBlock = *(FreeRangeHeader *)FollowingBlock; |
| 152 | // "FreeList" always needs to be a valid free block. If we're about to |
| 153 | // coalesce with it, update our notion of what the free list is. |
| 154 | if (&FollowingFreeBlock == FreeList) { |
| 155 | FreeList = FollowingFreeBlock.Next; |
| 156 | FreeListToReturn = 0; |
| 157 | assert(&FollowingFreeBlock != FreeList && "No tombstone block?"); |
| 158 | } |
| 159 | FollowingFreeBlock.RemoveFromFreeList(); |
| 160 | |
| 161 | // Include the following block into this one. |
| 162 | BlockSize += FollowingFreeBlock.BlockSize; |
| 163 | FollowingBlock = &FollowingFreeBlock.getBlockAfter(); |
| 164 | |
| 165 | // Tell the block after the block we are coalescing that this block is |
| 166 | // allocated. |
| 167 | FollowingBlock->PrevAllocated = 1; |
| 168 | } |
| 169 | |
| 170 | assert(FollowingBlock->ThisAllocated && "Missed coalescing?"); |
| 171 | |
| 172 | if (FreeRangeHeader *PrevFreeBlock = getFreeBlockBefore()) { |
| 173 | PrevFreeBlock->GrowBlock(PrevFreeBlock->BlockSize + BlockSize); |
| 174 | return FreeListToReturn ? FreeListToReturn : PrevFreeBlock; |
| 175 | } |
| 176 | |
| 177 | // Otherwise, mark this block free. |
| 178 | FreeRangeHeader &FreeBlock = *(FreeRangeHeader*)this; |
| 179 | FollowingBlock->PrevAllocated = 0; |
| 180 | FreeBlock.ThisAllocated = 0; |
| 181 | |
| 182 | // Link this into the linked list of free blocks. |
| 183 | FreeBlock.AddToFreeList(FreeList); |
| 184 | |
| 185 | // Add a marker at the end of the block, indicating the size of this free |
| 186 | // block. |
| 187 | FreeBlock.SetEndOfBlockSizeMarker(); |
| 188 | return FreeListToReturn ? FreeListToReturn : &FreeBlock; |
| 189 | } |
| 190 | |
| 191 | /// GrowBlock - The block after this block just got deallocated. Merge it |
| 192 | /// into the current block. |
| 193 | void FreeRangeHeader::GrowBlock(uintptr_t NewSize) { |
| 194 | assert(NewSize > BlockSize && "Not growing block?"); |
| 195 | BlockSize = NewSize; |
| 196 | SetEndOfBlockSizeMarker(); |
| 197 | getBlockAfter().PrevAllocated = 0; |
| 198 | } |
| 199 | |
| 200 | /// TrimAllocationToSize - If this allocated block is significantly larger |
| 201 | /// than NewSize, split it into two pieces (where the former is NewSize |
| 202 | /// bytes, including the header), and add the new block to the free list. |
| 203 | FreeRangeHeader *MemoryRangeHeader:: |
| 204 | TrimAllocationToSize(FreeRangeHeader *FreeList, uint64_t NewSize) { |
| 205 | assert(ThisAllocated && getBlockAfter().PrevAllocated && |
| 206 | "Cannot deallocate part of an allocated block!"); |
| 207 | |
Evan Cheng | 60b75f4 | 2008-07-29 07:38:32 +0000 | [diff] [blame] | 208 | // Don't allow blocks to be trimmed below minimum required size |
| 209 | NewSize = std::max<uint64_t>(FreeRangeHeader::getMinBlockSize(), NewSize); |
| 210 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 211 | // Round up size for alignment of header. |
| 212 | unsigned HeaderAlign = __alignof(FreeRangeHeader); |
| 213 | NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1); |
| 214 | |
| 215 | // Size is now the size of the block we will remove from the start of the |
| 216 | // current block. |
| 217 | assert(NewSize <= BlockSize && |
| 218 | "Allocating more space from this block than exists!"); |
| 219 | |
| 220 | // If splitting this block will cause the remainder to be too small, do not |
| 221 | // split the block. |
| 222 | if (BlockSize <= NewSize+FreeRangeHeader::getMinBlockSize()) |
| 223 | return FreeList; |
| 224 | |
| 225 | // Otherwise, we splice the required number of bytes out of this block, form |
| 226 | // a new block immediately after it, then mark this block allocated. |
| 227 | MemoryRangeHeader &FormerNextBlock = getBlockAfter(); |
| 228 | |
| 229 | // Change the size of this block. |
| 230 | BlockSize = NewSize; |
| 231 | |
| 232 | // Get the new block we just sliced out and turn it into a free block. |
| 233 | FreeRangeHeader &NewNextBlock = (FreeRangeHeader &)getBlockAfter(); |
| 234 | NewNextBlock.BlockSize = (char*)&FormerNextBlock - (char*)&NewNextBlock; |
| 235 | NewNextBlock.ThisAllocated = 0; |
| 236 | NewNextBlock.PrevAllocated = 1; |
| 237 | NewNextBlock.SetEndOfBlockSizeMarker(); |
| 238 | FormerNextBlock.PrevAllocated = 0; |
| 239 | NewNextBlock.AddToFreeList(FreeList); |
| 240 | return &NewNextBlock; |
| 241 | } |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
| 244 | // Memory Block Implementation. |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 247 | namespace { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 248 | /// DefaultJITMemoryManager - Manage memory for the JIT code generation. |
| 249 | /// This splits a large block of MAP_NORESERVE'd memory into two |
| 250 | /// sections, one for function stubs, one for the functions themselves. We |
| 251 | /// have to do this because we may need to emit a function stub while in the |
| 252 | /// middle of emitting a function, and we don't know how large the function we |
| 253 | /// are emitting is. |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 254 | class VISIBILITY_HIDDEN DefaultJITMemoryManager : public JITMemoryManager { |
| 255 | bool PoisonMemory; // Whether to poison freed memory. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 256 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 257 | std::vector<sys::MemoryBlock> Blocks; // Memory blocks allocated by the JIT |
| 258 | FreeRangeHeader *FreeMemoryList; // Circular list of free blocks. |
| 259 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 260 | // When emitting code into a memory block, this is the block. |
| 261 | MemoryRangeHeader *CurBlock; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 262 | |
| 263 | uint8_t *CurStubPtr, *StubBase; |
| 264 | uint8_t *CurGlobalPtr, *GlobalEnd; |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 265 | uint8_t *GOTBase; // Target Specific reserved memory |
| 266 | void *DlsymTable; // Stub external symbol information |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 267 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 268 | // Centralize memory block allocation. |
| 269 | sys::MemoryBlock getNewMemoryBlock(unsigned size); |
| 270 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 271 | std::map<const Function*, MemoryRangeHeader*> FunctionBlocks; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 272 | std::map<const Function*, MemoryRangeHeader*> TableBlocks; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 273 | public: |
| 274 | DefaultJITMemoryManager(); |
| 275 | ~DefaultJITMemoryManager(); |
| 276 | |
| 277 | void AllocateGOT(); |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 278 | void SetDlsymTable(void *); |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 279 | |
| 280 | uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, |
| 281 | unsigned Alignment); |
| 282 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 283 | /// startFunctionBody - When a function starts, allocate a block of free |
| 284 | /// executable memory, returning a pointer to it and its actual size. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 285 | uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize) { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 286 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 287 | FreeRangeHeader* candidateBlock = FreeMemoryList; |
| 288 | FreeRangeHeader* head = FreeMemoryList; |
| 289 | FreeRangeHeader* iter = head->Next; |
| 290 | |
| 291 | uintptr_t largest = candidateBlock->BlockSize; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 292 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 293 | // Search for the largest free block |
| 294 | while (iter != head) { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 295 | if (iter->BlockSize > largest) { |
| 296 | largest = iter->BlockSize; |
| 297 | candidateBlock = iter; |
| 298 | } |
| 299 | iter = iter->Next; |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 300 | } |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 301 | |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 302 | // Select this candidate block for allocation |
| 303 | CurBlock = candidateBlock; |
| 304 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 305 | // Allocate the entire memory block. |
Chris Lattner | 96c96b4 | 2009-03-09 21:34:10 +0000 | [diff] [blame] | 306 | FreeMemoryList = candidateBlock->AllocateBlock(); |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 307 | ActualSize = CurBlock->BlockSize-sizeof(MemoryRangeHeader); |
| 308 | return (uint8_t *)(CurBlock+1); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 309 | } |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 310 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 311 | /// endFunctionBody - The function F is now allocated, and takes the memory |
| 312 | /// in the range [FunctionStart,FunctionEnd). |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 313 | void endFunctionBody(const Function *F, uint8_t *FunctionStart, |
| 314 | uint8_t *FunctionEnd) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 315 | assert(FunctionEnd > FunctionStart); |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 316 | assert(FunctionStart == (uint8_t *)(CurBlock+1) && |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 317 | "Mismatched function start/end!"); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 318 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 319 | uintptr_t BlockSize = FunctionEnd - (uint8_t *)CurBlock; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 320 | FunctionBlocks[F] = CurBlock; |
| 321 | |
| 322 | // Release the memory at the end of this block that isn't needed. |
| 323 | FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize); |
| 324 | } |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 325 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 326 | /// allocateSpace - Allocate a memory block of the given size. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 327 | uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 328 | CurBlock = FreeMemoryList; |
| 329 | FreeMemoryList = FreeMemoryList->AllocateBlock(); |
| 330 | |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 331 | uint8_t *result = (uint8_t *)(CurBlock + 1); |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 332 | |
| 333 | if (Alignment == 0) Alignment = 1; |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 334 | result = (uint8_t*)(((intptr_t)result+Alignment-1) & |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 335 | ~(intptr_t)(Alignment-1)); |
| 336 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 337 | uintptr_t BlockSize = result + Size - (uint8_t *)CurBlock; |
Nuno Lopes | cef7527 | 2008-10-21 11:42:16 +0000 | [diff] [blame] | 338 | FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize); |
| 339 | |
| 340 | return result; |
| 341 | } |
| 342 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 343 | /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace, |
| 344 | /// this method does not touch the current block and can be called at any |
| 345 | /// time. |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 346 | uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 347 | uint8_t *Result = CurGlobalPtr; |
| 348 | |
| 349 | // Align the pointer. |
| 350 | if (Alignment == 0) Alignment = 1; |
| 351 | Result = (uint8_t*)(((uintptr_t)Result + Alignment-1) & |
| 352 | ~(uintptr_t)(Alignment-1)); |
| 353 | |
| 354 | // Move the current global pointer forward. |
| 355 | CurGlobalPtr += Result - CurGlobalPtr + Size; |
| 356 | |
| 357 | // Check for overflow. |
| 358 | if (CurGlobalPtr > GlobalEnd) { |
| 359 | // FIXME: Allocate more memory. |
| 360 | llvm_report_error("JIT ran out of memory for globals!"); |
| 361 | } |
| 362 | |
| 363 | return Result; |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 366 | /// startExceptionTable - Use startFunctionBody to allocate memory for the |
| 367 | /// function's exception table. |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 368 | uint8_t* startExceptionTable(const Function* F, uintptr_t &ActualSize) { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 369 | return startFunctionBody(F, ActualSize); |
| 370 | } |
| 371 | |
| 372 | /// endExceptionTable - The exception table of F is now allocated, |
| 373 | /// and takes the memory in the range [TableStart,TableEnd). |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 374 | void endExceptionTable(const Function *F, uint8_t *TableStart, |
| 375 | uint8_t *TableEnd, uint8_t* FrameRegister) { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 376 | assert(TableEnd > TableStart); |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 377 | assert(TableStart == (uint8_t *)(CurBlock+1) && |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 378 | "Mismatched table start/end!"); |
| 379 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 380 | uintptr_t BlockSize = TableEnd - (uint8_t *)CurBlock; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 381 | TableBlocks[F] = CurBlock; |
| 382 | |
| 383 | // Release the memory at the end of this block that isn't needed. |
| 384 | FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize); |
| 385 | } |
| 386 | |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 387 | uint8_t *getGOTBase() const { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 388 | return GOTBase; |
| 389 | } |
| 390 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 391 | void *getDlsymTable() const { |
| 392 | return DlsymTable; |
| 393 | } |
| 394 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 395 | /// deallocateMemForFunction - Deallocate all memory for the specified |
| 396 | /// function body. |
| 397 | void deallocateMemForFunction(const Function *F) { |
| 398 | std::map<const Function*, MemoryRangeHeader*>::iterator |
| 399 | I = FunctionBlocks.find(F); |
| 400 | if (I == FunctionBlocks.end()) return; |
| 401 | |
| 402 | // Find the block that is allocated for this function. |
| 403 | MemoryRangeHeader *MemRange = I->second; |
| 404 | assert(MemRange->ThisAllocated && "Block isn't allocated!"); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 406 | // Fill the buffer with garbage! |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 407 | if (PoisonMemory) { |
| 408 | memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange)); |
| 409 | } |
| 410 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 411 | // Free the memory. |
| 412 | FreeMemoryList = MemRange->FreeBlock(FreeMemoryList); |
| 413 | |
| 414 | // Finally, remove this entry from FunctionBlocks. |
| 415 | FunctionBlocks.erase(I); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 416 | |
| 417 | I = TableBlocks.find(F); |
| 418 | if (I == TableBlocks.end()) return; |
| 419 | |
| 420 | // Find the block that is allocated for this function. |
| 421 | MemRange = I->second; |
| 422 | assert(MemRange->ThisAllocated && "Block isn't allocated!"); |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 423 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 424 | // Fill the buffer with garbage! |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 425 | if (PoisonMemory) { |
| 426 | memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange)); |
| 427 | } |
| 428 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 429 | // Free the memory. |
| 430 | FreeMemoryList = MemRange->FreeBlock(FreeMemoryList); |
| 431 | |
| 432 | // Finally, remove this entry from TableBlocks. |
| 433 | TableBlocks.erase(I); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 434 | } |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 435 | |
| 436 | /// setMemoryWritable - When code generation is in progress, |
| 437 | /// the code pages may need permissions changed. |
| 438 | void setMemoryWritable(void) |
| 439 | { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 440 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) |
| 441 | sys::Memory::setWritable(Blocks[i]); |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 442 | } |
| 443 | /// setMemoryExecutable - When code generation is done and we're ready to |
| 444 | /// start execution, the code pages may need permissions changed. |
| 445 | void setMemoryExecutable(void) |
| 446 | { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 447 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) |
| 448 | sys::Memory::setExecutable(Blocks[i]); |
Jim Grosbach | cce6c29 | 2008-10-03 16:17:20 +0000 | [diff] [blame] | 449 | } |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 450 | |
| 451 | /// setPoisonMemory - Controls whether we write garbage over freed memory. |
| 452 | /// |
| 453 | void setPoisonMemory(bool poison) { |
| 454 | PoisonMemory = poison; |
| 455 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 456 | }; |
| 457 | } |
| 458 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 459 | DefaultJITMemoryManager::DefaultJITMemoryManager() { |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 460 | #ifdef NDEBUG |
Reid Kleckner | 81ce3ed | 2009-07-23 00:49:59 +0000 | [diff] [blame] | 461 | PoisonMemory = true; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 462 | #else |
| 463 | PoisonMemory = false; |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 464 | #endif |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 465 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 466 | // Allocate a 16M block of memory for functions. |
| 467 | #if defined(__APPLE__) && defined(__arm__) |
| 468 | sys::MemoryBlock MemBlock = getNewMemoryBlock(4 << 20); |
| 469 | #else |
| 470 | sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20); |
| 471 | #endif |
| 472 | |
| 473 | uint8_t *MemBase = static_cast<uint8_t*>(MemBlock.base()); |
| 474 | |
| 475 | // Allocate stubs backwards to the base, globals forward from the stubs, and |
| 476 | // functions forward after globals. |
| 477 | StubBase = MemBase; |
| 478 | CurStubPtr = MemBase + 512*1024; // Use 512k for stubs, working backwards. |
| 479 | CurGlobalPtr = CurStubPtr; // Use 2M for globals, working forwards. |
| 480 | GlobalEnd = CurGlobalPtr + 2*1024*1024; |
Jeffrey Yasskin | 489393d | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 482 | // We set up the memory chunk with 4 mem regions, like this: |
| 483 | // [ START |
| 484 | // [ Free #0 ] -> Large space to allocate functions from. |
| 485 | // [ Allocated #1 ] -> Tiny space to separate regions. |
| 486 | // [ Free #2 ] -> Tiny space so there is always at least 1 free block. |
| 487 | // [ Allocated #3 ] -> Tiny space to prevent looking past end of block. |
| 488 | // END ] |
| 489 | // |
| 490 | // The last three blocks are never deallocated or touched. |
| 491 | |
| 492 | // Add MemoryRangeHeader to the end of the memory region, indicating that |
| 493 | // the space after the block of memory is allocated. This is block #3. |
| 494 | MemoryRangeHeader *Mem3 = (MemoryRangeHeader*)(MemBase+MemBlock.size())-1; |
| 495 | Mem3->ThisAllocated = 1; |
| 496 | Mem3->PrevAllocated = 0; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 497 | Mem3->BlockSize = 0; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 498 | |
| 499 | /// Add a tiny free region so that the free list always has one entry. |
| 500 | FreeRangeHeader *Mem2 = |
| 501 | (FreeRangeHeader *)(((char*)Mem3)-FreeRangeHeader::getMinBlockSize()); |
| 502 | Mem2->ThisAllocated = 0; |
| 503 | Mem2->PrevAllocated = 1; |
| 504 | Mem2->BlockSize = FreeRangeHeader::getMinBlockSize(); |
| 505 | Mem2->SetEndOfBlockSizeMarker(); |
| 506 | Mem2->Prev = Mem2; // Mem2 *is* the free list for now. |
| 507 | Mem2->Next = Mem2; |
| 508 | |
| 509 | /// Add a tiny allocated region so that Mem2 is never coalesced away. |
| 510 | MemoryRangeHeader *Mem1 = (MemoryRangeHeader*)Mem2-1; |
| 511 | Mem1->ThisAllocated = 1; |
| 512 | Mem1->PrevAllocated = 0; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 513 | Mem1->BlockSize = (char*)Mem2 - (char*)Mem1; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 514 | |
| 515 | // Add a FreeRangeHeader to the start of the function body region, indicating |
| 516 | // that the space is free. Mark the previous block allocated so we never look |
| 517 | // at it. |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 518 | FreeRangeHeader *Mem0 = (FreeRangeHeader*)GlobalEnd; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 519 | Mem0->ThisAllocated = 0; |
| 520 | Mem0->PrevAllocated = 1; |
| 521 | Mem0->BlockSize = (char*)Mem1-(char*)Mem0; |
| 522 | Mem0->SetEndOfBlockSizeMarker(); |
| 523 | Mem0->AddToFreeList(Mem2); |
| 524 | |
| 525 | // Start out with the freelist pointing to Mem0. |
| 526 | FreeMemoryList = Mem0; |
| 527 | |
| 528 | GOTBase = NULL; |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 529 | DlsymTable = NULL; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void DefaultJITMemoryManager::AllocateGOT() { |
| 533 | assert(GOTBase == 0 && "Cannot allocate the got multiple times"); |
Bruno Cardoso Lopes | 186c670 | 2009-06-04 00:15:51 +0000 | [diff] [blame] | 534 | GOTBase = new uint8_t[sizeof(void*) * 8192]; |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 535 | HasGOT = true; |
| 536 | } |
| 537 | |
Nate Begeman | d6b7a24 | 2009-02-18 08:31:02 +0000 | [diff] [blame] | 538 | void DefaultJITMemoryManager::SetDlsymTable(void *ptr) { |
| 539 | DlsymTable = ptr; |
| 540 | } |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 541 | |
| 542 | DefaultJITMemoryManager::~DefaultJITMemoryManager() { |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 543 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) |
| 544 | sys::Memory::ReleaseRWX(Blocks[i]); |
| 545 | |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 546 | delete[] GOTBase; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 547 | Blocks.clear(); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 550 | uint8_t *DefaultJITMemoryManager::allocateStub(const GlobalValue* F, |
| 551 | unsigned StubSize, |
| 552 | unsigned Alignment) { |
| 553 | CurStubPtr -= StubSize; |
| 554 | CurStubPtr = (uint8_t*)(((intptr_t)CurStubPtr) & |
| 555 | ~(intptr_t)(Alignment-1)); |
| 556 | if (CurStubPtr < StubBase) { |
| 557 | // FIXME: allocate a new block |
| 558 | llvm_report_error("JIT ran out of memory for function stubs!"); |
| 559 | } |
| 560 | return CurStubPtr; |
| 561 | } |
| 562 | |
| 563 | sys::MemoryBlock DefaultJITMemoryManager::getNewMemoryBlock(unsigned size) { |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 564 | // Allocate a new block close to the last one. |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 565 | const sys::MemoryBlock *BOld = Blocks.empty() ? 0 : &Blocks.back(); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 566 | std::string ErrMsg; |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 567 | sys::MemoryBlock B = sys::Memory::AllocateRWX(size, BOld, &ErrMsg); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 568 | if (B.base() == 0) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 569 | llvm_report_error("Allocation failed when allocating new memory in the" |
| 570 | " JIT\n" + ErrMsg); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 571 | } |
Reid Kleckner | 4bf3706 | 2009-07-23 01:40:54 +0000 | [diff] [blame^] | 572 | Blocks.push_back(B); |
Chris Lattner | 8907b4b | 2007-12-05 23:39:57 +0000 | [diff] [blame] | 573 | return B; |
| 574 | } |
| 575 | |
| 576 | |
| 577 | JITMemoryManager *JITMemoryManager::CreateDefaultMemManager() { |
| 578 | return new DefaultJITMemoryManager(); |
| 579 | } |