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