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