729cc674a8cd7b995fe1cd347284e71c7ca4d1ca
[oota-llvm.git] / include / llvm / Support / Allocator.h
1 //===--- Allocator.h - Simple memory allocation abstraction -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MallocAllocator and BumpPtrAllocator interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_ALLOCATOR_H
15 #define LLVM_SUPPORT_ALLOCATOR_H
16
17 #include "llvm/Support/AlignOf.h"
18 #include <cstdlib>
19
20 namespace llvm {
21     
22 class MallocAllocator {
23 public:
24   MallocAllocator() {}
25   ~MallocAllocator() {}
26   
27   void Reset() {}
28   void *Allocate(unsigned Size, unsigned Alignment) { return malloc(Size); }
29   
30   template <typename T>
31   void *Allocate() { return reinterpret_cast<T*>(malloc(sizeof(T))); }
32   
33   void Deallocate(void *Ptr) { free(Ptr); }
34   void PrintStats() const {}
35 };
36
37 /// BumpPtrAllocator - This allocator is useful for containers that need very
38 /// simple memory allocation strategies.  In particular, this just keeps
39 /// allocating memory, and never deletes it until the entire block is dead. This
40 /// makes allocation speedy, but must only be used when the trade-off is ok.
41 class BumpPtrAllocator {
42   void *TheMemory;
43 public:
44   BumpPtrAllocator();
45   ~BumpPtrAllocator();
46   
47   void Reset();
48   void *Allocate(unsigned Size, unsigned Alignment);
49
50   template <typename T>
51   void *Allocate() { 
52     return reinterpret_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
53   }
54
55   
56   void Deallocate(void *Ptr) {}
57   void PrintStats() const;
58 };
59
60 }  // end namespace clang
61
62 #endif