1 //==- llvm/Support/RecyclingAllocator.h - Recycling Allocator ----*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the RecyclingAllocator class. See the doxygen comment for
10 // RecyclingAllocator for more details on the implementation.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
15 #define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
17 #include "llvm/Support/Recycler.h"
21 /// RecyclingAllocator - This class wraps an Allocator, adding the
22 /// functionality of recycling deleted objects.
24 template <class AllocatorType
, class T
, size_t Size
= sizeof(T
),
25 size_t Align
= alignof(T
)>
26 class RecyclingAllocator
{
28 /// Base - Implementation details.
30 Recycler
<T
, Size
, Align
> Base
;
32 /// Allocator - The wrapped allocator.
34 AllocatorType Allocator
;
37 ~RecyclingAllocator() { Base
.clear(Allocator
); }
39 /// Allocate - Return a pointer to storage for an object of type
40 /// SubClass. The storage may be either newly allocated or recycled.
42 template<class SubClass
>
43 SubClass
*Allocate() { return Base
.template Allocate
<SubClass
>(Allocator
); }
45 T
*Allocate() { return Base
.Allocate(Allocator
); }
47 /// Deallocate - Release storage for the pointed-to object. The
48 /// storage will be kept track of and may be recycled.
50 template<class SubClass
>
51 void Deallocate(SubClass
* E
) { return Base
.Deallocate(Allocator
, E
); }
54 Allocator
.PrintStats();
61 template<class AllocatorType
, class T
, size_t Size
, size_t Align
>
62 inline void *operator new(size_t size
,
63 llvm::RecyclingAllocator
<AllocatorType
,
64 T
, Size
, Align
> &Allocator
) {
65 assert(size
<= Size
&& "allocation size exceeded");
66 return Allocator
.Allocate();
69 template<class AllocatorType
, class T
, size_t Size
, size_t Align
>
70 inline void operator delete(void *E
,
71 llvm::RecyclingAllocator
<AllocatorType
,