LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RecyclingAllocator.h
Go to the documentation of this file.
1 //==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- 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 RecyclingAllocator class. See the doxygen comment for
11 // RecyclingAllocator for more details on the implementation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
16 #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
17 
18 #include "llvm/Support/Recycler.h"
19 
20 namespace llvm {
21 
22 /// RecyclingAllocator - This class wraps an Allocator, adding the
23 /// functionality of recycling deleted objects.
24 ///
25 template<class AllocatorType, class T,
26  size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
28 private:
29  /// Base - Implementation details.
30  ///
32 
33  /// Allocator - The wrapped allocator.
34  ///
35  AllocatorType Allocator;
36 
37 public:
38  ~RecyclingAllocator() { Base.clear(Allocator); }
39 
40  /// Allocate - Return a pointer to storage for an object of type
41  /// SubClass. The storage may be either newly allocated or recycled.
42  ///
43  template<class SubClass>
44  SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
45 
46  T *Allocate() { return Base.Allocate(Allocator); }
47 
48  /// Deallocate - Release storage for the pointed-to object. The
49  /// storage will be kept track of and may be recycled.
50  ///
51  template<class SubClass>
52  void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
53 
54  void PrintStats() {
55  Allocator.PrintStats();
56  Base.PrintStats();
57  }
58 };
59 
60 }
61 
62 template<class AllocatorType, class T, size_t Size, size_t Align>
63 inline void *operator new(size_t size,
64  llvm::RecyclingAllocator<AllocatorType,
65  T, Size, Align> &Allocator) {
66  assert(size <= Size && "allocation size exceeded");
67  return Allocator.Allocate();
68 }
69 
70 template<class AllocatorType, class T, size_t Size, size_t Align>
71 inline void operator delete(void *E,
72  llvm::RecyclingAllocator<AllocatorType,
73  T, Size, Align> &A) {
74  A.Deallocate(E);
75 }
76 
77 #endif
void PrintStats()
Definition: Recycler.h:122
void Deallocate(SubClass *E)
#define T
SubClass * Allocate(AllocatorType &Allocator)
Definition: Recycler.h:102
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
void Deallocate(AllocatorType &, SubClass *Element)
Definition: Recycler.h:118
void clear(AllocatorType &Allocator)
Definition: Recycler.h:85