GCFactoryListTraits.h revision d0fbbb227051be16931a1aa9b4a7722ac039c698
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_GC_FACTORY_LIST_TRAITS_H
10#define MCLD_GC_FACTORY_LIST_TRAITS_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <llvm/ADT/ilist_node.h>
16#include <llvm/ADT/ilist.h>
17
18#include <assert.h>
19
20namespace mcld {
21
22/** \class GCFactoryListTraits
23 *  \brief GCFactoryListTraits provides trait class for llvm::iplist when
24 *  the nodes in the list is produced by GCFactory.
25 */
26template<typename DataType>
27class GCFactoryListTraits : public llvm::ilist_default_traits<DataType>
28{
29private:
30  class SentinelNode : public DataType
31  {
32  public:
33    SentinelNode() { }
34  };
35
36public:
37  // override the traits provided in llvm::ilist_sentinel_traits since we've
38  // defined our own sentinel.
39  DataType *createSentinel() const
40  { return reinterpret_cast<DataType*>(&mSentinel); }
41
42  static void destroySentinel(DataType*) { }
43
44  DataType *provideInitialHead() const
45  { return createSentinel(); }
46
47  DataType *ensureHead(DataType*) const
48  { return createSentinel(); }
49
50  static void noteHead(DataType*, DataType*) { }
51
52  // override the traits provided in llvm::ilist_node_traits since
53  static DataType *createNode(const DataType &V) {
54    assert(false && "Only GCFactory knows how to create a node.");
55  }
56  static void deleteNode(DataType *V) {
57    // No action. GCFactory will handle it by itself.
58  }
59
60private:
61  mutable SentinelNode mSentinel;
62};
63
64} // namespace of mcld
65
66#endif
67