MemoryArea.h revision affc150dc44fab1911775a49636d0ce85333b634
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) 26namespace mcldtest 27{ 28 class MemoryAreaTest; 29} // namespace of mcldtest 30#endif 31 32namespace mcld 33{ 34 35class MemoryRegion; 36class 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 */ 54class MemoryArea : private Uncopyable 55{ 56public: 57 // constructor 58 MemoryArea(RegionFactory& pRegionFactory, 59 FileHandle& pFileHandle); 60 61 // destructor 62 ~MemoryArea(); 63 64 // request - create a MemoryRegion within a sufficient space 65 // find an existing space to hold the MemoryRegion. 66 // if MemoryArea does not find such space, then it creates a new space and 67 // assign a MemoryRegion into the space. 68 MemoryRegion* request(size_t pOffset, size_t pLength); 69 70 // release - release a MemoryRegion. 71 // release a MemoryRegion does not cause 72 void release(MemoryRegion* pRegion); 73 74 // clear - release all memory regions. 75 void clear(); 76 77 FileHandle* handler() 78 { return m_pFileHandle; } 79 80 const FileHandle* handler() const 81 { return m_pFileHandle; } 82 83private: 84 friend class MemoryAreaFactory; 85 86#if defined(ENABLE_UNITTEST) 87 friend class mcldtest::MemoryAreaTest; 88#endif 89 90 typedef llvm::iplist<Space> SpaceList; 91 92private: 93 // ----- special methods ----- // 94 // @param pRegionFactory The factory of regions. 95 // @param pUniverse A initial univeral space. 96 MemoryArea(RegionFactory& pRegionFactory, Space& pUniverse); 97 98 // ----- space list methods ----- // 99 Space* find(size_t pOffset, size_t pLength); 100 101 const Space* find(size_t pOffset, size_t pLength) const; 102 103private: 104 RegionFactory& m_RegionFactory; 105 SpaceList m_SpaceList; 106 FileHandle* m_pFileHandle; 107}; 108 109} // namespace of mcld 110 111#endif 112 113