dvander@195: /* vim: set ts=4 sw=4 tw=99 et: dvander@195: * dvander@0: * Copyright (C) 2012 David Anderson dvander@0: * dvander@195: * This file is part of SourcePawn. dvander@0: * dvander@195: * SourcePawn is free software: you can redistribute it and/or modify it under dvander@0: * the terms of the GNU General Public License as published by the Free dvander@0: * Software Foundation, either version 3 of the License, or (at your option) dvander@0: * any later version. dvander@0: * dvander@195: * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY dvander@0: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS dvander@0: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. dvander@0: * dvander@0: * You should have received a copy of the GNU General Public License along with dvander@195: * SourcePawn. If not, see http://www.gnu.org/licenses/. dvander@0: */ dvander@0: #ifndef _include_jitcraft_string_pool_h_ dvander@0: #define _include_jitcraft_string_pool_h_ dvander@0: danderson@91: #include dvander@54: #include "AllocatorPolicies.h" dvander@54: #include "HashTable.h" dvander@54: #include "Strings.h" dvander@0: dvander@59: namespace ke { dvander@0: dvander@54: class Zone; dvander@54: class String; dvander@0: dvander@54: // Guarantee that strings with the same contents will map to one pointer. dvander@0: class StringPool dvander@0: { dvander@54: struct Policy dvander@54: { dvander@54: typedef String * Payload; dvander@54: dvander@54: static uint32 hash(const CharsAndLength &key) { dvander@54: return HashCharSequence(key.str(), key.length()); dvander@54: } dvander@54: dvander@54: static bool matches(const CharsAndLength &key, String *string) { dvander@54: if (string->length() != key.length()) dvander@54: return false; dvander@54: return memcmp(string->chars(), key.str(), key.length() * sizeof(char)) == 0; dvander@54: } dvander@54: }; dvander@54: dvander@54: typedef HashTable StringSet; dvander@0: dvander@0: public: dvander@54: StringPool(Zone *zone); dvander@54: ~StringPool(); dvander@54: bool initialize(); dvander@0: dvander@54: public: dvander@54: String *add(const char *str, size_t len); dvander@54: void sweep(); dvander@54: dvander@54: private: dvander@54: Zone *zone_; dvander@54: StringSet set_; dvander@0: }; dvander@0: dvander@0: } dvander@0: dvander@0: #endif // _include_jitcraft_string_pool_h_