strings-storage.h 2.17 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_PROFILER_STRINGS_STORAGE_H_
#define V8_PROFILER_STRINGS_STORAGE_H_
7

8 9
#include <stdarg.h>

jfb's avatar
jfb committed
10
#include "src/base/compiler-specific.h"
lpy's avatar
lpy committed
11
#include "src/base/hashmap.h"
12
#include "src/common/globals.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18
class Name;

19 20
// Provides a storage of strings allocated in C++ heap, to hold them
// forever, even if they disappear from JS heap or external storage.
21
class V8_EXPORT_PRIVATE StringsStorage {
22
 public:
Yang Guo's avatar
Yang Guo committed
23
  StringsStorage();
24 25
  ~StringsStorage();

26 27
  // Copies the given c-string and stores it, returning the stored copy, or just
  // 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.
jfb's avatar
jfb committed
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 36
  // Appends string resulting from name to prefix, then returns the stored
  // result.
37
  const char* GetConsName(const char* prefix, Name name);
38 39 40 41 42 43 44
  // Reduces the refcount of the given string, freeing it if no other
  // references are made to it.
  // Returns true if the string was successfully unref'd.
  bool Release(const char* str);

  // Returns the number of strings in the store.
  size_t GetStringCountForTesting() const;
45 46 47

 private:
  static bool StringsMatch(void* key1, void* key2);
48 49
  // Adds the string to storage and returns it, or if a matching string exists
  // in the storage, deletes str and returns the matching string instead.
50
  const char* AddOrDisposeString(char* str, int len);
51
  base::CustomMatcherHashMap::Entry* GetEntry(const char* str, int len);
52 53
  PRINTF_FORMAT(2, 0)
  const char* GetVFormatted(const char* format, va_list args);
54

55
  base::CustomMatcherHashMap names_;
56 57 58

  DISALLOW_COPY_AND_ASSIGN(StringsStorage);
};
59

60 61
}  // namespace internal
}  // namespace v8
62

63
#endif  // V8_PROFILER_STRINGS_STORAGE_H_