• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MemoryArea.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_AREA_H
10 #define MCLD_MEMORY_AREA_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 
15 #include <mcld/ADT/Uncopyable.h>
16 #include <mcld/Support/Path.h>
17 #include <mcld/Support/FileSystem.h>
18 #include <mcld/Support/FileHandle.h>
19 #include <mcld/Support/Space.h>
20 #include <fcntl.h>
21 #include <string>
22 #include <list>
23 
24 
25 #if defined(ENABLE_UNITTEST)
26 namespace mcldtest
27 {
28   class MemoryAreaTest;
29 } // namespace of mcldtest
30 #endif
31 
32 namespace mcld
33 {
34 
35 class MemoryRegion;
36 class RegionFactory;
37 
38 /** \class MemoryArea
39  *  \brief MemoryArea is used to manage distinct MemoryRegions of address space.
40  *
41  *  Good linkers must well manipulate memory mapped I/O and dynamic memory.
42  *  In MCLinker, MemoryArea is the decision-maker to use memory mapped I/O or
43  *  dynamic memory. When a client requests MemoryArea for a piece of memory
44  *  to hold a part of a file, MemoryArea is going to see whether the requested
45  *  part of the file is already in any existing memory which is requested
46  *  before. If it is, MemoryArea creates a new MemoryRegion within the memory
47  *  requested before. Otherwise, MemoryArea uses memory mapped I/O or dynamic
48  *  memory to load the file.
49  *
50  *  If the part a file being loaded is larger than 3/4 pages, MemoryArea uses
51  *  memory mapped I/O to load the file. Otherwise, MemoryArea uses dynamic
52  *  memory to read the content of file into the memory space.
53  */
54 class MemoryArea : private Uncopyable
55 {
56   friend class MemoryAreaFactory;
57 public:
58   typedef llvm::iplist<Space> SpaceList;
59 
60 public:
61   // constructor by file handler.
62   // If the given file handler is read-only, client can not request a region
63   // that out of the file size.
64   // @param pFileHandle - file handler
65   MemoryArea(RegionFactory& pRegionFactory, FileHandle& pFileHandle);
66 
67   // constructor by set universal space.
68   // Client can not request a region that out of the universal space.
69   // @param pUniverse - file handler
70   MemoryArea(RegionFactory& pRegionFactory, Space& pUniverse);
71 
72   // destructor
73   ~MemoryArea();
74 
75   // request - create a MemoryRegion within a sufficient space
76   // find an existing space to hold the MemoryRegion.
77   // if MemoryArea does not find such space, then it creates a new space and
78   // assign a MemoryRegion into the space.
79   MemoryRegion* request(size_t pOffset, size_t pLength);
80 
81   // release - release a MemoryRegion.
82   // release a MemoryRegion does not cause
83   void release(MemoryRegion* pRegion);
84 
85   // clear - release all memory regions.
86   void clear();
87 
handler()88   FileHandle* handler()
89   { return m_pFileHandle; }
90 
handler()91   const FileHandle* handler() const
92   { return m_pFileHandle; }
93 
94   // -----  space list methods  ----- //
95   Space* find(size_t pOffset, size_t pLength);
96 
97   const Space* find(size_t pOffset, size_t pLength) const;
98 
99 private:
100   RegionFactory& m_RegionFactory;
101   SpaceList m_SpaceList;
102   FileHandle* m_pFileHandle;
103 };
104 
105 } // namespace of mcld
106 
107 #endif
108 
109