1//===- MergedStringTable.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/LD/MergedStringTable.h"
10
11namespace mcld {
12
13bool MergedStringTable::insertString(llvm::StringRef pString) {
14  return m_StringMap.insert(std::make_pair(pString, 0)).second;
15}
16
17uint64_t MergedStringTable::finalizeOffset() {
18  // trverse the string table and set the offset
19  string_map_iterator it, end = m_StringMap.end();
20  size_t offset = 0;
21  for (it = m_StringMap.begin(); it != end; ++it) {
22    it->setValue(offset);
23    offset += it->getKey().size() + 1;
24  }
25  return offset;
26}
27
28void MergedStringTable::emit(MemoryRegion& pRegion) {
29  char* ptr = reinterpret_cast<char*>(pRegion.begin());
30  string_map_iterator it, end = m_StringMap.end();
31  for (it = m_StringMap.begin(); it != end; ++it) {
32    ::memcpy(ptr, it->getKey().data(), it->getKey().size());
33    ptr += it->getKey().size() + 1;
34  }
35}
36
37size_t MergedStringTable::getOutputOffset(llvm::StringRef pStr) {
38  assert(m_StringMap.find(pStr) != m_StringMap.end());
39  return m_StringMap[pStr];
40}
41
42}  // namespace mcld
43