1 //===-- JITMemoryManager.cpp - Memory Allocator for JIT'd code ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DefaultJITMemoryManager class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ExecutionEngine/JITMemoryManager.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/System/Memory.h"
22 JITMemoryManager::~JITMemoryManager() {}
24 //===----------------------------------------------------------------------===//
25 // Memory Block Implementation.
26 //===----------------------------------------------------------------------===//
29 /// MemoryRangeHeader - For a range of memory, this is the header that we put
30 /// on the block of memory. It is carefully crafted to be one word of memory.
31 /// Allocated blocks have just this header, free'd blocks have FreeRangeHeader
32 /// which starts with this.
33 struct FreeRangeHeader;
34 struct MemoryRangeHeader {
35 /// ThisAllocated - This is true if this block is currently allocated. If
36 /// not, this can be converted to a FreeRangeHeader.
37 unsigned ThisAllocated : 1;
39 /// PrevAllocated - Keep track of whether the block immediately before us is
40 /// allocated. If not, the word immediately before this header is the size
41 /// of the previous block.
42 unsigned PrevAllocated : 1;
44 /// BlockSize - This is the size in bytes of this memory block,
45 /// including this header.
46 uintptr_t BlockSize : (sizeof(intptr_t)*8 - 2);
49 /// getBlockAfter - Return the memory block immediately after this one.
51 MemoryRangeHeader &getBlockAfter() const {
52 return *(MemoryRangeHeader*)((char*)this+BlockSize);
55 /// getFreeBlockBefore - If the block before this one is free, return it,
56 /// otherwise return null.
57 FreeRangeHeader *getFreeBlockBefore() const {
58 if (PrevAllocated) return 0;
59 intptr_t PrevSize = ((intptr_t *)this)[-1];
60 return (FreeRangeHeader*)((char*)this-PrevSize);
63 /// FreeBlock - Turn an allocated block into a free block, adjusting
64 /// bits in the object headers, and adding an end of region memory block.
65 FreeRangeHeader *FreeBlock(FreeRangeHeader *FreeList);
67 /// TrimAllocationToSize - If this allocated block is significantly larger
68 /// than NewSize, split it into two pieces (where the former is NewSize
69 /// bytes, including the header), and add the new block to the free list.
70 FreeRangeHeader *TrimAllocationToSize(FreeRangeHeader *FreeList,
74 /// FreeRangeHeader - For a memory block that isn't already allocated, this
75 /// keeps track of the current block and has a pointer to the next free block.
76 /// Free blocks are kept on a circularly linked list.
77 struct FreeRangeHeader : public MemoryRangeHeader {
78 FreeRangeHeader *Prev;
79 FreeRangeHeader *Next;
81 /// getMinBlockSize - Get the minimum size for a memory block. Blocks
82 /// smaller than this size cannot be created.
83 static unsigned getMinBlockSize() {
84 return sizeof(FreeRangeHeader)+sizeof(intptr_t);
87 /// SetEndOfBlockSizeMarker - The word at the end of every free block is
88 /// known to be the size of the free block. Set it for this block.
89 void SetEndOfBlockSizeMarker() {
90 void *EndOfBlock = (char*)this + BlockSize;
91 ((intptr_t *)EndOfBlock)[-1] = BlockSize;
94 FreeRangeHeader *RemoveFromFreeList() {
95 assert(Next->Prev == this && Prev->Next == this && "Freelist broken!");
97 return Prev->Next = Next;
100 void AddToFreeList(FreeRangeHeader *FreeList) {
102 Prev = FreeList->Prev;
107 /// GrowBlock - The block after this block just got deallocated. Merge it
108 /// into the current block.
109 void GrowBlock(uintptr_t NewSize);
111 /// AllocateBlock - Mark this entire block allocated, updating freelists
112 /// etc. This returns a pointer to the circular free-list.
113 FreeRangeHeader *AllocateBlock();
118 /// AllocateBlock - Mark this entire block allocated, updating freelists
119 /// etc. This returns a pointer to the circular free-list.
120 FreeRangeHeader *FreeRangeHeader::AllocateBlock() {
121 assert(!ThisAllocated && !getBlockAfter().PrevAllocated &&
122 "Cannot allocate an allocated block!");
123 // Mark this block allocated.
125 getBlockAfter().PrevAllocated = 1;
127 // Remove it from the free list.
128 return RemoveFromFreeList();
131 /// FreeBlock - Turn an allocated block into a free block, adjusting
132 /// bits in the object headers, and adding an end of region memory block.
133 /// If possible, coalesce this block with neighboring blocks. Return the
134 /// FreeRangeHeader to allocate from.
135 FreeRangeHeader *MemoryRangeHeader::FreeBlock(FreeRangeHeader *FreeList) {
136 MemoryRangeHeader *FollowingBlock = &getBlockAfter();
137 assert(ThisAllocated && "This block is already allocated!");
138 assert(FollowingBlock->PrevAllocated && "Flags out of sync!");
140 FreeRangeHeader *FreeListToReturn = FreeList;
142 // If the block after this one is free, merge it into this block.
143 if (!FollowingBlock->ThisAllocated) {
144 FreeRangeHeader &FollowingFreeBlock = *(FreeRangeHeader *)FollowingBlock;
145 // "FreeList" always needs to be a valid free block. If we're about to
146 // coalesce with it, update our notion of what the free list is.
147 if (&FollowingFreeBlock == FreeList) {
148 FreeList = FollowingFreeBlock.Next;
149 FreeListToReturn = 0;
150 assert(&FollowingFreeBlock != FreeList && "No tombstone block?");
152 FollowingFreeBlock.RemoveFromFreeList();
154 // Include the following block into this one.
155 BlockSize += FollowingFreeBlock.BlockSize;
156 FollowingBlock = &FollowingFreeBlock.getBlockAfter();
158 // Tell the block after the block we are coalescing that this block is
160 FollowingBlock->PrevAllocated = 1;
163 assert(FollowingBlock->ThisAllocated && "Missed coalescing?");
165 if (FreeRangeHeader *PrevFreeBlock = getFreeBlockBefore()) {
166 PrevFreeBlock->GrowBlock(PrevFreeBlock->BlockSize + BlockSize);
167 return FreeListToReturn ? FreeListToReturn : PrevFreeBlock;
170 // Otherwise, mark this block free.
171 FreeRangeHeader &FreeBlock = *(FreeRangeHeader*)this;
172 FollowingBlock->PrevAllocated = 0;
173 FreeBlock.ThisAllocated = 0;
175 // Link this into the linked list of free blocks.
176 FreeBlock.AddToFreeList(FreeList);
178 // Add a marker at the end of the block, indicating the size of this free
180 FreeBlock.SetEndOfBlockSizeMarker();
181 return FreeListToReturn ? FreeListToReturn : &FreeBlock;
184 /// GrowBlock - The block after this block just got deallocated. Merge it
185 /// into the current block.
186 void FreeRangeHeader::GrowBlock(uintptr_t NewSize) {
187 assert(NewSize > BlockSize && "Not growing block?");
189 SetEndOfBlockSizeMarker();
190 getBlockAfter().PrevAllocated = 0;
193 /// TrimAllocationToSize - If this allocated block is significantly larger
194 /// than NewSize, split it into two pieces (where the former is NewSize
195 /// bytes, including the header), and add the new block to the free list.
196 FreeRangeHeader *MemoryRangeHeader::
197 TrimAllocationToSize(FreeRangeHeader *FreeList, uint64_t NewSize) {
198 assert(ThisAllocated && getBlockAfter().PrevAllocated &&
199 "Cannot deallocate part of an allocated block!");
201 // Round up size for alignment of header.
202 unsigned HeaderAlign = __alignof(FreeRangeHeader);
203 NewSize = (NewSize+ (HeaderAlign-1)) & ~(HeaderAlign-1);
205 // Size is now the size of the block we will remove from the start of the
207 assert(NewSize <= BlockSize &&
208 "Allocating more space from this block than exists!");
210 // If splitting this block will cause the remainder to be too small, do not
212 if (BlockSize <= NewSize+FreeRangeHeader::getMinBlockSize())
215 // Otherwise, we splice the required number of bytes out of this block, form
216 // a new block immediately after it, then mark this block allocated.
217 MemoryRangeHeader &FormerNextBlock = getBlockAfter();
219 // Change the size of this block.
222 // Get the new block we just sliced out and turn it into a free block.
223 FreeRangeHeader &NewNextBlock = (FreeRangeHeader &)getBlockAfter();
224 NewNextBlock.BlockSize = (char*)&FormerNextBlock - (char*)&NewNextBlock;
225 NewNextBlock.ThisAllocated = 0;
226 NewNextBlock.PrevAllocated = 1;
227 NewNextBlock.SetEndOfBlockSizeMarker();
228 FormerNextBlock.PrevAllocated = 0;
229 NewNextBlock.AddToFreeList(FreeList);
230 return &NewNextBlock;
233 //===----------------------------------------------------------------------===//
234 // Memory Block Implementation.
235 //===----------------------------------------------------------------------===//
238 /// DefaultJITMemoryManager - Manage memory for the JIT code generation.
239 /// This splits a large block of MAP_NORESERVE'd memory into two
240 /// sections, one for function stubs, one for the functions themselves. We
241 /// have to do this because we may need to emit a function stub while in the
242 /// middle of emitting a function, and we don't know how large the function we
244 class VISIBILITY_HIDDEN DefaultJITMemoryManager : public JITMemoryManager {
245 std::vector<sys::MemoryBlock> Blocks; // Memory blocks allocated by the JIT
246 FreeRangeHeader *FreeMemoryList; // Circular list of free blocks.
248 // When emitting code into a memory block, this is the block.
249 MemoryRangeHeader *CurBlock;
251 unsigned char *CurStubPtr, *StubBase;
252 unsigned char *GOTBase; // Target Specific reserved memory
254 // Centralize memory block allocation.
255 sys::MemoryBlock getNewMemoryBlock(unsigned size);
257 std::map<const Function*, MemoryRangeHeader*> FunctionBlocks;
259 DefaultJITMemoryManager();
260 ~DefaultJITMemoryManager();
264 unsigned char *allocateStub(unsigned StubSize, unsigned Alignment);
266 /// startFunctionBody - When a function starts, allocate a block of free
267 /// executable memory, returning a pointer to it and its actual size.
268 unsigned char *startFunctionBody(const Function *F, uintptr_t &ActualSize) {
269 CurBlock = FreeMemoryList;
271 // Allocate the entire memory block.
272 FreeMemoryList = FreeMemoryList->AllocateBlock();
273 ActualSize = CurBlock->BlockSize-sizeof(MemoryRangeHeader);
274 return (unsigned char *)(CurBlock+1);
277 /// endFunctionBody - The function F is now allocated, and takes the memory
278 /// in the range [FunctionStart,FunctionEnd).
279 void endFunctionBody(const Function *F, unsigned char *FunctionStart,
280 unsigned char *FunctionEnd) {
281 assert(FunctionEnd > FunctionStart);
282 assert(FunctionStart == (unsigned char *)(CurBlock+1) &&
283 "Mismatched function start/end!");
285 uintptr_t BlockSize = FunctionEnd - (unsigned char *)CurBlock;
286 FunctionBlocks[F] = CurBlock;
288 // Release the memory at the end of this block that isn't needed.
289 FreeMemoryList =CurBlock->TrimAllocationToSize(FreeMemoryList, BlockSize);
292 unsigned char *getGOTBase() const {
296 /// deallocateMemForFunction - Deallocate all memory for the specified
298 void deallocateMemForFunction(const Function *F) {
299 std::map<const Function*, MemoryRangeHeader*>::iterator
300 I = FunctionBlocks.find(F);
301 if (I == FunctionBlocks.end()) return;
303 // Find the block that is allocated for this function.
304 MemoryRangeHeader *MemRange = I->second;
305 assert(MemRange->ThisAllocated && "Block isn't allocated!");
307 // Fill the buffer with garbage!
309 memset(MemRange+1, 0xCD, MemRange->BlockSize-sizeof(*MemRange));
313 FreeMemoryList = MemRange->FreeBlock(FreeMemoryList);
315 // Finally, remove this entry from FunctionBlocks.
316 FunctionBlocks.erase(I);
321 DefaultJITMemoryManager::DefaultJITMemoryManager() {
322 // Allocate a 16M block of memory for functions.
323 sys::MemoryBlock MemBlock = getNewMemoryBlock(16 << 20);
325 unsigned char *MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
327 // Allocate stubs backwards from the base, allocate functions forward
330 CurStubPtr = MemBase + 512*1024; // Use 512k for stubs, working backwards.
332 // We set up the memory chunk with 4 mem regions, like this:
334 // [ Free #0 ] -> Large space to allocate functions from.
335 // [ Allocated #1 ] -> Tiny space to separate regions.
336 // [ Free #2 ] -> Tiny space so there is always at least 1 free block.
337 // [ Allocated #3 ] -> Tiny space to prevent looking past end of block.
340 // The last three blocks are never deallocated or touched.
342 // Add MemoryRangeHeader to the end of the memory region, indicating that
343 // the space after the block of memory is allocated. This is block #3.
344 MemoryRangeHeader *Mem3 = (MemoryRangeHeader*)(MemBase+MemBlock.size())-1;
345 Mem3->ThisAllocated = 1;
346 Mem3->PrevAllocated = 0;
349 /// Add a tiny free region so that the free list always has one entry.
350 FreeRangeHeader *Mem2 =
351 (FreeRangeHeader *)(((char*)Mem3)-FreeRangeHeader::getMinBlockSize());
352 Mem2->ThisAllocated = 0;
353 Mem2->PrevAllocated = 1;
354 Mem2->BlockSize = FreeRangeHeader::getMinBlockSize();
355 Mem2->SetEndOfBlockSizeMarker();
356 Mem2->Prev = Mem2; // Mem2 *is* the free list for now.
359 /// Add a tiny allocated region so that Mem2 is never coalesced away.
360 MemoryRangeHeader *Mem1 = (MemoryRangeHeader*)Mem2-1;
361 Mem1->ThisAllocated = 1;
362 Mem1->PrevAllocated = 0;
363 Mem1->BlockSize = (char*)Mem2 - (char*)Mem1;
365 // Add a FreeRangeHeader to the start of the function body region, indicating
366 // that the space is free. Mark the previous block allocated so we never look
368 FreeRangeHeader *Mem0 = (FreeRangeHeader*)CurStubPtr;
369 Mem0->ThisAllocated = 0;
370 Mem0->PrevAllocated = 1;
371 Mem0->BlockSize = (char*)Mem1-(char*)Mem0;
372 Mem0->SetEndOfBlockSizeMarker();
373 Mem0->AddToFreeList(Mem2);
375 // Start out with the freelist pointing to Mem0.
376 FreeMemoryList = Mem0;
381 void DefaultJITMemoryManager::AllocateGOT() {
382 assert(GOTBase == 0 && "Cannot allocate the got multiple times");
383 GOTBase = new unsigned char[sizeof(void*) * 8192];
388 DefaultJITMemoryManager::~DefaultJITMemoryManager() {
389 for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
390 sys::Memory::ReleaseRWX(Blocks[i]);
396 unsigned char *DefaultJITMemoryManager::allocateStub(unsigned StubSize,
397 unsigned Alignment) {
398 CurStubPtr -= StubSize;
399 CurStubPtr = (unsigned char*)(((intptr_t)CurStubPtr) &
400 ~(intptr_t)(Alignment-1));
401 if (CurStubPtr < StubBase) {
402 // FIXME: allocate a new block
403 fprintf(stderr, "JIT ran out of memory for function stubs!\n");
409 sys::MemoryBlock DefaultJITMemoryManager::getNewMemoryBlock(unsigned size) {
410 // Allocate a new block close to the last one.
411 const sys::MemoryBlock *BOld = Blocks.empty() ? 0 : &Blocks.front();
413 sys::MemoryBlock B = sys::Memory::AllocateRWX(size, BOld, &ErrMsg);
416 "Allocation failed when allocating new memory in the JIT\n%s\n",
425 JITMemoryManager *JITMemoryManager::CreateDefaultMemManager() {
426 return new DefaultJITMemoryManager();