blob: 31b45c8d4aae77538940f8f1b9e948f7f2726e23 [file] [log] [blame]
Chris Lattner9f617d62006-10-29 22:08:03 +00001//===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9f617d62006-10-29 22:08:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BumpPtrAllocator interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Allocator.h"
Chandler Carruth8b67f772009-10-26 01:35:46 +000015#include "llvm/System/DataTypes.h"
Reid Kleckner8f51a622009-07-23 18:34:13 +000016#include "llvm/Support/Recycler.h"
Daniel Dunbar7da95592009-07-24 04:01:01 +000017#include "llvm/Support/raw_ostream.h"
Evan Chengc48edbb2009-09-09 01:45:24 +000018#include "llvm/System/Memory.h"
Reid Kleckner8f51a622009-07-23 18:34:13 +000019#include <cstring>
Chris Lattner9f617d62006-10-29 22:08:03 +000020
Reid Kleckner8f51a622009-07-23 18:34:13 +000021namespace llvm {
Chris Lattner9f617d62006-10-29 22:08:03 +000022
Reid Kleckner8f51a622009-07-23 18:34:13 +000023BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
24 SlabAllocator &allocator)
25 : SlabSize(size), SizeThreshold(threshold), Allocator(allocator),
26 CurSlab(0), BytesAllocated(0) {
27 StartNewSlab();
Chris Lattner9f617d62006-10-29 22:08:03 +000028}
29
30BumpPtrAllocator::~BumpPtrAllocator() {
Reid Kleckner8f51a622009-07-23 18:34:13 +000031 DeallocateSlabs(CurSlab);
Chris Lattner9f617d62006-10-29 22:08:03 +000032}
33
Reid Kleckner8f51a622009-07-23 18:34:13 +000034/// AlignPtr - Align Ptr to Alignment bytes, rounding up. Alignment should
35/// be a power of two. This method rounds up, so AlignPtr(7, 4) == 8 and
36/// AlignPtr(8, 4) == 8.
37char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
38 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
39 "Alignment is not a power of two!");
40
41 // Do the alignment.
42 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
43 ~(uintptr_t)(Alignment - 1));
44}
45
46/// StartNewSlab - Allocate a new slab and move the bump pointers over into
47/// the new slab. Modifies CurPtr and End.
48void BumpPtrAllocator::StartNewSlab() {
49 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
50 NewSlab->NextPtr = CurSlab;
51 CurSlab = NewSlab;
52 CurPtr = (char*)(CurSlab + 1);
53 End = ((char*)CurSlab) + CurSlab->Size;
54}
55
56/// DeallocateSlabs - Deallocate all memory slabs after and including this
57/// one.
58void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
59 while (Slab) {
60 MemSlab *NextSlab = Slab->NextPtr;
61#ifndef NDEBUG
62 // Poison the memory so stale pointers crash sooner. Note we must
63 // preserve the Size and NextPtr fields at the beginning.
Evan Chengc48edbb2009-09-09 01:45:24 +000064 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
Reid Kleckner8f51a622009-07-23 18:34:13 +000065 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
66#endif
67 Allocator.Deallocate(Slab);
68 Slab = NextSlab;
69 }
70}
71
72/// Reset - Deallocate all but the current slab and reset the current pointer
73/// to the beginning of it, freeing all memory allocated so far.
Evan Cheng188b5222007-09-05 21:41:34 +000074void BumpPtrAllocator::Reset() {
Reid Kleckner8f51a622009-07-23 18:34:13 +000075 DeallocateSlabs(CurSlab->NextPtr);
76 CurSlab->NextPtr = 0;
77 CurPtr = (char*)(CurSlab + 1);
78 End = ((char*)CurSlab) + CurSlab->Size;
Evan Cheng188b5222007-09-05 21:41:34 +000079}
80
Reid Kleckner8f51a622009-07-23 18:34:13 +000081/// Allocate - Allocate space at the specified alignment.
82///
83void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
84 // Keep track of how many bytes we've allocated.
85 BytesAllocated += Size;
86
87 // 0-byte alignment means 1-byte alignment.
88 if (Alignment == 0) Alignment = 1;
89
90 // Allocate the aligned space, going forwards from CurPtr.
91 char *Ptr = AlignPtr(CurPtr, Alignment);
92
93 // Check if we can hold it.
94 if (Ptr + Size <= End) {
95 CurPtr = Ptr + Size;
96 return Ptr;
97 }
98
99 // If Size is really big, allocate a separate slab for it.
Reid Kleckner7d509132009-07-25 21:26:02 +0000100 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
101 if (PaddedSize > SizeThreshold) {
Reid Kleckner8f51a622009-07-23 18:34:13 +0000102 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
103
104 // Put the new slab after the current slab, since we are not allocating
105 // into it.
106 NewSlab->NextPtr = CurSlab->NextPtr;
107 CurSlab->NextPtr = NewSlab;
108
109 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
110 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
111 return Ptr;
112 }
113
114 // Otherwise, start a new slab and try again.
115 StartNewSlab();
116 Ptr = AlignPtr(CurPtr, Alignment);
117 CurPtr = Ptr + Size;
118 assert(CurPtr <= End && "Unable to allocate memory!");
Chris Lattnerd675b832007-02-23 22:31:24 +0000119 return Ptr;
Chris Lattner9f617d62006-10-29 22:08:03 +0000120}
121
Reid Kleckner8f51a622009-07-23 18:34:13 +0000122unsigned BumpPtrAllocator::GetNumSlabs() const {
123 unsigned NumSlabs = 0;
124 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
125 ++NumSlabs;
126 }
127 return NumSlabs;
Reid Kleckner95eb3ad2009-07-23 00:30:41 +0000128}
129
Reid Kleckner8f51a622009-07-23 18:34:13 +0000130void BumpPtrAllocator::PrintStats() const {
131 unsigned NumSlabs = 0;
132 size_t TotalMemory = 0;
133 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
134 TotalMemory += Slab->Size;
135 ++NumSlabs;
136 }
137
Daniel Dunbar7da95592009-07-24 04:01:01 +0000138 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
139 << "Bytes used: " << BytesAllocated << '\n'
140 << "Bytes allocated: " << TotalMemory << '\n'
141 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
142 << " (includes alignment, etc)\n";
Reid Kleckner8f51a622009-07-23 18:34:13 +0000143}
144
145MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
146 MallocSlabAllocator();
147
148SlabAllocator::~SlabAllocator() { }
149
150MallocSlabAllocator::~MallocSlabAllocator() { }
151
152MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
153 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
154 Slab->Size = Size;
155 Slab->NextPtr = 0;
156 return Slab;
157}
158
159void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
160 Allocator.Deallocate(Slab);
161}
162
163void PrintRecyclerStats(size_t Size,
164 size_t Align,
165 size_t FreeListSize) {
Daniel Dunbar7da95592009-07-24 04:01:01 +0000166 errs() << "Recycler element size: " << Size << '\n'
167 << "Recycler element alignment: " << Align << '\n'
168 << "Number of elements free for recycling: " << FreeListSize << '\n';
Reid Kleckner8f51a622009-07-23 18:34:13 +0000169}
170
Dan Gohmane14d81d2008-07-07 22:58:06 +0000171}