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
19#ifndef LLVM_SUPPORT_ALLOCATORBASE_H
20#define LLVM_SUPPORT_ALLOCATORBASE_H
21
22#include "llvm/Support/Compiler.h"
23#include "llvm/Support/MemAlloc.h"
24
25namespace llvm {
26
27/// CRTP base class providing obvious overloads for the core \c
28/// Allocate() methods of LLVM-style allocators.
29///
30/// This base class both documents the full public interface exposed by all
31/// LLVM-style allocators, and redirects all of the overloads to a single core
32/// set of methods which the derived class must define.
33template <typename DerivedT> class AllocatorBase {
34public:
35 /// Allocate \a Size bytes of \a Alignment aligned memory. This method
36 /// must be implemented by \c DerivedT.
37 void *Allocate(size_t Size, size_t Alignment) {
38#ifdef __clang__
39 static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>(
40 &AllocatorBase::Allocate) !=
41 static_cast<void *(DerivedT::*)(size_t, size_t)>(
42 &DerivedT::Allocate),
43 "Class derives from AllocatorBase without implementing the "
44 "core Allocate(size_t, size_t) overload!");
45#endif
46 return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
47 }
48
49 /// Deallocate \a Ptr to \a Size bytes of memory allocated by this
50 /// allocator.
51 void Deallocate(const void *Ptr, size_t Size) {
52#ifdef __clang__
53 static_assert(static_cast<void (AllocatorBase::*)(const void *, size_t)>(
54 &AllocatorBase::Deallocate) !=
55 static_cast<void (DerivedT::*)(const void *, size_t)>(
56 &DerivedT::Deallocate),
57 "Class derives from AllocatorBase without implementing the "
58 "core Deallocate(void *) overload!");
59#endif
60 return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size);
61 }
62
63 // The rest of these methods are helpers that redirect to one of the above
64 // core methods.
65
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
71 /// Deallocate space for a sequence of objects without constructing them.
72 template <typename T>
73 std::enable_if_t<!std::is_same<std::remove_cv_t<T>, void>::value, void>
74 Deallocate(T *Ptr, size_t Num = 1) {
75 Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
76 }
77};
78
79class MallocAllocator : public AllocatorBase<MallocAllocator> {
80public:
81 void Reset() {}
82
83 LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
84 size_t /*Alignment*/) {
85 return safe_malloc(Size);
86 }
87
88 // Pull in base class overloads.
89 using AllocatorBase<MallocAllocator>::Allocate;
90
91 void Deallocate(const void *Ptr, size_t /*Size*/) {
92 free(const_cast<void *>(Ptr));
93 }
94
95 // Pull in base class overloads.
96 using AllocatorBase<MallocAllocator>::Deallocate;
97
98 void PrintStats() const {}
99};
100
101} // namespace llvm
102
103#endif // LLVM_SUPPORT_ALLOCATORBASE_H
104