15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- MemoryRegion.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//===----------------------------------------------------------------------===//
95460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#ifndef LD_MEMORY_REGION_H
105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#define LD_MEMORY_REGION_H
115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#ifdef ENABLE_UNITTEST
125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <gtest.h>
135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Config/Config.h>
165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <mcld/ADT/Uncopyable.h>
1722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Support/Allocators.h>
18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/Space.h>
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass MemoryArea;
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class MemoryRegion
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  \brief MemoryRegion is a range of virtual memory which is mapped onto a
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  range of files which is opened by MemoryArea.
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  MemoryArea maps a file onto virtual memory. Clients can get a range of
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  mapped memory space by requesting a MemoryRegion from MemoryArea, and
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  read/write the mapped file through the MemoryRegion.
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *
325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  When two different MemoryRegion may overlap memory space, race condition
335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  may occurs. Clients must call MemoryRegion::sync() explicit to tell the
345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  MemoryArea when to synchronize the virtual memory space with the mapped
355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao *  file.
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass MemoryRegion : private Uncopyable
385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
3922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaofriend class Chunk<MemoryRegion, MCLD_REGION_CHUNK_SIZE>;
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend class RegionFactory;
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend class MemoryArea;
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef Space::Address Address;
45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef Space::ConstAddress ConstAddress;
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
4822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  MemoryRegion();
495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  MemoryRegion(const Address pVMAStart, size_t pSize);
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ~MemoryRegion();
535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void setParent(Space& pSpace) { m_pParent = &pSpace; }
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaopublic:
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Create - To wrap a piece of memory and to create a new region.
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function wraps a piece of memory and to create a new region. Region
5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// is just a wraper, it is not responsible for deallocate the given memory.
6022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pStart [in] The start address of a piece of memory
6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pSize  [in] The size of the given memory
6322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static MemoryRegion* Create(void* pStart, size_t pSize);
6422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Create - To wrap a piece of memory and to create a new region.
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// This function wraps a piece of memory and to create a new region. Region
6722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// is just a wraper, it is not responsible for deallocate the given memory.
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
6922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// If a wrapped memory comes from a Space, then we say the space is the
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// parent of the region. pSpace is a memory counting container. It remembers
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// the number of regions in it. A space which has no region will be removed
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// quickly.
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
7422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// The wrapped memory will be deallocated by Space when the space has no
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// region used it.
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pStart [in] The start address of a piece of memory
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pSize  [in] The size of the given memory
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pSpace [in] The parent space.
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static MemoryRegion* Create(void* pStart, size_t pSize, Space& pSpace);
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// Destroy - To destroy the region
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// If the region has a parent space, it will be also remove from the space.
8422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ///
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// @param pRegion [in, out] pRegion is set to NULL if the destruction is
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// success.
8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  static void Destroy(MemoryRegion*& pRegion);
8822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const Space* parent() const { return m_pParent; }
9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Space*       parent()       { return m_pParent; }
9122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool hasParent() const { return (NULL != m_pParent); }
9322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ConstAddress start() const { return m_VMAStart; }
9522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Address      start()       { return m_VMAStart; }
9622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ConstAddress end() const { return m_VMAStart+m_Length; }
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Address      end()       { return m_VMAStart+m_Length; }
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  size_t size() const { return m_Length; }
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  ConstAddress getBuffer(size_t pOffset = 0) const
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { return m_VMAStart+pOffset; }
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
10522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Address getBuffer(size_t pOffset = 0)
10622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  { return m_VMAStart+pOffset; }
10722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Space* m_pParent;
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Address m_VMAStart;
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  size_t m_Length;
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
118