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