MemoryAreaFactory.h revision 5460a1f25d9ddecb5c70667267d66d51af177a99
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
15#include "mcld/Support/UniqueGCFactory.h"
16#include "mcld/Support/MemoryArea.h"
17#include "mcld/Support/Path.h"
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <fcntl.h>
21
22namespace mcld
23{
24
25class RegionFactory;
26/** \class MemoryAreaFactory
27 *  \brief MemoryAreaFactory avoids creating duplicated MemoryAreas of the
28 *   same file.
29 *
30 *  Users can give duplicated input files on the command line. In order to
31 *  prevent opening the same file twice, and create redundant MemoryRegions,
32 *  mcld::Input should not create MemoryArea directly. Instead, it should ask
33 *  MemoryAreaFactory and get the unique MemoryArea.
34 *
35 *  The timing of opening and closing files is not strictly bound to the
36 *  constructor and destructor of MCLDFile. For mcld::Output, MCLinker
37 *  opens the file rather after assigning offset to sections. On the other
38 *  aside, mcld::Input opens the file at constructor. In order to hide the
39 *  file operations, MemoryAreaFactory actually open the file untill the first
40 *  MemoryRegion is requested.
41 *
42 *  @see MemoryRegion
43 *  @see UniqueGCFactoryBase
44 */
45class MemoryAreaFactory : public UniqueGCFactoryBase<sys::fs::Path, MemoryArea, 0>
46{
47public:
48  explicit MemoryAreaFactory(size_t pNum);
49  ~MemoryAreaFactory();
50
51  // produce - create a MemoryArea and open its file
52  // If the file fails to be opened, the returned MemoryArea::isMapped()
53  // should be false
54  MemoryArea* produce(const sys::fs::Path& pPath, int pFlags);
55  MemoryArea* produce(const sys::fs::Path& pPath, int pFlags, mode_t pMode);
56
57private:
58  RegionFactory* m_pRegionFactory;
59};
60
61} // namespace of mcld
62
63#endif
64
65