diff --git a/llvm/include/llvm/ADT/ScopedHashTable.h b/llvm/include/llvm/ADT/ScopedHashTable.h index 40c49ebc0be1f..a5e57c6a16c2e 100644 --- a/llvm/include/llvm/ADT/ScopedHashTable.h +++ b/llvm/include/llvm/ADT/ScopedHashTable.h @@ -32,7 +32,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" -#include "llvm/Support/Allocator.h" +#include "llvm/Support/AllocatorBase.h" #include #include diff --git a/llvm/include/llvm/ADT/SparseSet.h b/llvm/include/llvm/ADT/SparseSet.h index a6eb9b942e804..35e9e0cb405b0 100644 --- a/llvm/include/llvm/ADT/SparseSet.h +++ b/llvm/include/llvm/ADT/SparseSet.h @@ -21,7 +21,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/Support/Allocator.h" +#include "llvm/Support/AllocatorBase.h" #include #include #include diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index 108185bd07b90..8e8323f4dd9c5 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -16,9 +16,8 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" -#include "llvm/Support/Allocator.h" +#include "llvm/Support/AllocatorBase.h" #include "llvm/Support/PointerLikeTypeTraits.h" -#include "llvm/Support/ErrorHandling.h" #include #include #include diff --git a/llvm/include/llvm/ADT/StringSet.h b/llvm/include/llvm/ADT/StringSet.h index 60be09d3c3264..c740aee41a31d 100644 --- a/llvm/include/llvm/ADT/StringSet.h +++ b/llvm/include/llvm/ADT/StringSet.h @@ -15,7 +15,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/Allocator.h" +#include "llvm/Support/AllocatorBase.h" #include #include #include diff --git a/llvm/include/llvm/BinaryFormat/MachO.h b/llvm/include/llvm/BinaryFormat/MachO.h index fb50e549cb9da..59601756fa1a3 100644 --- a/llvm/include/llvm/BinaryFormat/MachO.h +++ b/llvm/include/llvm/BinaryFormat/MachO.h @@ -15,7 +15,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/DataTypes.h" -#include "llvm/Support/Host.h" +#include "llvm/Support/SwapByteOrder.h" namespace llvm { namespace MachO { diff --git a/llvm/include/llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h b/llvm/include/llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h index 53ab2dd04aa7d..0e2f5d90e243b 100644 --- a/llvm/include/llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h +++ b/llvm/include/llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h @@ -18,7 +18,6 @@ #include "llvm/DebugInfo/CodeView/TypeRecord.h" #include "llvm/DebugInfo/CodeView/TypeRecordMapping.h" #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" -#include "llvm/Support/Allocator.h" #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryStreamWriter.h" #include "llvm/Support/Error.h" diff --git a/llvm/include/llvm/DebugInfo/PDB/Native/SymbolCache.h b/llvm/include/llvm/DebugInfo/PDB/Native/SymbolCache.h index 4adf3b394c2ea..bf9eb717f9806 100644 --- a/llvm/include/llvm/DebugInfo/PDB/Native/SymbolCache.h +++ b/llvm/include/llvm/DebugInfo/PDB/Native/SymbolCache.h @@ -14,7 +14,6 @@ #include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" #include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h" -#include "llvm/Support/Allocator.h" #include #include diff --git a/llvm/include/llvm/Object/IRSymtab.h b/llvm/include/llvm/Object/IRSymtab.h index 0bbfc932493c6..4ee32fca81bbe 100644 --- a/llvm/include/llvm/Object/IRSymtab.h +++ b/llvm/include/llvm/Object/IRSymtab.h @@ -28,6 +28,7 @@ #include "llvm/ADT/iterator_range.h" #include "llvm/IR/GlobalValue.h" #include "llvm/Object/SymbolicFile.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Error.h" #include diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h index d3ba55918a2de..4019fe2549be7 100644 --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -22,6 +22,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MathExtras.h" diff --git a/llvm/include/llvm/Remarks/RemarkStringTable.h b/llvm/include/llvm/Remarks/RemarkStringTable.h index 4ce27ee884c8c..c40bb5fa0ecdb 100644 --- a/llvm/include/llvm/Remarks/RemarkStringTable.h +++ b/llvm/include/llvm/Remarks/RemarkStringTable.h @@ -19,6 +19,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Remarks/Remark.h" +#include "llvm/Support/Allocator.h" #include namespace llvm { diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h index 670335ffecbcb..b631815df01be 100644 --- a/llvm/include/llvm/Support/Allocator.h +++ b/llvm/include/llvm/Support/Allocator.h @@ -23,6 +23,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/Alignment.h" +#include "llvm/Support/AllocatorBase.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" @@ -38,81 +39,6 @@ namespace llvm { -/// CRTP base class providing obvious overloads for the core \c -/// Allocate() methods of LLVM-style allocators. -/// -/// 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 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) { -#ifdef __clang__ - static_assert(static_cast( - &AllocatorBase::Allocate) != - static_cast( - &DerivedT::Allocate), - "Class derives from AllocatorBase without implementing the " - "core Allocate(size_t, size_t) overload!"); -#endif - return static_cast(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) { -#ifdef __clang__ - static_assert(static_cast( - &AllocatorBase::Deallocate) != - static_cast( - &DerivedT::Deallocate), - "Class derives from AllocatorBase without implementing the " - "core Deallocate(void *) overload!"); -#endif - return static_cast(this)->Deallocate(Ptr, Size); - } - - // 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 T *Allocate(size_t Num = 1) { - return static_cast(Allocate(Num * sizeof(T), alignof(T))); - } - - /// Deallocate space for a sequence of objects without constructing them. - template - typename std::enable_if< - !std::is_same::type, void>::value, void>::type - Deallocate(T *Ptr, size_t Num = 1) { - Deallocate(static_cast(Ptr), Num * sizeof(T)); - } -}; - -class MallocAllocator : public AllocatorBase { -public: - void Reset() {} - - LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, - size_t /*Alignment*/) { - return safe_malloc(Size); - } - - // Pull in base class overloads. - using AllocatorBase::Allocate; - - void Deallocate(const void *Ptr, size_t /*Size*/) { - free(const_cast(Ptr)); - } - - // Pull in base class overloads. - using AllocatorBase::Deallocate; - - void PrintStats() const {} -}; - namespace detail { // We call out to an external function to actually print the message as the diff --git a/llvm/include/llvm/Support/AllocatorBase.h b/llvm/include/llvm/Support/AllocatorBase.h new file mode 100644 index 0000000000000..5c4328a55fa5d --- /dev/null +++ b/llvm/include/llvm/Support/AllocatorBase.h @@ -0,0 +1,105 @@ +//===- AllocatorBase.h - Simple memory allocation abstraction -------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// \file +/// +/// This file defines the MallocAllocator and BumpPtrAllocator interfaces. Both +/// of these conform to an LLVM "Allocator" concept which consists of an +/// Allocate method accepting a size and alignment, and a Deallocate accepting +/// a pointer and size. Further, the LLVM "Allocator" concept has overloads of +/// Allocate and Deallocate for setting size and alignment based on the final +/// type. These overloads are typically provided by a base class template \c +/// AllocatorBase. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_ALLOCATORBASE_H +#define LLVM_SUPPORT_ALLOCATORBASE_H + +#include "llvm/Support/Compiler.h" +#include "llvm/Support/MemAlloc.h" + +namespace llvm { + +/// CRTP base class providing obvious overloads for the core \c +/// Allocate() methods of LLVM-style allocators. +/// +/// 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 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) { +#ifdef __clang__ + static_assert(static_cast( + &AllocatorBase::Allocate) != + static_cast( + &DerivedT::Allocate), + "Class derives from AllocatorBase without implementing the " + "core Allocate(size_t, size_t) overload!"); +#endif + return static_cast(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) { +#ifdef __clang__ + static_assert(static_cast( + &AllocatorBase::Deallocate) != + static_cast( + &DerivedT::Deallocate), + "Class derives from AllocatorBase without implementing the " + "core Deallocate(void *) overload!"); +#endif + return static_cast(this)->Deallocate(Ptr, Size); + } + + // 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 T *Allocate(size_t Num = 1) { + return static_cast(Allocate(Num * sizeof(T), alignof(T))); + } + + /// Deallocate space for a sequence of objects without constructing them. + template + typename std::enable_if< + !std::is_same::type, void>::value, void>::type + Deallocate(T *Ptr, size_t Num = 1) { + Deallocate(static_cast(Ptr), Num * sizeof(T)); + } +}; + +class MallocAllocator : public AllocatorBase { +public: + void Reset() {} + + LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size, + size_t /*Alignment*/) { + return safe_malloc(Size); + } + + // Pull in base class overloads. + using AllocatorBase::Allocate; + + void Deallocate(const void *Ptr, size_t /*Size*/) { + free(const_cast(Ptr)); + } + + // Pull in base class overloads. + using AllocatorBase::Deallocate; + + void PrintStats() const {} +}; + +} // namespace llvm + +#endif // LLVM_SUPPORT_ALLOCATORBASE_H diff --git a/llvm/include/llvm/Support/BinaryStreamReader.h b/llvm/include/llvm/Support/BinaryStreamReader.h index b7d61c02667b3..c586f865fdc54 100644 --- a/llvm/include/llvm/Support/BinaryStreamReader.h +++ b/llvm/include/llvm/Support/BinaryStreamReader.h @@ -11,6 +11,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Alignment.h" #include "llvm/Support/BinaryStreamArray.h" #include "llvm/Support/BinaryStreamRef.h" #include "llvm/Support/ConvertUTF.h" diff --git a/llvm/include/llvm/Support/Process.h b/llvm/include/llvm/Support/Process.h index 67e37912519bd..9444e165e142a 100644 --- a/llvm/include/llvm/Support/Process.h +++ b/llvm/include/llvm/Support/Process.h @@ -25,7 +25,7 @@ #define LLVM_SUPPORT_PROCESS_H #include "llvm/ADT/Optional.h" -#include "llvm/Support/Allocator.h" +#include "llvm/Support/AllocatorBase.h" #include "llvm/Support/Chrono.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Error.h" diff --git a/llvm/lib/IR/SafepointIRVerifier.cpp b/llvm/lib/IR/SafepointIRVerifier.cpp index f9578394a827c..c9aa85bb4c96e 100644 --- a/llvm/lib/IR/SafepointIRVerifier.cpp +++ b/llvm/lib/IR/SafepointIRVerifier.cpp @@ -45,6 +45,7 @@ #include "llvm/IR/Statepoint.h" #include "llvm/IR/Value.h" #include "llvm/InitializePasses.h" +#include "llvm/Support/Allocator.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" diff --git a/llvm/unittests/Support/BinaryStreamTest.cpp b/llvm/unittests/Support/BinaryStreamTest.cpp index 6d6ecc45c9021..c1db0638ef858 100644 --- a/llvm/unittests/Support/BinaryStreamTest.cpp +++ b/llvm/unittests/Support/BinaryStreamTest.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Support/Allocator.h" #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryItemStream.h" #include "llvm/Support/BinaryStreamArray.h"