diff options
Diffstat (limited to 'libbase/include/core/memory/AllocatorBase.h')
| -rw-r--r-- | libbase/include/core/memory/AllocatorBase.h | 122 |
1 files changed, 84 insertions, 38 deletions
diff --git a/libbase/include/core/memory/AllocatorBase.h b/libbase/include/core/memory/AllocatorBase.h index 0f7f9f1..af6f5ca 100644 --- a/libbase/include/core/memory/AllocatorBase.h +++ b/libbase/include/core/memory/AllocatorBase.h @@ -41,7 +41,7 @@ #define LIBBASE_SUPPORT_ALLOCATORBASE_H #ifdef _MSC_VER -#define LIBBASE_ALLOCATORHOLDER_EMPTYBASE __declspec(empty_bases) +#define LIBBASE_ALLOCATORHOLDER_EMPTYBASE __declspec (empty_bases) #else #define LIBBASE_ALLOCATORHOLDER_EMPTYBASE #endif // _MSC_VER @@ -51,7 +51,8 @@ #include <type_traits> #include <utility> -namespace libbase { +namespace libbase +{ /// CRTP base class providing obvious overloads for the core \c /// Allocate() methods of LLVM-style allocators. @@ -59,92 +60,137 @@ namespace libbase { /// This base class both documents the full public interface exposed by all /// LLVM-style allocators, and redirects all of the overloads to a single core /// set of methods which the derived class must define. -template <typename DerivedT> class AllocatorBase { +template <typename DerivedT> class AllocatorBase +{ public: /// Allocate \a Size bytes of \a Alignment aligned memory. This method /// must be implemented by \c DerivedT. - void *Allocate(size_t Size, size_t Alignment) { + void * + Allocate (size_t Size, size_t Alignment) + { #ifdef __clang__ - static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>( - &AllocatorBase::Allocate) != - static_cast<void *(DerivedT::*)(size_t, size_t)>( - &DerivedT::Allocate), - "Class derives from AllocatorBase without implementing the " - "core Allocate(size_t, size_t) overload!"); + static_assert (static_cast<void *(AllocatorBase::*) (size_t, size_t)> ( + &AllocatorBase::Allocate) != + static_cast<void *(DerivedT::*) (size_t, size_t)> ( + &DerivedT::Allocate), + "Class derives from AllocatorBase without implementing the " + "core Allocate(size_t, size_t) overload!"); #endif - return static_cast<DerivedT *>(this)->Allocate(Size, Alignment); + return static_cast<DerivedT *> (this)->Allocate (Size, Alignment); } /// Deallocate \a Ptr to \a Size bytes of memory allocated by this /// allocator. - void Deallocate(const void *Ptr, size_t Size, size_t Alignment) { + void + Deallocate (const void *Ptr, size_t Size, size_t Alignment) + { #ifdef __clang__ - static_assert( - static_cast<void (AllocatorBase::*)(const void *, size_t, size_t)>( + static_assert ( + static_cast<void (AllocatorBase::*) (const void *, size_t, size_t)> ( &AllocatorBase::Deallocate) != - static_cast<void (DerivedT::*)(const void *, size_t, size_t)>( + static_cast<void (DerivedT::*) (const void *, size_t, size_t)> ( &DerivedT::Deallocate), "Class derives from AllocatorBase without implementing the " "core Deallocate(void *) overload!"); #endif - return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size, Alignment); + return static_cast<DerivedT *> (this)->Deallocate (Ptr, Size, Alignment); } // The rest of these methods are helpers that redirect to one of the above // core methods. /// Allocate space for a sequence of objects without constructing them. - template <typename T> T *Allocate(size_t Num = 1) { - return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T))); + template <typename T> + T * + Allocate (size_t Num = 1) + { + return static_cast<T *> (Allocate (Num * sizeof (T), alignof (T))); } /// Deallocate space for a sequence of objects without constructing them. template <typename T> std::enable_if_t<!std::is_same_v<std::remove_cv_t<T>, void>, void> - Deallocate(T *Ptr, size_t Num = 1) { - Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T), alignof(T)); + Deallocate (T *Ptr, size_t Num = 1) + { + Deallocate (static_cast<const void *> (Ptr), Num * sizeof (T), alignof (T)); } }; -class MallocAllocator : public AllocatorBase<MallocAllocator> { +class MallocAllocator : public AllocatorBase<MallocAllocator> +{ public: - void Reset() {} + void + Reset () + { + } - LIBBASE_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, size_t Alignment) { - return allocate_buffer(Size, Alignment); + LIBBASE_ATTRIBUTE_RETURNS_NONNULL void * + Allocate (size_t Size, size_t Alignment) + { + return allocate_buffer (Size, Alignment); } // Pull in base class overloads. using AllocatorBase<MallocAllocator>::Allocate; - void Deallocate(const void *Ptr, size_t Size, size_t Alignment) { - deallocate_buffer(const_cast<void *>(Ptr), Size, Alignment); + void + Deallocate (const void *Ptr, size_t Size, size_t Alignment) + { + deallocate_buffer (const_cast<void *> (Ptr), Size, Alignment); } // Pull in base class overloads. using AllocatorBase<MallocAllocator>::Deallocate; - void PrintStats() const {} + void + PrintStats () const + { + } }; -namespace detail { +namespace detail +{ -template <typename Alloc> class AllocatorHolder : Alloc { +template <typename Alloc> class AllocatorHolder : Alloc +{ public: - AllocatorHolder() = default; - AllocatorHolder(const Alloc &A) : Alloc(A) {} - AllocatorHolder(Alloc &&A) : Alloc(std::move(A)) {} - Alloc &getAllocator() { return *this; } - const Alloc &getAllocator() const { return *this; } + AllocatorHolder () = default; + AllocatorHolder (const Alloc &A) : Alloc (A) + { + } + AllocatorHolder (Alloc &&A) : Alloc (std::move (A)) + { + } + Alloc & + getAllocator () + { + return *this; + } + const Alloc & + getAllocator () const + { + return *this; + } }; -template <typename Alloc> class AllocatorHolder<Alloc &> { +template <typename Alloc> class AllocatorHolder<Alloc &> +{ Alloc &A; public: - AllocatorHolder(Alloc &A) : A(A) {} - Alloc &getAllocator() { return A; } - const Alloc &getAllocator() const { return A; } + AllocatorHolder (Alloc &A) : A (A) + { + } + Alloc & + getAllocator () + { + return A; + } + const Alloc & + getAllocator () const + { + return A; + } }; } // namespace detail |
