15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- TreeAllocator.h ----------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_ADT_TREEALLOCATOR_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_ADT_TREEALLOCATOR_H_
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/ADT/TreeBase.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/GCFactory.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <set>
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
176f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesnamespace mcld {
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class NodeFactory
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief NodeFactory manages the creation and destruction of mcld::Node.
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactory guarantees all allocated memory are released finally. When
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  the destructor of NodeFactory is called, all allocated memory are freed.
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactory provides delegation of memory. Sometimes, we have to merge two
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactories, and NodeFactory::delegate() can move the memory from one
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactories to another.
28551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines *
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  @see LinearAllocator
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
3137b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <typename DataType>
3237b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass NodeFactory : public GCFactory<Node<DataType>, 64> {
3337b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef GCFactory<Node<DataType>, 64> Alloc;
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3637b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
3737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef Node<DataType> NodeType;
3837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef typename Alloc::iterator iterator;
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef typename Alloc::const_iterator const_iterator;
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
4137b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// produce - produce a node, add it under control
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  NodeType* produce() {
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    NodeType* result = Alloc::allocate();
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::construct(result);
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return result;
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// delegate - get the control of chunks owned by the client
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  after calling delegate(), client will renouce its control
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  of memory space.
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void delegate(NodeFactory& pClient) {
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (this == &pClient)
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pClient.empty())
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (Alloc::empty()) {
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      replace(pClient);
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      pClient.renounce();
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    }
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // neither me nor client is empty
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    concatenate(pClient);
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    pClient.renounce();
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
7037b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// renounce - give up the control of all chunks
7237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  void renounce() { Alloc::reset(); }
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// replace - be the agent of client.
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void replace(NodeFactory& pClient) {
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pRoot = pClient.Alloc::m_pRoot;
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent = pClient.Alloc::m_pCurrent;
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_AllocatedNum = pClient.Alloc::m_AllocatedNum;
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_NumAllocData = pClient.Alloc::m_NumAllocData;
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// concatenate - conncet two factories
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void concatenate(NodeFactory& pClient) {
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent->next = pClient.Alloc::m_pRoot;
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent = pClient.Alloc::m_pCurrent;
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_AllocatedNum += pClient.Alloc::m_AllocatedNum;
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_NumAllocData += pClient.Alloc::m_NumAllocData;
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
9137b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
9337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_ADT_TREEALLOCATOR_H_
94