Home | History | Annotate | Download | only in Support
      1 //===- Space.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_MEMORY_SPACE_H
     10 #define MCLD_MEMORY_SPACE_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 #include <llvm/Support/DataTypes.h>
     15 #include <mcld/ADT/TypeTraits.h>
     16 
     17 namespace mcld {
     18 
     19 class FileHandle;
     20 class MemoryRegion;
     21 
     22 /** \class Space
     23  *  \brief Space contains a chunk of memory space that does not overlap with
     24  *  the other Space.
     25  *
     26  */
     27 class Space
     28 {
     29 public:
     30   enum Type
     31   {
     32     ALLOCATED_ARRAY,
     33     MMAPED,
     34     EXTERNAL,
     35     UNALLOCATED
     36   };
     37 
     38   typedef NonConstTraits<uint8_t>::pointer Address;
     39   typedef ConstTraits<uint8_t>::pointer ConstAddress;
     40 
     41 private:
     42   Space();
     43 
     44   ~Space();
     45 
     46   Space(Type pType, void* pMemBuffer, size_t pSize);
     47 
     48 public:
     49   void setStart(size_t pOffset)
     50   { m_StartOffset = pOffset; }
     51 
     52   Address memory()
     53   { return m_Data; }
     54 
     55   ConstAddress memory() const
     56   { return m_Data; }
     57 
     58   size_t start() const
     59   { return m_StartOffset; }
     60 
     61   size_t size() const
     62   { return m_Size; }
     63 
     64   Type type() const
     65   { return m_Type; }
     66 
     67   void addRegion(MemoryRegion& pRegion)
     68   { ++m_RegionCount; }
     69 
     70   void removeRegion(MemoryRegion& pRegion)
     71   { --m_RegionCount; }
     72 
     73   size_t numOfRegions() const
     74   { return m_RegionCount; }
     75 
     76   /// Create - Create a Space from external memory
     77   static Space* Create(void* pMemBuffer, size_t pSize);
     78 
     79   /// Create - Create a Space from FileHandler
     80   static Space* Create(FileHandle& pHandler, size_t pOffset, size_t pSize);
     81 
     82   static void Destroy(Space*& pSpace);
     83 
     84   static void Release(Space* pSpace, FileHandle& pHandler);
     85 
     86   static void Sync(Space* pSpace, FileHandle& pHandler);
     87 
     88 private:
     89   Address m_Data;
     90   uint32_t m_StartOffset;
     91   uint32_t m_Size;
     92   uint16_t m_RegionCount;
     93   Type m_Type : 2;
     94 };
     95 
     96 } // namespace of mcld
     97 
     98 #endif
     99 
    100