RecyclingAllocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the RecyclingAllocator class. See the doxygen comment for
  15. // RecyclingAllocator for more details on the implementation.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
  19. #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
  20. #include "llvm/Support/Recycler.h"
  21. namespace llvm {
  22. /// RecyclingAllocator - This class wraps an Allocator, adding the
  23. /// functionality of recycling deleted objects.
  24. ///
  25. template <class AllocatorType, class T, size_t Size = sizeof(T),
  26. size_t Align = alignof(T)>
  27. class RecyclingAllocator {
  28. private:
  29. /// Base - Implementation details.
  30. ///
  31. Recycler<T, Size, Align> Base;
  32. /// Allocator - The wrapped allocator.
  33. ///
  34. AllocatorType Allocator;
  35. public:
  36. ~RecyclingAllocator() { Base.clear(Allocator); }
  37. /// Allocate - Return a pointer to storage for an object of type
  38. /// SubClass. The storage may be either newly allocated or recycled.
  39. ///
  40. template<class SubClass>
  41. SubClass *Allocate() { return Base.template Allocate<SubClass>(Allocator); }
  42. T *Allocate() { return Base.Allocate(Allocator); }
  43. /// Deallocate - Release storage for the pointed-to object. The
  44. /// storage will be kept track of and may be recycled.
  45. ///
  46. template<class SubClass>
  47. void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
  48. void PrintStats() {
  49. Allocator.PrintStats();
  50. Base.PrintStats();
  51. }
  52. };
  53. }
  54. template<class AllocatorType, class T, size_t Size, size_t Align>
  55. inline void *operator new(size_t size,
  56. llvm::RecyclingAllocator<AllocatorType,
  57. T, Size, Align> &Allocator) {
  58. assert(size <= Size && "allocation size exceeded");
  59. return Allocator.Allocate();
  60. }
  61. template<class AllocatorType, class T, size_t Size, size_t Align>
  62. inline void operator delete(void *E,
  63. llvm::RecyclingAllocator<AllocatorType,
  64. T, Size, Align> &A) {
  65. A.Deallocate(E);
  66. }
  67. #endif
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic pop
  70. #endif