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