RecyclingAllocator.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
  15. #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
  16. #include "llvm/Support/Recycler.h"
  17. namespace llvm {
  18. /// RecyclingAllocator - This class wraps an Allocator, adding the
  19. /// functionality of recycling deleted objects.
  20. ///
  21. template<class AllocatorType, class T,
  22. size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
  23. class RecyclingAllocator {
  24. private:
  25. /// Base - Implementation details.
  26. ///
  27. Recycler<T, Size, Align> Base;
  28. /// Allocator - The wrapped allocator.
  29. ///
  30. AllocatorType Allocator;
  31. public:
  32. ~RecyclingAllocator() { Base.clear(Allocator); }
  33. /// Allocate - Return a pointer to storage for an object of type
  34. /// SubClass. The storage may be either newly allocated or recycled.
  35. ///
  36. template<class SubClass>
  37. SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
  38. T *Allocate() { return Base.Allocate(Allocator); }
  39. /// Deallocate - Release storage for the pointed-to object. The
  40. /// storage will be kept track of and may be recycled.
  41. ///
  42. template<class SubClass>
  43. void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
  44. void PrintStats() {
  45. Allocator.PrintStats();
  46. Base.PrintStats();
  47. }
  48. };
  49. }
  50. template<class AllocatorType, class T, size_t Size, size_t Align>
  51. inline void *operator new(size_t size,
  52. llvm::RecyclingAllocator<AllocatorType,
  53. T, Size, Align> &Allocator) {
  54. assert(size <= Size && "allocation size exceeded");
  55. return Allocator.Allocate();
  56. }
  57. template<class AllocatorType, class T, size_t Size, size_t Align>
  58. inline void operator delete(void *E,
  59. llvm::RecyclingAllocator<AllocatorType,
  60. T, Size, Align> &A) {
  61. A.Deallocate(E);
  62. }
  63. #endif