5 #include "src/profiler/strings-storage.h" 9 #include "src/allocation.h" 10 #include "src/objects-inl.h" 15 bool StringsStorage::StringsMatch(
void* key1,
void* key2) {
16 return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
20 StringsStorage::StringsStorage() : names_(StringsMatch) {}
22 StringsStorage::~StringsStorage() {
23 for (base::HashMap::Entry* p = names_.Start(); p !=
nullptr;
25 DeleteArray(reinterpret_cast<const char*>(p->value));
29 const char* StringsStorage::GetCopy(
const char* src) {
30 int len =
static_cast<int>(strlen(src));
31 base::HashMap::Entry* entry = GetEntry(src, len);
32 if (entry->value ==
nullptr) {
33 Vector<char> dst = Vector<char>::New(len + 1);
34 StrNCpy(dst, src, len);
36 entry->key = dst.start();
37 entry->value = entry->key;
39 return reinterpret_cast<const char*
>(entry->value);
42 const char* StringsStorage::GetFormatted(
const char* format, ...) {
44 va_start(args, format);
45 const char* result = GetVFormatted(format, args);
50 const char* StringsStorage::AddOrDisposeString(
char* str,
int len) {
51 base::HashMap::Entry* entry = GetEntry(str, len);
52 if (entry->value ==
nullptr) {
59 return reinterpret_cast<const char*
>(entry->value);
62 const char* StringsStorage::GetVFormatted(
const char* format, va_list args) {
63 Vector<char> str = Vector<char>::New(1024);
64 int len = VSNPrintF(str, format, args);
66 DeleteArray(str.start());
67 return GetCopy(format);
69 return AddOrDisposeString(str.start(), len);
72 const char* StringsStorage::GetName(Name name) {
73 if (name->IsString()) {
74 String str = String::cast(name);
75 int length = Min(FLAG_heap_snapshot_string_limit, str->length());
76 int actual_length = 0;
77 std::unique_ptr<char[]> data = str->ToCString(
78 DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
79 return AddOrDisposeString(data.release(), actual_length);
80 }
else if (name->IsSymbol()) {
86 const char* StringsStorage::GetName(
int index) {
87 return GetFormatted(
"%d", index);
90 const char* StringsStorage::GetConsName(
const char* prefix, Name name) {
91 if (name->IsString()) {
92 String str = String::cast(name);
93 int length = Min(FLAG_heap_snapshot_string_limit, str->length());
94 int actual_length = 0;
95 std::unique_ptr<char[]> data = str->ToCString(
96 DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
98 int cons_length = actual_length +
static_cast<int>(strlen(prefix)) + 1;
99 char* cons_result = NewArray<char>(cons_length);
100 snprintf(cons_result, cons_length,
"%s%s", prefix, data.get());
102 return AddOrDisposeString(cons_result, cons_length);
103 }
else if (name->IsSymbol()) {
109 base::HashMap::Entry* StringsStorage::GetEntry(
const char* str,
int len) {
110 uint32_t hash = StringHasher::HashSequentialString(str, len, kZeroHashSeed);
111 return names_.LookupOrInsert(const_cast<char*>(str), hash);