1affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//===- HandleToArea.h -----------------------------------------------------===//
2affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//
3affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//                     The MCLinker Project
4affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//
5affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// This file is distributed under the University of Illinois Open Source
6affc150dc44fab1911775a49636d0ce85333b634Zonr Chang// License. See LICENSE.TXT for details.
7affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//
8affc150dc44fab1911775a49636d0ce85333b634Zonr Chang//===----------------------------------------------------------------------===//
9affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#ifndef MCLD_FILE_HANDLE_TO_MEMORY_AREA_H
10affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#define MCLD_FILE_HANDLE_TO_MEMORY_AREA_H
11affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#ifdef ENABLE_UNITTEST
12affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <gtest.h>
13affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#endif
14affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/ADT/Uncopyable.h>
15affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/ADT/TypeTraits.h>
16affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/ADT/StringHash.h>
17affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/Path.h>
18affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <mcld/Support/FileHandle.h>
19affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#include <vector>
20affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
21affc150dc44fab1911775a49636d0ce85333b634Zonr Changnamespace mcld
22affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{
23affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
24affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass MemoryArea;
25affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
26affc150dc44fab1911775a49636d0ce85333b634Zonr Chang/** \class HandleToArea
27affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *
28affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  Special double-key associative container. Keys are Path and file handler,
29affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  associative value is MemoryArea.
30affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *
31affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  For high performance, HandleToArea is not designed to contain unique
32affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  <key, value> pair. The key and value may be duplicated.
33affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *
34affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  Like FileHandle, HandleToArea should neither throw exception nor call
35affc150dc44fab1911775a49636d0ce85333b634Zonr Chang *  expressive diagnostic.
36affc150dc44fab1911775a49636d0ce85333b634Zonr Chang */
37affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass HandleToArea : private Uncopyable
38affc150dc44fab1911775a49636d0ce85333b634Zonr Chang{
39affc150dc44fab1911775a49636d0ce85333b634Zonr Changprivate:
40affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  struct Bucket {
41affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    unsigned int hash_value;
42affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    FileHandle* handle;
43affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    MemoryArea* area;
44affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  };
45affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
46affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // the best data structure is a binary search tree.
47affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // However, by the shrinking time-to-market constraint, I used
48affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // vector and sequential search here.
49affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef std::vector<Bucket> HandleToAreaMap;
50affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
51affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef StringHash<BKDR> HashFunction;
52affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
53affc150dc44fab1911775a49636d0ce85333b634Zonr Changpublic:
54affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef HandleToAreaMap::iterator iterator;
55affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  typedef HandleToAreaMap::const_iterator const_iterator;
56affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
57affc150dc44fab1911775a49636d0ce85333b634Zonr Changpublic:
58affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  struct Result {
59affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  public:
60affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    Result(FileHandle* pHandle, MemoryArea* pArea)
61affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      : handle(pHandle), area(pArea) { }
62affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
63affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  public:
64affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    FileHandle* handle;
65affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    MemoryArea* area;
66affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  };
67affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
68affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  struct ConstResult {
69affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  public:
70affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    ConstResult(const FileHandle* pHandle, const MemoryArea* pArea)
71affc150dc44fab1911775a49636d0ce85333b634Zonr Chang      : handle(pHandle), area(pArea) { }
72affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
73affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  public:
74affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    const FileHandle* handle;
75affc150dc44fab1911775a49636d0ce85333b634Zonr Chang    const MemoryArea* area;
76affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  };
77affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
78affc150dc44fab1911775a49636d0ce85333b634Zonr Changpublic:
79affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  bool push_back(FileHandle* pHandle, MemoryArea* pArea);
80affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
81affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  bool erase(MemoryArea* pArea);
82affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
83affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  bool erase(const sys::fs::Path& pPath);
84affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
85affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  Result findFirst(const sys::fs::Path& pPath);
86affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
87affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  ConstResult findFirst(const sys::fs::Path& pPath) const;
88affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
89affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  iterator begin()
90affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.begin(); }
91affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
92affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  iterator end()
93affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.end(); }
94affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
95affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  const_iterator begin() const
96affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.begin(); }
97affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
98affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  const_iterator end() const
99affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.end(); }
100affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
101affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  // -----  capacity  ----- //
102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  bool empty() const
103affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.empty(); }
104affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
105affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  size_t size() const
106affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  { return m_AreaMap.size(); }
107affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
108affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  HandleToArea() : m_AreaMap() { }
109affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
110affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  ~HandleToArea() { }
111affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
112affc150dc44fab1911775a49636d0ce85333b634Zonr Changprivate:
113affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  HandleToAreaMap m_AreaMap;
114affc150dc44fab1911775a49636d0ce85333b634Zonr Chang};
115affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
116affc150dc44fab1911775a49636d0ce85333b634Zonr Chang} // namespace of mcld
117affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
118affc150dc44fab1911775a49636d0ce85333b634Zonr Chang#endif
119affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
120