Add some basic Pool-allocation infrastructure. This adds a Recycler class,

for handling bookkeeping for deleted objects, as well as the alist class
template, for keeping lists of objects allocated from Recyclers, and some
related utilities.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53210 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2008-07-07 22:58:06 +00:00
parent 19d6d424aa
commit e14d81deeb
7 changed files with 589 additions and 3 deletions

292
include/llvm/ADT/alist.h Normal file
View File

@ -0,0 +1,292 @@
//==- llvm/ADT/alist.h - Linked lists with hooks -----------------*- 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 alist class template, and related infrastructure.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ADT_ALIST_H
#define LLVM_ADT_ALIST_H
#include <cassert>
#include "llvm/ADT/alist_node.h"
#include "llvm/ADT/STLExtras.h"
namespace llvm {
/// alist_iterator - An iterator class for alist.
///
template<class T, class LargestT = T, class ValueT = T,
class NodeIterT = ilist_iterator<alist_node<T, LargestT> > >
class alist_iterator : public bidirectional_iterator<ValueT, ptrdiff_t> {
typedef bidirectional_iterator<ValueT, ptrdiff_t> super;
typedef alist_node<T, LargestT> NodeTy;
/// NodeIter - The underlying iplist iterator that is being wrapped.
NodeIterT NodeIter;
public:
typedef size_t size_type;
typedef typename super::pointer pointer;
typedef typename super::reference reference;
alist_iterator(NodeIterT NI) : NodeIter(NI) {}
alist_iterator(pointer EP) : NodeIter(NodeTy::getNode(EP)) {}
alist_iterator() : NodeIter() {}
// This is templated so that we can allow constructing a const iterator from
// a nonconst iterator...
template<class V, class W, class X, class Y>
alist_iterator(const alist_iterator<V, W, X, Y> &RHS)
: NodeIter(RHS.getNodeIterUnchecked()) {}
// This is templated so that we can allow assigning to a const iterator from
// a nonconst iterator...
template<class V, class W, class X, class Y>
const alist_iterator &operator=(const alist_iterator<V, W, X, Y> &RHS) {
NodeIter = RHS.getNodeIterUnchecked();
return *this;
}
operator pointer() const { return NodeIter->getElement((T*)0); }
reference operator*() const { return *NodeIter->getElement((T*)0); }
pointer operator->() const { return &operator*(); }
bool operator==(const alist_iterator &RHS) const {
return NodeIter == RHS.NodeIter;
}
bool operator!=(const alist_iterator &RHS) const {
return NodeIter != RHS.NodeIter;
}
alist_iterator &operator--() {
--NodeIter;
return *this;
}
alist_iterator &operator++() {
++NodeIter;
return *this;
}
alist_iterator operator--(int) {
alist_iterator tmp = *this;
--*this;
return tmp;
}
alist_iterator operator++(int) {
alist_iterator tmp = *this;
++*this;
return tmp;
}
NodeIterT getNodeIterUnchecked() const { return NodeIter; }
};
// do not implement. this is to catch errors when people try to use
// them as random access iterators
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator-(int, alist_iterator<T, LargestT, ValueT, NodeIterT>);
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator-(alist_iterator<T, LargestT, ValueT, NodeIterT>,int);
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator+(int, alist_iterator<T, LargestT, ValueT, NodeIterT>);
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator+(alist_iterator<T, LargestT, ValueT, NodeIterT>,int);
// operator!=/operator== - Allow mixed comparisons without dereferencing
// the iterator, which could very likely be pointing to end().
template<class T, class V, class W, class X, class Y>
bool operator!=(T* LHS, const alist_iterator<V, W, X, Y> &RHS) {
return LHS != RHS.getNodeIterUnchecked().getNodePtrUnchecked()
->getElement((T*)0);
}
template<class T, class V, class W, class X, class Y>
bool operator==(T* LHS, const alist_iterator<V, W, X, Y> &RHS) {
return LHS == RHS.getNodeIterUnchecked().getNodePtrUnchecked()
->getElement((T*)0);
}
// Allow alist_iterators to convert into pointers to a node automatically when
// used by the dyn_cast, cast, isa mechanisms...
template<class From> struct simplify_type;
template<class V, class W, class X, class Y>
struct simplify_type<alist_iterator<V, W, X, Y> > {
typedef alist_node<V, W> NodeTy;
typedef NodeTy* SimpleType;
static SimpleType
getSimplifiedValue(const alist_iterator<V, W, X, Y> &Node) {
return &*Node;
}
};
template<class V, class W, class X, class Y>
struct simplify_type<const alist_iterator<V, W, X, Y> > {
typedef alist_node<V, W> NodeTy;
typedef NodeTy* SimpleType;
static SimpleType
getSimplifiedValue(const alist_iterator<V, W, X, Y> &Node) {
return &*Node;
}
};
/// Template traits for alist. By specializing this template class, you
/// can register custom actions to be run when a node is added to or removed
/// from an alist. A common use of this is to update parent pointers.
///
template<class T, class LargestT = T>
class alist_traits {
typedef alist_iterator<T, LargestT> iterator;
public:
void addNodeToList(T *) {}
void removeNodeFromList(T *) {}
void transferNodesFromList(alist_traits &, iterator, iterator) {}
void deleteNode(T *E) { delete alist_node<T, LargestT>::getNode(E); }
};
/// alist - This class is an ilist-style container that automatically
/// adds the next/prev pointers. It is designed to work in cooperation
/// with <llvm/Support/Recycler.h>.
///
template<class T, class LargestT = T>
class alist {
typedef alist_node<T, LargestT> NodeTy;
public:
typedef typename ilist<NodeTy>::size_type size_type;
private:
/// NodeListTraits - ilist traits for NodeList.
///
struct NodeListTraits : ilist_traits<alist_node<T, LargestT> > {
alist_traits<T, LargestT> UserTraits;
void addNodeToList(NodeTy *N) {
UserTraits.addNodeToList(N->getElement((T*)0));
}
void removeNodeFromList(NodeTy *N) {
UserTraits.removeNodeFromList(N->getElement((T*)0));
}
void transferNodesFromList(iplist<NodeTy, NodeListTraits> &L2,
ilist_iterator<NodeTy> first,
ilist_iterator<NodeTy> last) {
UserTraits.transferNodesFromList(L2.UserTraits,
iterator(first),
iterator(last));
}
};
/// NodeList - Doubly-linked list of nodes that have constructed
/// contents and may be in active use.
///
iplist<NodeTy, NodeListTraits> NodeList;
public:
~alist() { clear(); }
typedef alist_iterator<T, LargestT, T, ilist_iterator<NodeTy> >
iterator;
typedef alist_iterator<T, LargestT, const T, ilist_iterator<const NodeTy> >
const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
iterator begin() { return iterator(NodeList.begin()); }
iterator end() { return iterator(NodeList.end()); }
const_iterator begin() const { return const_iterator(NodeList.begin()); }
const_iterator end() const { return const_iterator(NodeList.end()); }
reverse_iterator rbegin() { return reverse_iterator(NodeList.rbegin()); }
reverse_iterator rend() { return reverse_iterator(NodeList.rend()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(NodeList.rbegin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(NodeList.rend());
}
typedef T& reference;
typedef const T& const_reference;
reference front() { return *NodeList.front().getElement((T*)0); }
reference back() { return *NodeList.back().getElement((T*)0); }
const_reference front() const { return *NodeList.front().getElement((T*)0); }
const_reference back() const { return *NodeList.back().getElement((T*)0); }
bool empty() const { return NodeList.empty(); }
size_type size() const { return NodeList.size(); }
void push_front(T *E) {
NodeTy *N = alist_node<T, LargestT>::getNode(E);
assert(N->getPrev() == 0);
assert(N->getNext() == 0);
NodeList.push_front(N);
}
void push_back(T *E) {
NodeTy *N = alist_node<T, LargestT>::getNode(E);
assert(N->getPrev() == 0);
assert(N->getNext() == 0);
NodeList.push_back(N);
}
iterator insert(iterator I, T *E) {
NodeTy *N = alist_node<T, LargestT>::getNode(E);
assert(N->getPrev() == 0);
assert(N->getNext() == 0);
return iterator(NodeList.insert(I.getNodeIterUnchecked(), N));
}
void splice(iterator where, alist &Other) {
NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList);
}
void splice(iterator where, alist &Other, iterator From) {
NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList,
From.getNodeIterUnchecked());
}
void splice(iterator where, alist &Other, iterator From,
iterator To) {
NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList,
From.getNodeIterUnchecked(), To.getNodeIterUnchecked());
}
void pop_front() {
erase(NodeList.begin());
}
void pop_back() {
erase(prior(NodeList.end()));
}
iterator erase(iterator I) {
iterator Next = next(I);
NodeTy *N = NodeList.remove(I.getNodeIterUnchecked());
NodeList.UserTraits.deleteNode(N->getElement((T*)0));
return Next;
}
iterator erase(iterator first, iterator last) {
while (first != last)
first = erase(first);
return last;
}
T *remove(T *E) {
NodeTy *N = alist_node<T, LargestT>::getNode(E);
return NodeList.remove(N)->getElement((T*)0);
}
void clear() {
while (!empty()) pop_front();
}
alist_traits<T, LargestT> &getTraits() {
return NodeList.UserTraits;
}
};
}
#endif

