• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/globals.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class Name;
18 
19 // Provides a storage of strings allocated in C++ heap, to hold them
20 // forever, even if they disappear from JS heap or external storage.
21 class V8_EXPORT_PRIVATE StringsStorage {
22  public:
23   StringsStorage();
24   ~StringsStorage();
25 
26   // Copies the given c-string and stores it, returning the stored copy, or just
27   // returns the existing string in storage if it already exists.
28   const char* GetCopy(const char* src);
29   // Returns a formatted string, de-duplicated via the storage.
30   PRINTF_FORMAT(2, 3) const char* GetFormatted(const char* format, ...);
31   // Returns a stored string resulting from name, or "<symbol>" for a symbol.
32   const char* GetName(Name* name);
33   // Returns the string representation of the int from the store.
34   const char* GetName(int index);
35   // Appends string resulting from name to prefix, then returns the stored
36   // result.
37   const char* GetConsName(const char* prefix, Name* name);
38 
39  private:
40   static bool StringsMatch(void* key1, void* key2);
41   // Adds the string to storage and returns it, or if a matching string exists
42   // in the storage, deletes str and returns the matching string instead.
43   const char* AddOrDisposeString(char* str, int len);
44   base::CustomMatcherHashMap::Entry* GetEntry(const char* str, int len);
45   PRINTF_FORMAT(2, 0)
46   const char* GetVFormatted(const char* format, va_list args);
47 
48   base::CustomMatcherHashMap names_;
49 
50   DISALLOW_COPY_AND_ASSIGN(StringsStorage);
51 };
52 
53 }  // namespace internal
54 }  // namespace v8
55 
56 #endif  // V8_PROFILER_STRINGS_STORAGE_H_
57