1//===- HandleToArea.cpp ----------------------------------------------------===//
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#include <mcld/Support/HandleToArea.h>
10#include <mcld/Support/MemoryArea.h>
11#include <llvm/ADT/StringRef.h>
12
13using namespace mcld;
14
15//===----------------------------------------------------------------------===//
16// HandleToArea
17//===----------------------------------------------------------------------===//
18bool HandleToArea::push_back(FileHandle* pHandle, MemoryArea* pArea)
19{
20  if (NULL == pHandle || NULL == pArea)
21    return false;
22
23  Bucket bucket;
24  bucket.hash_value = HashFunction()(
25                              llvm::StringRef(pHandle->path().native().c_str(),
26                                              pHandle->path().native().size()));
27
28  bucket.handle = pHandle;
29  bucket.area = pArea;
30  m_AreaMap.push_back(bucket);
31  return true;
32}
33
34bool HandleToArea::erase(MemoryArea* pArea)
35{
36  if (NULL == pArea || NULL == pArea->handler())
37    return false;
38
39  return erase(pArea->handler()->path());
40}
41
42bool HandleToArea::erase(const sys::fs::Path& pPath)
43{
44  unsigned int hash_value = HashFunction()(
45                                  llvm::StringRef(pPath.native().c_str(),
46                                                  pPath.native().size()));
47
48  HandleToAreaMap::iterator bucket, bEnd = m_AreaMap.end();
49  for (bucket = m_AreaMap.begin(); bucket != bEnd; ++bucket) {
50    if (bucket->hash_value == hash_value && bucket->handle->path() == pPath) {
51      // found
52      m_AreaMap.erase(bucket);
53      return true;
54    }
55  }
56
57  return false;
58}
59
60HandleToArea::Result HandleToArea::findFirst(const sys::fs::Path& pPath)
61{
62  unsigned int hash_value = HashFunction()(llvm::StringRef(pPath.native().c_str(),
63                                                         pPath.native().size()));
64
65  HandleToAreaMap::iterator bucket, bEnd = m_AreaMap.end();
66
67  for (bucket = m_AreaMap.begin(); bucket != bEnd; ++bucket) {
68    if (bucket->hash_value == hash_value) {
69      if (bucket->handle->path() == pPath) {
70        return Result(bucket->handle, bucket->area);
71      }
72    }
73  }
74
75  return Result(NULL, NULL);
76}
77
78HandleToArea::ConstResult HandleToArea::findFirst(const sys::fs::Path& pPath) const
79{
80  unsigned int hash_value = HashFunction()(llvm::StringRef(pPath.native().c_str(),
81                                                         pPath.native().size()));
82
83  HandleToAreaMap::const_iterator bucket, bEnd = m_AreaMap.end();
84
85  for (bucket = m_AreaMap.begin(); bucket != bEnd; ++bucket) {
86    if (bucket->hash_value == hash_value) {
87      if (bucket->handle->path() == pPath) {
88        return ConstResult(bucket->handle, bucket->area);
89      }
90    }
91  }
92
93  return ConstResult(NULL, NULL);
94}
95
96