View File

@ -0,0 +1,126 @@
//==- llvm/ADT/alist_node.h - Next/Prev helper class for alist ---*- 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 alist_node class template, which is used by the alist
// class template to provide next/prev pointers for arbitrary objects.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ADT_ALIST_NODE_H
#define LLVM_ADT_ALIST_NODE_H
#include <cassert>
#include "llvm/ADT/ilist.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/DataTypes.h"
namespace llvm {
/// alist_node - This is a utility class used by alist. It holds prev and next
/// pointers for use with ilists, as well as storage for objects as large as
/// LargestT, that are in T's inheritance tree.
///
template<class T, class LargestT = T>
class alist_node {
alist_node *Prev, *Next;
public:
alist_node() : Prev(0), Next(0) {}
alist_node *getPrev() const { return Prev; }
alist_node *getNext() const { return Next; }
void setPrev(alist_node *N) { Prev = N; }
void setNext(alist_node *N) { Next = N; }
union {
char Bytes[sizeof(LargestT)];
long long L;
void *P;
} Storage;
template<class SubClass>
SubClass *getElement(SubClass *) {
assert(sizeof(SubClass) <= sizeof(LargestT));
assert(unsigned(AlignOf<SubClass>::Alignment) <=
unsigned(AlignOf<LargestT>::Alignment));
return reinterpret_cast<SubClass*>(&Storage.Bytes);
}
template<class SubClass>
const SubClass *getElement(SubClass *) const {
assert(sizeof(SubClass) <= sizeof(LargestT));
assert(unsigned(AlignOf<SubClass>::Alignment) <=
unsigned(AlignOf<LargestT>::Alignment));
return reinterpret_cast<const SubClass*>(&Storage.Bytes);
}
// This code essentially does offsetof, but actual offsetof hits an ICE in
// GCC 4.0 relating to offsetof being used inside a template.
static alist_node* getNode(T *D) {
return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
(uintptr_t)&getNull()->Storage);
}
static const alist_node* getNode(const T *D) {
return reinterpret_cast<alist_node*>(reinterpret_cast<char*>(D) -
(uintptr_t)&getNull()->Storage);
}
private:
static alist_node* getNull() { return 0; }
};
// A specialization of ilist_traits for alist_nodes.
template<class T, class LargestT>
class ilist_traits<alist_node<T, LargestT> > {
typedef alist_node<T, LargestT> NodeTy;
protected:
// Allocate a sentinel inside the traits class. This works
// because iplist carries an instance of the traits class.
NodeTy Sentinel;
public:
static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
NodeTy *createSentinel() const {
assert(Sentinel.getPrev() == 0);
assert(Sentinel.getNext() == 0);
return const_cast<NodeTy*>(&Sentinel);
}
void destroySentinel(NodeTy *N) {
assert(N == &Sentinel);
Sentinel.setPrev(0);
Sentinel.setNext(0);
}
void addNodeToList(NodeTy *N) {}
void removeNodeFromList(NodeTy *N) {}
void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
ilist_iterator<NodeTy> first,
ilist_iterator<NodeTy> last) {}
// Ideally we wouldn't implement this, but ilist's clear calls it,
// which is called from ilist's destructor. We won't ever call
// either of those with a non-empty list, but statically this
// method needs to exist.
void deleteNode(NodeTy *N) { assert(0); }
private:
static NodeTy *createNode(const NodeTy &V); // do not implement
};
}
#endif

