11f7ba80a6f14750f0f94a900ce766fb4fcb533f
[oota-llvm.git] / lib / Support / Allocator.cpp
1 //===--- Allocator.cpp - Simple memory allocation abstraction -------------===//
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 implements the BumpPtrAllocator interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/DataTypes.h"
17 #include "llvm/Support/Memory.h"
18 #include "llvm/Support/Recycler.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <cstring>
21
22 namespace llvm {
23
24 SlabAllocator::~SlabAllocator() { }
25
26 MallocSlabAllocator::~MallocSlabAllocator() { }
27
28 MemSlab *MallocSlabAllocator::Allocate(size_t Size) {
29   MemSlab *Slab = (MemSlab*)Allocator.Allocate(Size, 0);
30   Slab->Size = Size;
31   Slab->NextPtr = nullptr;
32   return Slab;
33 }
34
35 void MallocSlabAllocator::Deallocate(MemSlab *Slab) {
36   Allocator.Deallocate(Slab);
37 }
38
39 void BumpPtrAllocatorBase::PrintStats() const {
40   unsigned NumSlabs = 0;
41   size_t TotalMemory = 0;
42   for (MemSlab *Slab = CurSlab; Slab != nullptr; Slab = Slab->NextPtr) {
43     TotalMemory += Slab->Size;
44     ++NumSlabs;
45   }
46
47   errs() << "\nNumber of memory regions: " << NumSlabs << '\n'
48          << "Bytes used: " << BytesAllocated << '\n'
49          << "Bytes allocated: " << TotalMemory << '\n'
50          << "Bytes wasted: " << (TotalMemory - BytesAllocated)
51          << " (includes alignment, etc)\n";
52 }
53
54 size_t BumpPtrAllocatorBase::getTotalMemory() const {
55   size_t TotalMemory = 0;
56   for (MemSlab *Slab = CurSlab; Slab != nullptr; Slab = Slab->NextPtr) {
57     TotalMemory += Slab->Size;
58   }
59   return TotalMemory;
60 }
61
62 void PrintRecyclerStats(size_t Size,
63                         size_t Align,
64                         size_t FreeListSize) {
65   errs() << "Recycler element size: " << Size << '\n'
66          << "Recycler element alignment: " << Align << '\n'
67          << "Number of elements free for recycling: " << FreeListSize << '\n';
68 }
69
70 }