2009-07-23 20:17:34 +02:00
|
|
|
//===-- StringPool.h - Interned string pool ---------------------*- C++ -*-===//
|
2007-12-08 18:07:47 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-08 18:07:47 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares an interned string pool, which helps reduce the cost of
|
|
|
|
// strings by using the same storage for identical strings.
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
// To intern a string:
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
// StringPool Pool;
|
|
|
|
// PooledStringPtr Str = Pool.intern("wakka wakka");
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
// To use the value of an interned string, use operator bool and operator*:
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
// if (Str)
|
|
|
|
// cerr << "the string is" << *Str << "\n";
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
// Pooled strings are immutable, but you can change a PooledStringPtr to point
|
|
|
|
// to another instance. So that interned strings can eventually be freed,
|
|
|
|
// strings in the string pool are reference-counted (automatically).
|
2009-02-20 23:51:36 +01:00
|
|
|
//
|
2007-12-08 18:07:47 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_STRINGPOOL_H
|
|
|
|
#define LLVM_SUPPORT_STRINGPOOL_H
|
|
|
|
|
2007-12-09 02:12:08 +01:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2007-12-08 18:07:47 +01:00
|
|
|
#include <new>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class PooledStringPtr;
|
|
|
|
|
|
|
|
/// StringPool - An interned string pool. Use the intern method to add a
|
|
|
|
/// string. Strings are removed automatically as PooledStringPtrs are
|
|
|
|
/// destroyed.
|
|
|
|
class StringPool {
|
2007-12-08 21:10:40 +01:00
|
|
|
/// PooledString - This is the value of an entry in the pool's interning
|
|
|
|
/// table.
|
2007-12-08 18:07:47 +01:00
|
|
|
struct PooledString {
|
|
|
|
StringPool *Pool; ///< So the string can remove itself.
|
|
|
|
unsigned Refcount; ///< Number of referencing PooledStringPtrs.
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
public:
|
|
|
|
PooledString() : Pool(0), Refcount(0) { }
|
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
friend class PooledStringPtr;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
typedef StringMap<PooledString> table_t;
|
|
|
|
typedef StringMapEntry<PooledString> entry_t;
|
|
|
|
table_t InternTable;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
public:
|
|
|
|
StringPool();
|
|
|
|
~StringPool();
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 21:10:40 +01:00
|
|
|
/// intern - Adds a string to the pool and returns a reference-counted
|
|
|
|
/// pointer to it. No additional memory is allocated if the string already
|
|
|
|
/// exists in the pool.
|
2009-07-23 20:17:34 +02:00
|
|
|
PooledStringPtr intern(const StringRef &Str);
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-10 04:35:18 +01:00
|
|
|
/// empty - Checks whether the pool is empty. Returns true if so.
|
2009-02-20 23:51:36 +01:00
|
|
|
///
|
2007-12-10 04:35:18 +01:00
|
|
|
inline bool empty() const { return InternTable.empty(); }
|
2007-12-08 18:07:47 +01:00
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
/// PooledStringPtr - A pointer to an interned string. Use operator bool to
|
|
|
|
/// test whether the pointer is valid, and operator * to get the string if so.
|
|
|
|
/// This is a lightweight value class with storage requirements equivalent to
|
|
|
|
/// a single pointer, but it does have reference-counting overhead when
|
|
|
|
/// copied.
|
|
|
|
class PooledStringPtr {
|
|
|
|
typedef StringPool::entry_t entry_t;
|
|
|
|
entry_t *S;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
public:
|
|
|
|
PooledStringPtr() : S(0) {}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
explicit PooledStringPtr(entry_t *E) : S(E) {
|
|
|
|
if (S) ++S->getValue().Refcount;
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
|
|
|
|
if (S) ++S->getValue().Refcount;
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
PooledStringPtr &operator=(const PooledStringPtr &That) {
|
|
|
|
if (S != That.S) {
|
|
|
|
clear();
|
|
|
|
S = That.S;
|
|
|
|
if (S) ++S->getValue().Refcount;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
void clear() {
|
|
|
|
if (!S)
|
|
|
|
return;
|
|
|
|
if (--S->getValue().Refcount == 0) {
|
|
|
|
S->getValue().Pool->InternTable.remove(S);
|
2008-01-15 21:02:11 +01:00
|
|
|
S->Destroy();
|
2007-12-08 18:07:47 +01:00
|
|
|
}
|
|
|
|
S = 0;
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
~PooledStringPtr() { clear(); }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
inline const char *begin() const {
|
|
|
|
assert(*this && "Attempt to dereference empty PooledStringPtr!");
|
|
|
|
return S->getKeyData();
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
inline const char *end() const {
|
|
|
|
assert(*this && "Attempt to dereference empty PooledStringPtr!");
|
|
|
|
return S->getKeyData() + S->getKeyLength();
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
inline unsigned size() const {
|
|
|
|
assert(*this && "Attempt to dereference empty PooledStringPtr!");
|
|
|
|
return S->getKeyLength();
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
inline const char *operator*() const { return begin(); }
|
|
|
|
inline operator bool() const { return S != 0; }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
|
|
|
|
inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
|
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2007-12-08 18:07:47 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|