View File

@ -60,6 +60,7 @@ struct ilist_traits {
static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); } static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); } static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
static void deleteNode(NodeTy *V) { delete V; }
static NodeTy *createSentinel() { return new NodeTy(); } static NodeTy *createSentinel() { return new NodeTy(); }
static void destroySentinel(NodeTy *N) { delete N; } static void destroySentinel(NodeTy *N) { delete N; }
@ -121,8 +122,7 @@ public:
assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!"); assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
return *NodePtr; return *NodePtr;
} }
pointer operator->() { return &operator*(); } pointer operator->() const { return &operator*(); }
const pointer operator->() const { return &operator*(); }
// Comparison operators // Comparison operators
bool operator==(const ilist_iterator &RHS) const { bool operator==(const ilist_iterator &RHS) const {
@ -380,7 +380,7 @@ public:
// erase - remove a node from the controlled sequence... and delete it. // erase - remove a node from the controlled sequence... and delete it.
iterator erase(iterator where) { iterator erase(iterator where) {
delete remove(where); deleteNode(remove(where));
return where; return where;
} }

View File

@ -0,0 +1,97 @@
//==- 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
#include <cassert>
#include "llvm/ADT/alist_node.h"
namespace llvm {
/// PrintRecyclingAllocatorStats - Helper for RecyclingAllocator for
/// printing statistics.
///
void PrintRecyclerStats(size_t LargestTypeSize, size_t FreeListSize);
/// Recycler - This class manages a linked-list of deallocated nodes
/// and facilitates reusing deallocated memory in place of allocating
/// new memory. The objects it allocates are stored in alist_node
/// containers, so they may be used in alists.
///
template<class T, class LargestT = T>
class Recycler {
typedef alist_node<T, LargestT> NodeTy;
/// FreeListTraits - ilist traits for FreeList.
///
struct FreeListTraits : ilist_traits<alist_node<T, LargestT> > {
NodeTy &getSentinel() { return this->Sentinel; }
};
/// FreeList - Doubly-linked list of nodes that have deleted contents and
/// are not in active use.
///
iplist<NodeTy, FreeListTraits> FreeList;
/// CreateNewNode - Allocate a new node object and initialize its
/// prev and next pointers to 0.
///
template<class AllocatorType>
NodeTy *CreateNewNode(AllocatorType &Allocator) {
// Note that we're calling new on the *node*, to initialize its
// Next/Prev pointers, not new on the end-user object.
return new (Allocator.Allocate<NodeTy>()) NodeTy();
}
public:
~Recycler() { assert(FreeList.empty()); }
template<class AllocatorType>
void clear(AllocatorType &Allocator) {
while (!FreeList.empty())
Allocator.Deallocate(FreeList.remove(FreeList.begin()));
}
template<class SubClass, class AllocatorType>
SubClass *Allocate(AllocatorType &Allocator) {
NodeTy *N = !FreeList.empty() ?
FreeList.remove(FreeList.front()) :
CreateNewNode(Allocator);
assert(N->getPrev() == 0);
assert(N->getNext() == 0);
return N->getElement((SubClass*)0);
}
template<class AllocatorType>
T *Allocate(AllocatorType &Allocator) {
return Allocate<T>(Allocator);
}
template<class SubClass, class AllocatorType>
void Deallocate(AllocatorType &Allocator, SubClass* Element) {
NodeTy *N = NodeTy::getNode(Element);
assert(N->getPrev() == 0);
assert(N->getNext() == 0);
FreeList.push_front(N);
}
void PrintStats() {
PrintRecyclerStats(sizeof(LargestT), FreeList.size());
}
};
}
#endif

View File

@ -0,0 +1,60 @@
//==- llvm/Support/RecyclingAllocator.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 RecyclingAllocator class. See the doxygen comment for
// RecyclingAllocator for more details on the implementation.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_RECYCLINGALLOCATOR_H
#define LLVM_SUPPORT_RECYCLINGALLOCATOR_H
#include <cassert>
#include "llvm/Support/Recycler.h"
#include "llvm/ADT/STLExtras.h"
namespace llvm {
/// RecyclingAllocator - This class wraps an Allocator, adding the
/// functionality of recycling deleted objects.
///
template<class AllocatorType, class T, class LargestT = T>
class RecyclingAllocator {
private:
/// Base - Implementation details.
///
Recycler<T, LargestT> Base;
/// Allocator - The wrapped allocator.
///
AllocatorType Allocator;
public:
~RecyclingAllocator() { Base.clear(Allocator); }
/// Allocate - Return a pointer to storage for an object of type
/// SubClass. The storage may be either newly allocated or recycled.
///
template<class SubClass>
SubClass *Allocate() { return Base.Allocate<SubClass>(Allocator); }
T *Allocate() { return Base.Allocate(Allocator); }
/// Deallocate - Release storage for the pointed-to object. The
/// storage will be kept track of and may be recycled.
///
template<class SubClass>
void Deallocate(SubClass* E) { return Base.Deallocate(Allocator, E); }
void PrintStats() { Base.PrintStats(); }
};
}
#endif

View File

@ -57,6 +57,10 @@ public:
return V->getNext(); return V->getNext();
} }
void deleteNode(ValueSubClass *V) {
delete V;
}
static void setPrev(ValueSubClass *V, ValueSubClass *P) { V->setPrev(P); } static void setPrev(ValueSubClass *V, ValueSubClass *P) { V->setPrev(P); }
static void setNext(ValueSubClass *V, ValueSubClass *N) { V->setNext(N); } static void setNext(ValueSubClass *V, ValueSubClass *N) { V->setNext(N); }

View File

@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
#include "llvm/Support/Recycler.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
#include <ostream> #include <ostream>
@ -130,3 +131,9 @@ void BumpPtrAllocator::PrintStats() const {
cerr << "\nNumber of memory regions: " << NumRegions << "\n"; cerr << "\nNumber of memory regions: " << NumRegions << "\n";
cerr << "Bytes allocated: " << BytesUsed << "\n"; cerr << "Bytes allocated: " << BytesUsed << "\n";
} }
void llvm::PrintRecyclerStats(size_t LargestTypeSize,
size_t FreeListSize) {
cerr << "Recycler element size: " << LargestTypeSize << '\n';
cerr << "Number of elements free for recycling: " << FreeListSize << '\n';
}