MemoryAreaFactory.h revision 551ae4ebd3e9d137ea668fb83ae4a55b8cfba451
1//===- MemoryAreaFactory.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_SUPPORT_MEMORYAREAFACTORY_H
10#define MCLD_SUPPORT_MEMORYAREAFACTORY_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14#include <mcld/Support/GCFactory.h>
15#include <mcld/Support/MemoryArea.h>
16#include <mcld/Support/Path.h>
17#include <mcld/Support/FileHandle.h>
18#include <llvm/ADT/StringMap.h>
19
20namespace mcld
21{
22
23/** \class MemoryAreaFactory
24 *  \brief MemoryAreaFactory avoids creating duplicated MemoryAreas of the
25 *   same file.
26 *
27 *  Users can give duplicated input files on the command line. In order to
28 *  prevent opening the same file twice, and create redundant MemoryRegions,
29 *  mcld::Input should not create MemoryArea directly. Instead, it should ask
30 *  MemoryAreaFactory and get the unique MemoryArea.
31 *
32 *  The timing of opening and closing files is not strictly bound to the
33 *  constructor and destructor of MCLDFile. For mcld::Output, MCLinker
34 *  opens the file rather after assigning offset to sections. On the other
35 *  aside, mcld::Input opens the file at constructor. In order to hide the
36 *  file operations, MemoryAreaFactory actually open the file untill the first
37 *  MemoryRegion is requested.
38 *
39 *  @see MemoryRegion
40 */
41class MemoryAreaFactory : public GCFactory<MemoryArea, 0>
42{
43public:
44  explicit MemoryAreaFactory(size_t pNum);
45
46  virtual ~MemoryAreaFactory();
47
48  // produce - create a MemoryArea and open its file.
49  MemoryArea* produce(const sys::fs::Path& pPath,
50                      FileHandle::OpenMode pMode);
51
52  // produce - create a MemoryArea and open its file.
53  MemoryArea* produce(const sys::fs::Path& pPath,
54                      FileHandle::OpenMode pMode,
55                      FileHandle::Permission pPerm);
56
57  // Create a MemoryArea with an universal space.
58  // The created MemoryArea is not moderated by m_HandleToArea.
59  MemoryArea* produce(void* pMemBuffer, size_t pSize);
60
61  // Create a MemoryArea by the given file handler
62  // The created MemoryArea is not moderated by m_HandleToArea.
63  MemoryArea* produce(int pFD, FileHandle::OpenMode pMode);
64
65  void destruct(MemoryArea* pArea);
66private:
67  llvm::StringMap<MemoryArea*> m_AreaMap;
68};
69
70} // namespace of mcld
71
72#endif
73