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//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_SUPPORT_MEMORYAREAFACTORY_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_SUPPORT_MEMORYAREAFACTORY_H_
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/FileHandle.h"
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/GCFactory.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/MemoryArea.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/Path.h"
1537b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#include <llvm/ADT/StringMap.h>
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1837b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace mcld {
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 */
3837b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass MemoryAreaFactory : public GCFactory<MemoryArea, 0> {
3937b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  explicit MemoryAreaFactory(size_t pNum);
415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
42affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  virtual ~MemoryAreaFactory();
43affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // produce - create a MemoryArea and open its file.
4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  MemoryArea* produce(const sys::fs::Path& pPath, FileHandle::OpenMode pMode);
46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
47affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // produce - create a MemoryArea and open its file.
48affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  MemoryArea* produce(const sys::fs::Path& pPath,
49affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                      FileHandle::OpenMode pMode,
50affc150dc44fab1911775a49636d0ce85333b634Zonr Chang                      FileHandle::Permission pPerm);
51affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
52affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // Create a MemoryArea with an universal space.
53affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // The created MemoryArea is not moderated by m_HandleToArea.
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  MemoryArea* produce(void* pMemBuffer, size_t pSize);
55affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
56affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // Create a MemoryArea by the given file handler
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // The created MemoryArea is not moderated by m_HandleToArea.
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  MemoryArea* produce(int pFD, FileHandle::OpenMode pMode);
5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void destruct(MemoryArea* pArea);
6137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
6237b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
6387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  llvm::StringMap<MemoryArea*> m_AreaMap;
645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
6637b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
6837b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_SUPPORT_MEMORYAREAFACTORY_H_
69