summaryrefslogtreecommitdiff
path: root/include/mcld/Support/MemoryAreaFactory.h
blob: c3c3a0d1a8717db5605ebd6522d52bf3f8089f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===- MemoryAreaFactory.h ------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_SUPPORT_MEMORYAREAFACTORY_H
#define MCLD_SUPPORT_MEMORYAREAFACTORY_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif
#include <mcld/Support/GCFactory.h>
#include <mcld/Support/MemoryArea.h>
#include <mcld/Support/Path.h>
#include <mcld/Support/FileHandle.h>
#include <llvm/ADT/StringMap.h>

namespace mcld
{

/** \class MemoryAreaFactory
 *  \brief MemoryAreaFactory avoids creating duplicated MemoryAreas of the
 *   same file.
 *
 *  Users can give duplicated input files on the command line. In order to
 *  prevent opening the same file twice, and create redundant MemoryRegions,
 *  mcld::Input should not create MemoryArea directly. Instead, it should ask
 *  MemoryAreaFactory and get the unique MemoryArea.
 *
 *  The timing of opening and closing files is not strictly bound to the
 *  constructor and destructor of MCLDFile. For mcld::Output, MCLinker
 *  opens the file rather after assigning offset to sections. On the other
 *  aside, mcld::Input opens the file at constructor. In order to hide the
 *  file operations, MemoryAreaFactory actually open the file untill the first
 *  MemoryRegion is requested.
 *
 *  @see MemoryRegion
 */
class MemoryAreaFactory : public GCFactory<MemoryArea, 0>
{
public:
  explicit MemoryAreaFactory(size_t pNum);

  virtual ~MemoryAreaFactory();

  // produce - create a MemoryArea and open its file.
  MemoryArea* produce(const sys::fs::Path& pPath,
                      FileHandle::OpenMode pMode);

  // produce - create a MemoryArea and open its file.
  MemoryArea* produce(const sys::fs::Path& pPath,
                      FileHandle::OpenMode pMode,
                      FileHandle::Permission pPerm);

  // Create a MemoryArea with an universal space.
  // The created MemoryArea is not moderated by m_HandleToArea.
  MemoryArea* produce(void* pMemBuffer, size_t pSize);

  // Create a MemoryArea by the given file handler
  // The created MemoryArea is not moderated by m_HandleToArea.
  MemoryArea* produce(int pFD, FileHandle::OpenMode pMode);

  void destruct(MemoryArea* pArea);
private:
  llvm::StringMap<MemoryArea*> m_AreaMap;
};

} // namespace of mcld

#endif