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//===----------------------------------------------------------------------===//
987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#ifndef MCLD_ADT_TREEALLOCATOR_H
1087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#define MCLD_ADT_TREEALLOCATOR_H
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <set>
1287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#include <mcld/Support/GCFactory.h>
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
146f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hinesnamespace mcld {
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class NodeFactory
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief NodeFactory manages the creation and destruction of mcld::Node.
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactory guarantees all allocated memory are released finally. When
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  the destructor of NodeFactory is called, all allocated memory are freed.
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactory provides delegation of memory. Sometimes, we have to merge two
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactories, and NodeFactory::delegate() can move the memory from one
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  NodeFactories to another.
25551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines *
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  @see LinearAllocator
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaotemplate<typename DataType>
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass NodeFactory : public GCFactory<Node<DataType>, 64>
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef GCFactory<Node<DataType>, 64> Alloc;
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef Node<DataType>                 NodeType;
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef typename Alloc::iterator       iterator;
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef typename Alloc::const_iterator const_iterator;
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// produce - produce a node, add it under control
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  NodeType* produce() {
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    NodeType* result = Alloc::allocate();
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::construct(result);
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return result;
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// delegate - get the control of chunks owned by the client
485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  after calling delegate(), client will renouce its control
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  //  of memory space.
505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void delegate(NodeFactory& pClient) {
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (this == &pClient)
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pClient.empty())
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (Alloc::empty()) {
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      replace(pClient);
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      pClient.renounce();
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return;
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    }
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    // neither me nor client is empty
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    concatenate(pClient);
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    pClient.renounce();
665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// renounce - give up the control of all chunks
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void renounce()
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { Alloc::reset(); }
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// replace - be the agent of client.
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void replace(NodeFactory& pClient) {
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pRoot = pClient.Alloc::m_pRoot;
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent = pClient.Alloc::m_pCurrent;
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_AllocatedNum = pClient.Alloc::m_AllocatedNum;
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_NumAllocData = pClient.Alloc::m_NumAllocData;
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// concatenate - conncet two factories
825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void concatenate(NodeFactory& pClient) {
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent->next = pClient.Alloc::m_pRoot;
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_pCurrent = pClient.Alloc::m_pCurrent;
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_AllocatedNum += pClient.Alloc::m_AllocatedNum;
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Alloc::m_NumAllocData += pClient.Alloc::m_NumAllocData;
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
93affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
94