15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- MemoryAreaFactory.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//===----------------------------------------------------------------------===// 987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#ifndef MCLD_SUPPORT_MEMORYAREAFACTORY_H 1087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#define MCLD_SUPPORT_MEMORYAREAFACTORY_H 11affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/GCFactory.h> 12affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/MemoryArea.h> 13affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/Path.h> 14affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/FileHandle.h> 1587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#include <llvm/ADT/StringMap.h> 165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaonamespace mcld 185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{ 195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class MemoryAreaFactory 215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief MemoryAreaFactory avoids creating duplicated MemoryAreas of the 225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * same file. 235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 24551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines * Users can give duplicated input files on the command line. In order to 255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * prevent opening the same file twice, and create redundant MemoryRegions, 265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * mcld::Input should not create MemoryArea directly. Instead, it should ask 275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * MemoryAreaFactory and get the unique MemoryArea. 285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * The timing of opening and closing files is not strictly bound to the 305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * constructor and destructor of MCLDFile. For mcld::Output, MCLinker 315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * opens the file rather after assigning offset to sections. On the other 325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * aside, mcld::Input opens the file at constructor. In order to hide the 335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * file operations, MemoryAreaFactory actually open the file untill the first 345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * MemoryRegion is requested. 355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * @see MemoryRegion 375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 38affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass MemoryAreaFactory : public GCFactory<MemoryArea, 0> 395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{ 405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic: 415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao explicit MemoryAreaFactory(size_t pNum); 425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 43affc150dc44fab1911775a49636d0ce85333b634Zonr Chang virtual ~MemoryAreaFactory(); 44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // produce - create a MemoryArea and open its file. 46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang MemoryArea* produce(const sys::fs::Path& pPath, 47affc150dc44fab1911775a49636d0ce85333b634Zonr Chang FileHandle::OpenMode pMode); 48affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 49affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // produce - create a MemoryArea and open its file. 50affc150dc44fab1911775a49636d0ce85333b634Zonr Chang MemoryArea* produce(const sys::fs::Path& pPath, 51affc150dc44fab1911775a49636d0ce85333b634Zonr Chang FileHandle::OpenMode pMode, 52affc150dc44fab1911775a49636d0ce85333b634Zonr Chang FileHandle::Permission pPerm); 53affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 54affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // Create a MemoryArea with an universal space. 55affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // The created MemoryArea is not moderated by m_HandleToArea. 5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao MemoryArea* produce(void* pMemBuffer, size_t pSize); 57affc150dc44fab1911775a49636d0ce85333b634Zonr Chang 58affc150dc44fab1911775a49636d0ce85333b634Zonr Chang // Create a MemoryArea by the given file handler 5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao // The created MemoryArea is not moderated by m_HandleToArea. 6022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao MemoryArea* produce(int pFD, FileHandle::OpenMode pMode); 6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao 6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao void destruct(MemoryArea* pArea); 635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate: 6487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines llvm::StringMap<MemoryArea*> m_AreaMap; 655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld 685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif 70