AllocatorBase.h 4.1 KB

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