Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //===- PerThreadBumpPtrAllocator.h ------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 /// PerThreadAllocator is used in conjunction with ThreadPoolExecutor to allow
0019 /// per-thread allocations. It wraps a possibly thread-unsafe allocator,
0020 /// e.g. BumpPtrAllocator. PerThreadAllocator must be used with only main thread
0021 /// or threads created by ThreadPoolExecutor, as it utilizes getThreadIndex,
0022 /// which is set by ThreadPoolExecutor. To work properly, ThreadPoolExecutor
0023 /// should be initialized before PerThreadAllocator is created.
0024 /// TODO: The same approach might be implemented for ThreadPool.
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   /// \defgroup Methods which could be called asynchronously:
0035   ///
0036   /// @{
0037 
0038   using AllocatorBase<PerThreadAllocator<AllocatorTy>>::Allocate;
0039 
0040   using AllocatorBase<PerThreadAllocator<AllocatorTy>>::Deallocate;
0041 
0042   /// Allocate \a Size bytes of \a Alignment aligned memory.
0043   void *Allocate(size_t Size, size_t Alignment) {
0044     assert(getThreadIndex() < NumOfAllocators);
0045     return Allocators[getThreadIndex()].Allocate(Size, Alignment);
0046   }
0047 
0048   /// Deallocate \a Ptr to \a Size bytes of memory allocated by this
0049   /// allocator.
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   /// Return allocator corresponding to the current thread.
0056   AllocatorTy &getThreadLocalAllocator() {
0057     assert(getThreadIndex() < NumOfAllocators);
0058     return Allocators[getThreadIndex()];
0059   }
0060 
0061   // Return number of used allocators.
0062   size_t getNumberOfAllocators() const { return NumOfAllocators; }
0063   /// @}
0064 
0065   /// \defgroup Methods which could not be called asynchronously:
0066   ///
0067   /// @{
0068 
0069   /// Reset state of allocators.
0070   void Reset() {
0071     for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0072       Allocators[Idx].Reset();
0073   }
0074 
0075   /// Return total memory size used by all allocators.
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   /// Return allocated size by all allocators.
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   /// Set red zone for all allocators.
0096   void setRedZoneSize(size_t NewSize) {
0097     for (size_t Idx = 0; Idx < getNumberOfAllocators(); Idx++)
0098       Allocators[Idx].setRedZoneSize(NewSize);
0099   }
0100 
0101   /// Print statistic for each allocator.
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 } // end namespace parallel
0118 } // end namespace llvm
0119 
0120 #endif // LLVM_SUPPORT_PERTHREADBUMPPTRALLOCATOR_H