14211488a5953fa2a813e784c5d000b7a3abb4ab
[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 is distributed under the University of Illinois Open Source
6 // 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
29   void *Allocate(size_t Size, size_t Alignment) { return malloc(Size); }
30   
31   template <typename T>
32   T *Allocate() { return static_cast<T*>(malloc(sizeof(T))); }
33   
34   void Deallocate(void *Ptr) { free(Ptr); }
35
36   void PrintStats() const {}
37 };
38
39 /// BumpPtrAllocator - This allocator is useful for containers that need very
40 /// simple memory allocation strategies.  In particular, this just keeps
41 /// allocating memory, and never deletes it until the entire block is dead. This
42 /// makes allocation speedy, but must only be used when the trade-off is ok.
43 class BumpPtrAllocator {
44   void *TheMemory;
45 public:
46   BumpPtrAllocator();
47   ~BumpPtrAllocator();
48   
49   void Reset();
50
51   void *Allocate(size_t Size, size_t Alignment);
52
53   template <typename T>
54   T *Allocate() { 
55     return static_cast<T*>(Allocate(sizeof(T),AlignOf<T>::Alignment));
56   }
57   
58   void Deallocate(void *Ptr) {}
59
60   void PrintStats() const;
61 };
62
63 }  // end namespace llvm
64
65 #endif