Home | History | Annotate | Download | only in Support
      1 //===- MemoryRegion.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 LD_MEMORY_REGION_H
     10 #define LD_MEMORY_REGION_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 
     15 #include <mcld/ADT/Uncopyable.h>
     16 #include <mcld/ADT/SizeTraits.h>
     17 #include <mcld/ADT/TypeTraits.h>
     18 #include <mcld/Support/FileSystem.h>
     19 #include <mcld/Support/MemoryArea.h>
     20 #include <llvm/ADT/ilist.h>
     21 #include <llvm/ADT/StringRef.h>
     22 
     23 namespace mcld
     24 {
     25 
     26 /** \class MemoryRegion
     27  *  \brief MemoryRegion is a range of virtual memory which is mapped onto a
     28  *  range of files which is opened by MemoryArea.
     29  *
     30  *  MemoryArea maps a file onto virtual memory. Clients can get a range of
     31  *  mapped memory space by requesting a MemoryRegion from MemoryArea, and
     32  *  read/write the mapped file through the MemoryRegion.
     33  *
     34  *  When two different MemoryRegion may overlap memory space, race condition
     35  *  may occurs. Clients must call MemoryRegion::sync() explicit to tell the
     36  *  MemoryArea when to synchronize the virtual memory space with the mapped
     37  *  file.
     38  */
     39 class MemoryRegion : private Uncopyable
     40 {
     41 friend class RegionFactory;
     42 friend class MemoryArea;
     43 
     44 public:
     45 typedef NonConstTraits<mcld::sys::fs::detail::Address>::value_type Address;
     46 typedef ConstTraits<mcld::sys::fs::detail::Address>::value_type    ConstAddress;
     47 typedef NonConstTraits<mcld::sys::fs::detail::Offset>::value_type  Offset;
     48 typedef ConstTraits<mcld::sys::fs::detail::Offset>::value_type     ConstOffset;
     49 
     50 private:
     51   MemoryRegion(MemoryArea::Space* pParentSpace,
     52                const Address pVMAStart,
     53                size_t pSize);
     54 
     55   // drift - leave parent space
     56   void drift();
     57 
     58   MemoryArea::Space* parent()
     59   { return m_pParentSpace; }
     60 
     61   const MemoryArea::Space* parent() const
     62   { return m_pParentSpace; }
     63 
     64 public:
     65   ~MemoryRegion();
     66 
     67   Address start()
     68   { return m_VMAStart; }
     69 
     70   ConstAddress start() const
     71   { return m_VMAStart; }
     72 
     73   Address end()
     74   { return m_VMAStart+m_Length; }
     75 
     76   ConstAddress end() const
     77   { return m_VMAStart+m_Length; }
     78 
     79   size_t size() const
     80   { return m_Length; }
     81 
     82   Address getBuffer(Offset pOffset = 0)
     83   { return m_VMAStart+pOffset; }
     84 
     85   ConstAddress getBuffer(Offset pOffset = 0) const
     86   { return m_VMAStart+pOffset; }
     87 
     88 private:
     89   MemoryArea::Space* m_pParentSpace;
     90   Address m_VMAStart;
     91   size_t m_Length;
     92 };
     93 
     94 } // namespace of mcld
     95 
     96 #endif
     97 
     98