1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the BumpPtrAllocator interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/System/DataTypes.h"
16 #include "llvm/Support/Recycler.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/System/Memory.h"
23 BumpPtrAllocator::BumpPtrAllocator(size_t size, size_t threshold,
24 SlabAllocator &allocator)
25 : SlabSize(size), SizeThreshold(threshold), Allocator(allocator),
26 CurSlab(0), BytesAllocated(0) {
30 BumpPtrAllocator::~BumpPtrAllocator() {
31 DeallocateSlabs(CurSlab);
34 /// 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.
37 char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
38 assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
39 "Alignment is not a power of two!");
42 return (char*)(((uintptr_t)Ptr + Alignment - 1) &
43 ~(uintptr_t)(Alignment - 1));
46 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
47 /// the new slab. Modifies CurPtr and End.
48 void BumpPtrAllocator::StartNewSlab() {
49 MemSlab *NewSlab = Allocator.Allocate(SlabSize);
50 NewSlab->NextPtr = CurSlab;
52 CurPtr = (char*)(CurSlab + 1);
53 End = ((char*)CurSlab) + CurSlab->Size;
56 /// DeallocateSlabs - Deallocate all memory slabs after and including this
58 void BumpPtrAllocator::DeallocateSlabs(MemSlab *Slab) {
60 MemSlab *NextSlab = Slab->NextPtr;
62 // Poison the memory so stale pointers crash sooner. Note we must
63 // preserve the Size and NextPtr fields at the beginning.
64 sys::Memory::setRangeWritable(Slab + 1, Slab->Size - sizeof(MemSlab));
65 memset(Slab + 1, 0xCD, Slab->Size - sizeof(MemSlab));
67 Allocator.Deallocate(Slab);
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.
74 void BumpPtrAllocator::Reset() {
75 DeallocateSlabs(CurSlab->NextPtr);
77 CurPtr = (char*)(CurSlab + 1);
78 End = ((char*)CurSlab) + CurSlab->Size;
81 void BumpPtrAllocator::Reset(size_t Size, size_t Alignment, DTorFunction DTor) {
82 if (Alignment == 0) Alignment = 1;
83 MemSlab *Slab = CurSlab;
85 char *End = Slab == CurSlab ? CurPtr : (char*)Slab + Slab->Size;
86 for (char *Ptr = (char*)Slab+1; Ptr < End; Ptr += Size) {
87 Ptr = AlignPtr(Ptr, Alignment);
88 if (Ptr + Size <= End)
96 /// Allocate - Allocate space at the specified alignment.
98 void *BumpPtrAllocator::Allocate(size_t Size, size_t Alignment) {
99 // Keep track of how many bytes we've allocated.
100 BytesAllocated += Size;
102 // 0-byte alignment means 1-byte alignment.
103 if (Alignment == 0) Alignment = 1;
105 // Allocate the aligned space, going forwards from CurPtr.
106 char *Ptr = AlignPtr(CurPtr, Alignment);
108 // Check if we can hold it.
109 if (Ptr + Size <= End) {
114 // If Size is really big, allocate a separate slab for it.
115 size_t PaddedSize = Size + sizeof(MemSlab) + Alignment - 1;
116 if (PaddedSize > SizeThreshold) {
117 MemSlab *NewSlab = Allocator.Allocate(PaddedSize);
119 // Put the new slab after the current slab, since we are not allocating
121 NewSlab->NextPtr = CurSlab->NextPtr;
122 CurSlab->NextPtr = NewSlab;
124 Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
125 assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
129 // Otherwise, start a new slab and try again.
131 Ptr = AlignPtr(CurPtr, Alignment);
133 assert(CurPtr <= End && "Unable to allocate memory!");
137 unsigned BumpPtrAllocator::GetNumSlabs() const {
138 unsigned NumSlabs = 0;
139 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
145 void BumpPtrAllocator::PrintStats() const {
146 unsigned NumSlabs = 0;
147 size_t TotalMemory = 0;
148 for (MemSlab *Slab = CurSlab; Slab != 0; Slab = Slab->NextPtr) {
149 TotalMemory += Slab->Size;
153 errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
154 << "Bytes used: " << BytesAllocated << '\n'
155 << "Bytes allocated: " << TotalMemory << '\n'
156 << "Bytes wasted: " << (TotalMemory - BytesAllocated)
157 << " (includes alignment, etc)\n";
160 MallocSlabAllocator BumpPtrAllocator::DefaultSlabAllocator =
161 MallocSlabAllocator();
163 SlabAllocator::~SlabAllocator() { }
165 MallocSlabAllocator::~MallocSlabAllocator() { }
167 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
168 MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
174 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
175 Allocator.Deallocate(Slab);
178 void PrintRecyclerStats(size_t Size,
180 size_t FreeListSize) {
181 errs() << "Recycler element size: " << Size << '\n'
182 << "Recycler element alignment: " << Align << '\n'
183 << "Number of elements free for recycling: " << FreeListSize << '\n';