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/Config/Config.h>
     16 #include <mcld/ADT/Uncopyable.h>
     17 #include <mcld/Support/Allocators.h>
     18 #include <mcld/Support/Space.h>
     19 
     20 namespace mcld {
     21 
     22 class MemoryArea;
     23 
     24 /** \class MemoryRegion
     25  *  \brief MemoryRegion is a range of virtual memory which is mapped onto a
     26  *  range of files which is opened by MemoryArea.
     27  *
     28  *  MemoryArea maps a file onto virtual memory. Clients can get a range of
     29  *  mapped memory space by requesting a MemoryRegion from MemoryArea, and
     30  *  read/write the mapped file through the MemoryRegion.
     31  *
     32  *  When two different MemoryRegion may overlap memory space, race condition
     33  *  may occurs. Clients must call MemoryRegion::sync() explicit to tell the
     34  *  MemoryArea when to synchronize the virtual memory space with the mapped
     35  *  file.
     36  */
     37 class MemoryRegion : private Uncopyable
     38 {
     39 friend class Chunk<MemoryRegion, MCLD_REGION_CHUNK_SIZE>;
     40 friend class RegionFactory;
     41 friend class MemoryArea;
     42 
     43 public:
     44   typedef Space::Address Address;
     45   typedef Space::ConstAddress ConstAddress;
     46 
     47 private:
     48   MemoryRegion();
     49 
     50   MemoryRegion(const Address pVMAStart, size_t pSize);
     51 
     52   ~MemoryRegion();
     53 
     54   void setParent(Space& pSpace) { m_pParent = &pSpace; }
     55 
     56 public:
     57   /// Create - To wrap a piece of memory and to create a new region.
     58   /// This function wraps a piece of memory and to create a new region. Region
     59   /// is just a wraper, it is not responsible for deallocate the given memory.
     60   ///
     61   /// @param pStart [in] The start address of a piece of memory
     62   /// @param pSize  [in] The size of the given memory
     63   static MemoryRegion* Create(void* pStart, size_t pSize);
     64 
     65   /// Create - To wrap a piece of memory and to create a new region.
     66   /// This function wraps a piece of memory and to create a new region. Region
     67   /// is just a wraper, it is not responsible for deallocate the given memory.
     68   ///
     69   /// If a wrapped memory comes from a Space, then we say the space is the
     70   /// parent of the region. pSpace is a memory counting container. It remembers
     71   /// the number of regions in it. A space which has no region will be removed
     72   /// quickly.
     73   ///
     74   /// The wrapped memory will be deallocated by Space when the space has no
     75   /// region used it.
     76   ///
     77   /// @param pStart [in] The start address of a piece of memory
     78   /// @param pSize  [in] The size of the given memory
     79   /// @param pSpace [in] The parent space.
     80   static MemoryRegion* Create(void* pStart, size_t pSize, Space& pSpace);
     81 
     82   /// Destroy - To destroy the region
     83   /// If the region has a parent space, it will be also remove from the space.
     84   ///
     85   /// @param pRegion [in, out] pRegion is set to NULL if the destruction is
     86   /// success.
     87   static void Destroy(MemoryRegion*& pRegion);
     88 
     89   const Space* parent() const { return m_pParent; }
     90   Space*       parent()       { return m_pParent; }
     91 
     92   bool hasParent() const { return (NULL != m_pParent); }
     93 
     94   ConstAddress start() const { return m_VMAStart; }
     95   Address      start()       { return m_VMAStart; }
     96 
     97   ConstAddress end() const { return m_VMAStart+m_Length; }
     98   Address      end()       { return m_VMAStart+m_Length; }
     99 
    100   size_t size() const { return m_Length; }
    101 
    102   ConstAddress getBuffer(size_t pOffset = 0) const
    103   { return m_VMAStart+pOffset; }
    104 
    105   Address getBuffer(size_t pOffset = 0)
    106   { return m_VMAStart+pOffset; }
    107 
    108 private:
    109   Space* m_pParent;
    110   Address m_VMAStart;
    111   size_t m_Length;
    112 };
    113 
    114 } // namespace of mcld
    115 
    116 #endif
    117 
    118