GCFactoryListTraits.h revision 533eae20118036f425f27bf0536ef0ccbb090b65
1//===- GCFactoryListTraits.h ----------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_SUPPORT_GCFACTORYLISTTRAITS_H
10#define MCLD_SUPPORT_GCFACTORYLISTTRAITS_H
11
12#include <llvm/ADT/ilist_node.h>
13#include <llvm/ADT/ilist.h>
14
15#include <assert.h>
16
17namespace mcld {
18
19/** \class GCFactoryListTraits
20 *  \brief GCFactoryListTraits provides trait class for llvm::iplist when
21 *  the nodes in the list is produced by GCFactory.
22 */
23template<typename DataType>
24class GCFactoryListTraits : public llvm::ilist_default_traits<DataType>
25{
26private:
27  class SentinelNode : public DataType
28  {
29  public:
30    SentinelNode() { }
31  };
32
33public:
34  // override the traits provided in llvm::ilist_sentinel_traits since we've
35  // defined our own sentinel.
36  DataType *createSentinel() const
37  { return reinterpret_cast<DataType*>(&mSentinel); }
38
39  static void destroySentinel(DataType*) { }
40
41  DataType *provideInitialHead() const
42  { return createSentinel(); }
43
44  DataType *ensureHead(DataType*) const
45  { return createSentinel(); }
46
47  static void noteHead(DataType*, DataType*) { }
48
49  // override the traits provided in llvm::ilist_node_traits since
50  static DataType *createNode(const DataType &V) {
51    assert(false && "Only GCFactory knows how to create a node.");
52  }
53  static void deleteNode(DataType *V) {
54    // No action. GCFactory will handle it by itself.
55  }
56
57private:
58  mutable SentinelNode mSentinel;
59};
60
61} // namespace of mcld
62
63#endif
64