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/FileHandle.h"
12 #include "mcld/Support/GCFactory.h"
13 #include "mcld/Support/MemoryArea.h"
14 #include "mcld/Support/Path.h"
15 
16 #include <llvm/ADT/StringMap.h>
17 
18 namespace mcld {
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  */
38 class MemoryAreaFactory : public GCFactory<MemoryArea, 0> {
39  public:
40   explicit MemoryAreaFactory(size_t pNum);
41 
42   virtual ~MemoryAreaFactory();
43 
44   // produce - create a MemoryArea and open its file.
45   MemoryArea* produce(const sys::fs::Path& pPath, FileHandle::OpenMode pMode);
46 
47   // produce - create a MemoryArea and open its file.
48   MemoryArea* produce(const sys::fs::Path& pPath,
49                       FileHandle::OpenMode pMode,
50                       FileHandle::Permission pPerm);
51 
52   // Create a MemoryArea with an universal space.
53   // The created MemoryArea is not moderated by m_HandleToArea.
54   MemoryArea* produce(void* pMemBuffer, size_t pSize);
55 
56   // Create a MemoryArea by the given file handler
57   // The created MemoryArea is not moderated by m_HandleToArea.
58   MemoryArea* produce(int pFD, FileHandle::OpenMode pMode);
59 
60   void destruct(MemoryArea* pArea);
61 
62  private:
63   llvm::StringMap<MemoryArea*> m_AreaMap;
64 };
65 
66 }  // namespace mcld
67 
68 #endif  // MCLD_SUPPORT_MEMORYAREAFACTORY_H_
69