AllocatorBase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===- AllocatorBase.h - Simple memory allocation abstraction ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file defines MallocAllocator. MallocAllocator conforms to the LLVM
  11. /// "Allocator" concept which consists of an Allocate method accepting a size
  12. /// and alignment, and a Deallocate accepting a pointer and size. Further, the
  13. /// LLVM "Allocator" concept has overloads of Allocate and Deallocate for
  14. /// setting size and alignment based on the final type. These overloads are
  15. /// typically provided by a base class template \c AllocatorBase.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_ALLOCATORBASE_H
  19. #define LLVM_SUPPORT_ALLOCATORBASE_H
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/MemAlloc.h"
  22. namespace llvm {
  23. /// CRTP base class providing obvious overloads for the core \c
  24. /// Allocate() methods of LLVM-style allocators.
  25. ///
  26. /// This base class both documents the full public interface exposed by all
  27. /// LLVM-style allocators, and redirects all of the overloads to a single core
  28. /// set of methods which the derived class must define.
  29. template <typename DerivedT> class AllocatorBase {
  30. public:
  31. /// Allocate \a Size bytes of \a Alignment aligned memory. This method
  32. /// must be implemented by \c DerivedT.
  33. void *Allocate(size_t Size, size_t Alignment) {
  34. #ifdef __clang__
  35. static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>(
  36. &AllocatorBase::Allocate) !=
  37. static_cast<void *(DerivedT::*)(size_t, size_t)>(
  38. &DerivedT::Allocate),
  39. "Class derives from AllocatorBase without implementing the "
  40. "core Allocate(size_t, size_t) overload!");
  41. #endif
  42. return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
  43. }
  44. /// Deallocate \a Ptr to \a Size bytes of memory allocated by this
  45. /// allocator.
  46. void Deallocate(const void *Ptr, size_t Size, size_t Alignment) {
  47. #ifdef __clang__
  48. static_assert(
  49. static_cast<void (AllocatorBase::*)(const void *, size_t, size_t)>(
  50. &AllocatorBase::Deallocate) !=
  51. static_cast<void (DerivedT::*)(const void *, size_t, size_t)>(
  52. &DerivedT::Deallocate),
  53. "Class derives from AllocatorBase without implementing the "
  54. "core Deallocate(void *) overload!");
  55. #endif
  56. return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size, Alignment);
  57. }
  58. // The rest of these methods are helpers that redirect to one of the above
  59. // core methods.
  60. /// Allocate space for a sequence of objects without constructing them.
  61. template <typename T> T *Allocate(size_t Num = 1) {
  62. return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
  63. }
  64. /// Deallocate space for a sequence of objects without constructing them.
  65. template <typename T>
  66. std::enable_if_t<!std::is_same<std::remove_cv_t<T>, void>::value, void>
  67. Deallocate(T *Ptr, size_t Num = 1) {
  68. Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T), alignof(T));
  69. }
  70. };
  71. class MallocAllocator : public AllocatorBase<MallocAllocator> {
  72. public:
  73. void Reset() {}
  74. LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, size_t Alignment) {
  75. return allocate_buffer(Size, Alignment);
  76. }
  77. // Pull in base class overloads.
  78. using AllocatorBase<MallocAllocator>::Allocate;
  79. void Deallocate(const void *Ptr, size_t Size, size_t Alignment) {
  80. deallocate_buffer(const_cast<void *>(Ptr), Size, Alignment);
  81. }
  82. // Pull in base class overloads.
  83. using AllocatorBase<MallocAllocator>::Deallocate;
  84. void PrintStats() const {}
  85. };
  86. } // namespace llvm
  87. #endif // LLVM_SUPPORT_ALLOCATORBASE_H