Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // stringpool.h -- a string pool for gold -*- C++ -*- |
2 | ||
3 | #include <string> | |
4 | #include <list> | |
5 | ||
6 | // Stringpool | |
7 | // Manage a pool of unique strings. | |
8 | ||
9 | #ifndef GOLD_STRINGPOOL_H | |
10 | #define GOLD_STRINGPOOL_H | |
11 | ||
12 | namespace gold | |
13 | { | |
14 | ||
15 | class Stringpool | |
16 | { | |
17 | public: | |
18 | Stringpool(); | |
19 | ||
20 | ~Stringpool(); | |
21 | ||
22 | // Add a string to the pool. This returns a canonical permanent | |
23 | // pointer to the string. | |
24 | const char* add(const char*); | |
25 | ||
26 | const char* add(const std::string& s) | |
27 | { return this->add(s.c_str()); } | |
28 | ||
29 | // Add the prefix of a string to the pool. | |
30 | const char* add(const char *, size_t); | |
31 | ||
32 | private: | |
33 | Stringpool(const Stringpool&); | |
34 | Stringpool& operator=(const Stringpool&); | |
35 | ||
36 | struct stringdata | |
37 | { | |
38 | // Length of data in buffer. | |
39 | size_t len; | |
40 | // Allocated size of buffer. | |
41 | size_t alc; | |
42 | // Buffer. | |
43 | char data[1]; | |
44 | }; | |
45 | ||
46 | const char* add_string(const char*); | |
47 | ||
48 | struct Stringpool_hash | |
49 | { | |
50 | size_t | |
51 | operator()(const char*) const; | |
52 | }; | |
53 | ||
54 | struct Stringpool_eq | |
55 | { | |
56 | bool | |
57 | operator()(const char* p1, const char* p2) const | |
58 | { return strcmp(p1, p2) == 0; } | |
59 | }; | |
60 | ||
61 | typedef Unordered_set<const char*, Stringpool_hash, Stringpool_eq, | |
62 | std::allocator<const char*>, | |
63 | true> String_set_type; | |
64 | String_set_type string_set_; | |
65 | std::list<stringdata*> strings_; | |
66 | }; | |
67 | ||
68 | } // End namespace gold. | |
69 | ||
70 | #endif // !defined(GOLD_STRINGPOOL_H) |