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 <llvm/ADT/ilist.h>
16#include <llvm/ADT/ilist_node.h>
17#include <mcld/ADT/TypeTraits.h>
18
19namespace mcld
20{
21
22class FileHandle;
23class MemoryRegion;
24
25/** \class Space
26 *  \brief Space contains a chunk of memory space that does not overlap with
27 *  the other Space.
28 *
29 */
30class Space : public llvm::ilist_node<Space>
31{
32public:
33  enum Type
34  {
35    ALLOCATED_ARRAY,
36    MMAPED,
37    EXTERNAL,
38    UNALLOCATED
39  };
40
41  typedef NonConstTraits<uint8_t>::pointer Address;
42  typedef ConstTraits<uint8_t>::pointer ConstAddress;
43
44// llvm::iplist functions
45public:
46  // llvm::iplist needs default constructor to make a sentinel.
47  // Normal users should use @ref Space::createSpace function.
48  Space();
49
50  // llvm::iplist needs public destructor to delete the sentinel.
51  // Normal users should use @ref Space::releaseSpace function.
52  ~Space();
53
54  // This constructor is opened for the clients who want to control the
55  // details. In MCLinker, this constructor is used no where.
56  Space(Type pType, void* pMemBuffer, size_t pSize);
57
58public:
59  void setStart(size_t pOffset)
60  { m_StartOffset = pOffset; }
61
62  Address memory()
63  { return m_Data; }
64
65  ConstAddress memory() const
66  { return m_Data; }
67
68  size_t start() const
69  { return m_StartOffset; }
70
71  size_t size() const
72  { return m_Size; }
73
74  Type type() const
75  { return m_Type; }
76
77  void addRegion(MemoryRegion& pRegion)
78  { ++m_RegionCount; }
79
80  void removeRegion(MemoryRegion& pRegion)
81  { --m_RegionCount; }
82
83  size_t numOfRegions() const
84  { return m_RegionCount; }
85
86  static Space* createSpace(FileHandle& pHandler,
87                            size_t pOffset, size_t pSize);
88
89  static void releaseSpace(Space* pSpace, FileHandle& pHandler);
90
91  static void syncSpace(Space* pSpace, FileHandle& pHandler);
92
93private:
94  Address m_Data;
95  uint32_t m_StartOffset;
96  uint32_t m_Size;
97  uint16_t m_RegionCount;
98  Type m_Type : 2;
99};
100
101} // namespace of mcld
102
103#endif
104
105