Home | History | Annotate | Download | only in Fragment
      1 //===- FragmentRef.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_FRAGMENT_FRAGMENT_REFERENCE_H
     10 #define MCLD_FRAGMENT_FRAGMENT_REFERENCE_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 
     15 #include <mcld/Config/Config.h>
     16 #include <mcld/ADT/SizeTraits.h>
     17 #include <mcld/ADT/TypeTraits.h>
     18 #include <mcld/Support/Allocators.h>
     19 
     20 namespace mcld {
     21 
     22 class Fragment;
     23 class LDSection;
     24 class Layout;
     25 
     26 /** \class FragmentRef
     27  *  \brief FragmentRef is a reference of a Fragment's contetnt.
     28  *
     29  */
     30 class FragmentRef
     31 {
     32 public:
     33   typedef uint64_t Offset; // FIXME: use SizeTraits<T>::Offset
     34   typedef NonConstTraits<unsigned char>::pointer Address;
     35   typedef ConstTraits<unsigned char>::pointer ConstAddress;
     36 
     37 public:
     38   /// Create - create a fragment reference for a given fragment.
     39   ///
     40   /// @param pFrag - the given fragment
     41   /// @param pOffset - the offset, can be larger than the fragment, but can not
     42   ///                  be larger than the section size.
     43   /// @return if the offset is legal, return the fragment reference. Otherwise,
     44   /// return NULL.
     45   static FragmentRef* Create(Fragment& pFrag, uint64_t pOffset);
     46 
     47   static FragmentRef* Create(LDSection& pSection, uint64_t pOffset);
     48 
     49   /// Clear - clear all generated FragmentRef in the system.
     50   static void Clear();
     51 
     52   static FragmentRef* Null();
     53 
     54   // -----  modifiers  ----- //
     55   FragmentRef& assign(const FragmentRef& pCopy);
     56 
     57   FragmentRef& assign(Fragment& pFrag, Offset pOffset = 0);
     58 
     59   /// memcpy - copy memory
     60   /// copy memory from the fragment to the pDesc.
     61   /// @pDest - the destination address
     62   /// @pNBytes - copies pNBytes from the fragment[offset()+pOffset]
     63   /// @pOffset - additional offset.
     64   ///            the start address offset from fragment[offset()]
     65   void memcpy(void* pDest, size_t pNBytes, Offset pOffset = 0) const;
     66 
     67   // -----  observers  ----- //
     68   bool isNull() const { return (this == Null()); }
     69 
     70   Fragment* frag()
     71   { return m_pFragment; }
     72 
     73   const Fragment* frag() const
     74   { return m_pFragment; }
     75 
     76   Offset offset() const
     77   { return m_Offset; }
     78 
     79   Offset getOutputOffset() const;
     80 
     81   // -----  dereference  ----- //
     82   Address deref();
     83 
     84   ConstAddress deref() const;
     85 
     86   Address operator*()
     87   { return deref(); }
     88 
     89   ConstAddress operator*() const
     90   { return deref(); }
     91 
     92 private:
     93   friend FragmentRef& NullFragmentRef();
     94   friend class Chunk<FragmentRef, MCLD_SECTIONS_PER_INPUT>;
     95   friend class Relocation;
     96 
     97   FragmentRef();
     98 
     99   FragmentRef(Fragment& pFrag, Offset pOffset = 0);
    100 
    101 private:
    102   Fragment* m_pFragment;
    103   Offset m_Offset;
    104 
    105   static FragmentRef g_NullFragmentRef;
    106 
    107 };
    108 
    109 } // namespace of mcld
    110 
    111 #endif
    112 
    113