File indexing completed on 2026-05-10 08:44:33
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_SUPPORT_PERTHREADBUMPPTRALLOCATOR_H
0010 #define LLVM_SUPPORT_PERTHREADBUMPPTRALLOCATOR_H
0011
0012 #include "llvm/Support/Allocator.h"
0013 #include "llvm/Support/Parallel.h"
0014
0015 namespace llvm {
0016 namespace parallel {
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 template <typename AllocatorTy>
0027 class PerThreadAllocator
0028 : public AllocatorBase<PerThreadAllocator<AllocatorTy>> {
0029 public:
0030 PerThreadAllocator()
0031 : NumOfAllocators(parallel::getThreadCount()),
0032 Allocators(std::make_unique<AllocatorTy[]>(NumOfAllocators)) {}
0033
0034
0035
0036
0037
0038 using AllocatorBase<PerThreadAllocator<AllocatorTy>>::Allocate;
0039
0040 using AllocatorBase<PerThreadAllocator<AllocatorTy>>::Deallocate;
0041
0042
0043 void *Allocate(size_t Size, size_t Alignment) {
0044 assert(getThreadIndex() < NumOfAllocators);
0045 return Allocators[getThreadIndex()].Allocate(Size, Alignment);
0046 }
0047
0048
0049
0050 void Deallocate(const void *Ptr, size_t Size, size_t Alignment) {
0051 assert(getThreadIndex() < NumOfAllocators);
0052 return Allocators[getThreadIndex()].Deallocate(Ptr, Size, Alignment);
0053 }
0054
0055
0056 AllocatorTy &getThreadLocalAllocator() {
0057 assert(getThreadIndex() < NumOfAllocators);
0058 return Allocators[getThreadIndex()];
0059 }
0060
0061
0062 size_t getNumberOfAllocators() const { return NumOfAllocators; }
0063
0064
0065
0066
0067
0068
0069
0070 void Reset() {
0071 for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0072 Allocators[Idx].Reset();
0073 }
0074
0075
0076 size_t getTotalMemory() const {
0077 size_t TotalMemory = 0;
0078
0079 for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0080 TotalMemory += Allocators[Idx].getTotalMemory();
0081
0082 return TotalMemory;
0083 }
0084
0085
0086 size_t getBytesAllocated() const {
0087 size_t BytesAllocated = 0;
0088
0089 for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0090 BytesAllocated += Allocators[Idx].getBytesAllocated();
0091
0092 return BytesAllocated;
0093 }
0094
0095
0096 void setRedZoneSize(size_t NewSize) {
0097 for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0098 Allocators[Idx].setRedZoneSize(NewSize);
0099 }
0100
0101
0102 void PrintStats() const {
0103 for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++) {
0104 errs() << "\n Allocator " << Idx << "\n";
0105 Allocators[Idx].PrintStats();
0106 }
0107 }
0108
0109
0110 protected:
0111 size_t NumOfAllocators;
0112 std::unique_ptr<AllocatorTy[]> Allocators;
0113 };
0114
0115 using PerThreadBumpPtrAllocator = PerThreadAllocator<BumpPtrAllocator>;
0116
0117 }
0118 }
0119
0120 #endif