15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- MemoryArea.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 MCLD_MEMORY_AREA_H 105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#define MCLD_MEMORY_AREA_H 115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#ifdef ENABLE_UNITTEST 125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <gtest.h> 135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif 145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 15affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/ADT/Uncopyable.h> 16affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/Path.h> 17affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/FileSystem.h> 18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/FileHandle.h> 19affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/Space.h> 205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <fcntl.h> 215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <string> 225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <list> 235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 24affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#if defined(ENABLE_UNITTEST) 265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcldtest 275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{ 285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao class MemoryAreaTest; 295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcldtest 305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif 31affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld 335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{ 345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass MemoryRegion; 365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass RegionFactory; 375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class MemoryArea 395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief MemoryArea is used to manage distinct MemoryRegions of address space. 405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Good linkers must well manipulate memory mapped I/O and dynamic memory. 425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * In MCLinker, MemoryArea is the decision-maker to use memory mapped I/O or 435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * dynamic memory. When a client requests MemoryArea for a piece of memory 445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * to hold a part of a file, MemoryArea is going to see whether the requested 455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * part of the file is already in any existing memory which is requested 465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * before. If it is, MemoryArea creates a new MemoryRegion within the memory 475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * requested before. Otherwise, MemoryArea uses memory mapped I/O or dynamic 485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * memory to load the file. 495460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * If the part a file being loaded is larger than 3/4 pages, MemoryArea uses 515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * memory mapped I/O to load the file. Otherwise, MemoryArea uses dynamic 525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * memory to read the content of file into the memory space. 535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoclass MemoryArea : private Uncopyable 555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{ 5667e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao friend class MemoryAreaFactory; 5767e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liaopublic: 5867e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao typedef llvm::iplist<Space> SpaceList; 5967e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao 605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic: 6167e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // constructor by file handler. 6267e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // If the given file handler is read-only, client can not request a region 6367e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // that out of the file size. 6467e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // @param pFileHandle - file handler 6567e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao MemoryArea(RegionFactory& pRegionFactory, FileHandle& pFileHandle); 6667e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao 6767e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // constructor by set universal space. 6867e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // Client can not request a region that out of the universal space. 6967e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao // @param pUniverse - file handler 7067e37f1be98c926645219cfb47fab9e90d8c725cShih-wei Liao MemoryArea(RegionFactory& pRegionFactory, Space& pUniverse); 715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // destructor 735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao ~MemoryArea(); 745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // request - create a MemoryRegion within a sufficient space 765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // find an existing space to hold the MemoryRegion. 775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // if MemoryArea does not find such space, then it creates a new space and 785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // assign a MemoryRegion into the space. 795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao MemoryRegion* request(size_t pOffset, size_t pLength); 805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // release - release a MemoryRegion. 825460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // release a MemoryRegion does not cause 835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void release(MemoryRegion* pRegion); 845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 85affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // clear - release all memory regions. 86affc150dc44fab1911775a49636d0ce85333b634Zonr Chang void clear(); 875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 88affc150dc44fab1911775a49636d0ce85333b634Zonr Chang FileHandle* handler() 89affc150dc44fab1911775a49636d0ce85333b634Zonr Chang { return m_pFileHandle; } 905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 91affc150dc44fab1911775a49636d0ce85333b634Zonr Chang const FileHandle* handler() const 92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang { return m_pFileHandle; } 935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 94affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // ----- space list methods ----- // 955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao Space* find(size_t pOffset, size_t pLength); 965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 97affc150dc44fab1911775a49636d0ce85333b634Zonr Chang const Space* find(size_t pOffset, size_t pLength) const; 985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate: 1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao RegionFactory& m_RegionFactory; 1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao SpaceList m_SpaceList; 102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang FileHandle* m_pFileHandle; 1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld 1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif 1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 109