diff -r 13bd3a8df334 -r 8e22792a3921 jitcraft/StringPool.h --- a/jitcraft/StringPool.h Sat Jul 07 17:11:31 2012 -0700 +++ b/jitcraft/StringPool.h Sat Jul 07 20:28:29 2012 -0700 @@ -18,37 +18,66 @@ #ifndef _include_jitcraft_string_pool_h_ #define _include_jitcraft_string_pool_h_ -#include -#include -#include "PoolAllocator.h" +#include "AllocatorPolicies.h" +#include "HashTable.h" +#include "Strings.h" namespace jc { -typedef const char Name; +class Zone; +class String; -// Guarantee that strings with the same contents will map to one pointer. The -// strings are pool allocated, and search is linear for simplicity. +// Guarantee that strings with the same contents will map to one pointer. class StringPool { - PoolAllocator &pool_; - std::vector strings_; + class CharsAndLength { + const char *str_; + size_t length_; + + public: + CharsAndLength(const char *str, size_t length) + : str_(str), + length_(length) + { + } + + const char *str() const { + return str_; + } + size_t length() const { + return length_; + } + }; + + struct Policy + { + typedef String * Payload; + + static uint32 hash(const CharsAndLength &key) { + return HashCharSequence(key.str(), key.length()); + } + + static bool matches(const CharsAndLength &key, String *string) { + if (string->length() != key.length()) + return false; + return memcmp(string->chars(), key.str(), key.length() * sizeof(char)) == 0; + } + }; + + typedef HashTable StringSet; public: - StringPool(PoolAllocator &pool) - : pool_(pool) - { - } + StringPool(Zone *zone); + ~StringPool(); + bool initialize(); - Name *add(const char *string) { - for (size_t i = 0; i < strings_.size(); i++) { - if (strcmp(strings_[i], string) == 0) - return strings_[i]; - } - char *name = (char *)pool_.allocate(strlen(string) + 1); - strcpy(name, string); - strings_.push_back(name); - return name; - } + public: + String *add(const char *str, size_t len); + void sweep(); + + private: + Zone *zone_; + StringSet set_; }; }