2008-07-07 22:58:06 +00:00
|
|
|
//==- llvm/Support/Recycler.h - Recycling Allocator --------------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the Recycler class template. See the doxygen comment for
|
|
|
|
// Recycler for more details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_RECYCLER_H
|
|
|
|
#define LLVM_SUPPORT_RECYCLER_H
|
|
|
|
|
2008-07-28 21:51:04 +00:00
|
|
|
#include "llvm/ADT/ilist.h"
|
|
|
|
#include "llvm/Support/AlignOf.h"
|
2014-03-30 11:36:29 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2012-02-05 22:14:15 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-07-28 21:51:04 +00:00
|
|
|
#include <cassert>
|
2008-07-07 22:58:06 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
|
|
|
|
/// printing statistics.
|
|
|
|
///
|
2008-07-28 21:51:04 +00:00
|
|
|
void PrintRecyclerStats(size_t Size, size_t Align, size_t FreeListSize);
|
|
|
|
|
|
|
|
/// RecyclerStruct - Implementation detail for Recycler. This is a
|
|
|
|
/// class that the recycler imposes on free'd memory to carve out
|
|
|
|
/// next/prev pointers.
|
|
|
|
struct RecyclerStruct {
|
|
|
|
RecyclerStruct *Prev, *Next;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2009-09-06 08:55:57 +00:00
|
|
|
struct ilist_traits<RecyclerStruct> :
|
|
|
|
public ilist_default_traits<RecyclerStruct> {
|
2008-07-28 21:51:04 +00:00
|
|
|
static RecyclerStruct *getPrev(const RecyclerStruct *t) { return t->Prev; }
|
|
|
|
static RecyclerStruct *getNext(const RecyclerStruct *t) { return t->Next; }
|
|
|
|
static void setPrev(RecyclerStruct *t, RecyclerStruct *p) { t->Prev = p; }
|
|
|
|
static void setNext(RecyclerStruct *t, RecyclerStruct *n) { t->Next = n; }
|
|
|
|
|
|
|
|
mutable RecyclerStruct Sentinel;
|
|
|
|
RecyclerStruct *createSentinel() const {
|
|
|
|
return &Sentinel;
|
|
|
|
}
|
|
|
|
static void destroySentinel(RecyclerStruct *) {}
|
|
|
|
|
2009-03-04 20:36:44 +00:00
|
|
|
RecyclerStruct *provideInitialHead() const { return createSentinel(); }
|
|
|
|
RecyclerStruct *ensureHead(RecyclerStruct*) const { return createSentinel(); }
|
2009-03-04 21:54:31 +00:00
|
|
|
static void noteHead(RecyclerStruct*, RecyclerStruct*) {}
|
2009-03-04 20:36:44 +00:00
|
|
|
|
2008-07-28 21:51:04 +00:00
|
|
|
static void deleteNode(RecyclerStruct *) {
|
2012-02-05 22:14:15 +00:00
|
|
|
llvm_unreachable("Recycler's ilist_traits shouldn't see a deleteNode call!");
|
2008-07-28 21:51:04 +00:00
|
|
|
}
|
|
|
|
};
|
2008-07-07 22:58:06 +00:00
|
|
|
|
|
|
|
/// Recycler - This class manages a linked-list of deallocated nodes
|
|
|
|
/// and facilitates reusing deallocated memory in place of allocating
|
2008-07-28 21:51:04 +00:00
|
|
|
/// new memory.
|
2008-07-07 22:58:06 +00:00
|
|
|
///
|
2008-07-28 21:51:04 +00:00
|
|
|
template<class T, size_t Size = sizeof(T), size_t Align = AlignOf<T>::Alignment>
|
2008-07-07 22:58:06 +00:00
|
|
|
class Recycler {
|
|
|
|
/// FreeList - Doubly-linked list of nodes that have deleted contents and
|
|
|
|
/// are not in active use.
|
|
|
|
///
|
2008-07-28 21:51:04 +00:00
|
|
|
iplist<RecyclerStruct> FreeList;
|
2008-07-07 22:58:06 +00:00
|
|
|
|
|
|
|
public:
|
2008-07-28 21:51:04 +00:00
|
|
|
~Recycler() {
|
|
|
|
// If this fails, either the callee has lost track of some allocation,
|
|
|
|
// or the callee isn't tracking allocations and should just call
|
|
|
|
// clear() before deleting the Recycler.
|
|
|
|
assert(FreeList.empty() && "Non-empty recycler deleted!");
|
|
|
|
}
|
2008-07-07 22:58:06 +00:00
|
|
|
|
2008-07-28 21:51:04 +00:00
|
|
|
/// clear - Release all the tracked allocations to the allocator. The
|
|
|
|
/// recycler must be free of any tracked allocations before being
|
|
|
|
/// deleted; calling clear is one way to ensure this.
|
2008-07-07 22:58:06 +00:00
|
|
|
template<class AllocatorType>
|
|
|
|
void clear(AllocatorType &Allocator) {
|
2008-07-28 21:51:04 +00:00
|
|
|
while (!FreeList.empty()) {
|
|
|
|
T *t = reinterpret_cast<T *>(FreeList.remove(FreeList.begin()));
|
|
|
|
Allocator.Deallocate(t);
|
|
|
|
}
|
2008-07-07 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 22:35:45 +00:00
|
|
|
/// Special case for BumpPtrAllocator which has an empty Deallocate()
|
|
|
|
/// function.
|
|
|
|
///
|
|
|
|
/// There is no need to traverse the free list, pulling all the objects into
|
|
|
|
/// cache.
|
|
|
|
void clear(BumpPtrAllocator&) {
|
|
|
|
FreeList.clearAndLeakNodesUnsafely();
|
|
|
|
}
|
|
|
|
|
2008-07-07 22:58:06 +00:00
|
|
|
template<class SubClass, class AllocatorType>
|
|
|
|
SubClass *Allocate(AllocatorType &Allocator) {
|
2014-03-15 18:47:07 +00:00
|
|
|
static_assert(AlignOf<SubClass>::Alignment <= Align,
|
|
|
|
"Recycler allocation alignment is less than object align!");
|
|
|
|
static_assert(sizeof(SubClass) <= Size,
|
|
|
|
"Recycler allocation size is less than object size!");
|
2008-07-28 21:51:04 +00:00
|
|
|
return !FreeList.empty() ?
|
|
|
|
reinterpret_cast<SubClass *>(FreeList.remove(FreeList.begin())) :
|
|
|
|
static_cast<SubClass *>(Allocator.Allocate(Size, Align));
|
2008-07-07 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class AllocatorType>
|
|
|
|
T *Allocate(AllocatorType &Allocator) {
|
|
|
|
return Allocate<T>(Allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class SubClass, class AllocatorType>
|
2008-07-10 22:57:49 +00:00
|
|
|
void Deallocate(AllocatorType & /*Allocator*/, SubClass* Element) {
|
2008-07-28 21:51:04 +00:00
|
|
|
FreeList.push_front(reinterpret_cast<RecyclerStruct *>(Element));
|
2008-07-07 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintStats() {
|
2008-07-28 21:51:04 +00:00
|
|
|
PrintRecyclerStats(Size, Align, FreeList.size());
|
2008-07-07 22:58:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|