1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_PROFILER_STRINGS_STORAGE_H_ 6 #define V8_PROFILER_STRINGS_STORAGE_H_ 7 8 #include <stdarg.h> 9 10 #include "src/base/compiler-specific.h" 11 #include "src/base/hashmap.h" 12 #include "src/base/platform/mutex.h" 13 #include "src/common/globals.h" 14 15 namespace v8 { 16 namespace internal { 17 18 class Name; 19 class Symbol; 20 21 // Provides a storage of strings allocated in C++ heap, to hold them 22 // forever, even if they disappear from JS heap or external storage. 23 class V8_EXPORT_PRIVATE StringsStorage { 24 public: 25 StringsStorage(); 26 ~StringsStorage(); 27 StringsStorage(const StringsStorage&) = delete; 28 StringsStorage& operator=(const StringsStorage&) = delete; 29 30 // Copies the given c-string and stores it, returning the stored copy, or just 31 // returns the existing string in storage if it already exists. 32 const char* GetCopy(const char* src); 33 // Returns a formatted string, de-duplicated via the storage. 34 PRINTF_FORMAT(2, 3) const char* GetFormatted(const char* format, ...); 35 // Returns a stored string resulting from name, or "<symbol>" for a symbol. 36 const char* GetName(Name name); 37 // Returns the string representation of the int from the store. 38 const char* GetName(int index); 39 // Appends string resulting from name to prefix, then returns the stored 40 // result. 41 const char* GetConsName(const char* prefix, Name name); 42 // Reduces the refcount of the given string, freeing it if no other 43 // references are made to it. Returns true if the string was successfully 44 // unref'd, or false if the string was not present in the table. 45 bool Release(const char* str); 46 47 // Returns the number of strings in the store. 48 size_t GetStringCountForTesting() const; 49 50 // Returns the size of strings in the store 51 size_t GetStringSize(); 52 53 // Returns true if the strings table is empty. empty()54 bool empty() const { return names_.occupancy() == 0; } 55 56 private: 57 static bool StringsMatch(void* key1, void* key2); 58 // Adds the string to storage and returns it, or if a matching string exists 59 // in the storage, deletes str and returns the matching string instead. 60 const char* AddOrDisposeString(char* str, int len); 61 base::CustomMatcherHashMap::Entry* GetEntry(const char* str, int len); 62 PRINTF_FORMAT(2, 0) 63 const char* GetVFormatted(const char* format, va_list args); 64 const char* GetSymbol(Symbol sym); 65 66 base::CustomMatcherHashMap names_; 67 base::Mutex mutex_; 68 size_t string_size_ = 0; 69 }; 70 71 } // namespace internal 72 } // namespace v8 73 74 #endif // V8_PROFILER_STRINGS_STORAGE_H_ 75