aboutsummaryrefslogtreecommitdiff
path: root/gold/strtab.h
blob: ab22b8fc286f2fec8f85d613e856fad8fca093c7 (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
73
// strtab.h -- manage an ELF string table for gold   -*- C++ -*-

#ifndef GOLD_STRTAB_H
#define GOLD_STRTAB_H

#include <cstring>
#include <string>

namespace gold
{

// This class holds an ELF string table.  We keep a reference count
// for each string, which we use to determine which strings are
// actually required at the end.  When all operations are done, the
// string table is finalized, which sets the offsets to use for each
// string.

class Strtab
{
 public:
  Strtab();

  ~Strtab();

  Strtab_ref* add(const char*);

  Strtab_ref* add(const std::string& s)
  { return this->add(s.c_str()); }

 private:
  Strtab(const Strtab&);
  Strtab& operator=(const Strtab&);

  struct strtab_hash
  {
    std::size_t
    operator()(const char*p);
  };

  struct strtab_eq
  {
    bool
    operator()(const char* p1, const char* p2)
    { return strcmp(p1, p2) == 0; }
  };

  Unordered_map<const char*, Strtab_ref*, strtab_hash, strtab_eq,
		std::allocator<std::pair<const char* const, Strtab_ref*> >,
		true> strings_;
};

// Users of Strtab work with pointers to Strtab_ref structures.  These
// are allocated via new and should be deleted if the string is no
// longer needed.

class Strtab_ref
{
 public:
  ~Strtab_ref();

  const char*
  str() const;

 private:
  Strtab_ref(const Strtab_ref&);
  Strtab_ref& operator=(const Strtab_ref&);

  int refs_;
};

} // End namespace gold.

#endif // !defined(GOLD_STRTAB_H)