strings-storage.cc 4.58 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
#include "src/profiler/strings-storage.h"
6

7 8
#include <memory>

9
#include "src/utils/allocation.h"
10
#include "src/objects/objects-inl.h"
11 12 13 14 15 16 17 18 19

namespace v8 {
namespace internal {

bool StringsStorage::StringsMatch(void* key1, void* key2) {
  return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
         0;
}

Yang Guo's avatar
Yang Guo committed
20
StringsStorage::StringsStorage() : names_(StringsMatch) {}
21 22

StringsStorage::~StringsStorage() {
23
  for (base::HashMap::Entry* p = names_.Start(); p != nullptr;
lpy's avatar
lpy committed
24
       p = names_.Next(p)) {
25
    DeleteArray(reinterpret_cast<const char*>(p->key));
26 27 28 29 30
  }
}

const char* StringsStorage::GetCopy(const char* src) {
  int len = static_cast<int>(strlen(src));
lpy's avatar
lpy committed
31
  base::HashMap::Entry* entry = GetEntry(src, len);
32
  if (entry->value == nullptr) {
33 34 35
    Vector<char> dst = Vector<char>::New(len + 1);
    StrNCpy(dst, src, len);
    dst[len] = '\0';
36
    entry->key = dst.begin();
37
  }
38 39 40
  entry->value =
      reinterpret_cast<void*>(reinterpret_cast<size_t>(entry->value) + 1);
  return reinterpret_cast<const char*>(entry->key);
41 42 43 44 45 46 47 48 49 50 51
}

const char* StringsStorage::GetFormatted(const char* format, ...) {
  va_list args;
  va_start(args, format);
  const char* result = GetVFormatted(format, args);
  va_end(args);
  return result;
}

const char* StringsStorage::AddOrDisposeString(char* str, int len) {
52
  base::MutexGuard guard(&mutex_);
lpy's avatar
lpy committed
53
  base::HashMap::Entry* entry = GetEntry(str, len);
54
  if (entry->value == nullptr) {
55 56 57 58 59
    // New entry added.
    entry->key = str;
  } else {
    DeleteArray(str);
  }
60 61 62
  entry->value =
      reinterpret_cast<void*>(reinterpret_cast<size_t>(entry->value) + 1);
  return reinterpret_cast<const char*>(entry->key);
63 64 65 66 67 68
}

const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
  Vector<char> str = Vector<char>::New(1024);
  int len = VSNPrintF(str, format, args);
  if (len == -1) {
69
    DeleteArray(str.begin());
70 71
    return GetCopy(format);
  }
72
  return AddOrDisposeString(str.begin(), len);
73 74
}

75
const char* StringsStorage::GetName(Name name) {
76
  if (name.IsString()) {
77
    String str = String::cast(name);
78
    int length = std::min(FLAG_heap_snapshot_string_limit, str.length());
79
    int actual_length = 0;
80
    std::unique_ptr<char[]> data = str.ToCString(
81
        DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
82
    return AddOrDisposeString(data.release(), actual_length);
83
  } else if (name.IsSymbol()) {
84 85 86 87 88 89 90 91 92
    return "<symbol>";
  }
  return "";
}

const char* StringsStorage::GetName(int index) {
  return GetFormatted("%d", index);
}

93
const char* StringsStorage::GetConsName(const char* prefix, Name name) {
94
  if (name.IsString()) {
95
    String str = String::cast(name);
96
    int length = std::min(FLAG_heap_snapshot_string_limit, str.length());
97
    int actual_length = 0;
98
    std::unique_ptr<char[]> data = str.ToCString(
99 100 101 102 103 104
        DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);

    int cons_length = actual_length + static_cast<int>(strlen(prefix)) + 1;
    char* cons_result = NewArray<char>(cons_length);
    snprintf(cons_result, cons_length, "%s%s", prefix, data.get());

105
    return AddOrDisposeString(cons_result, cons_length - 1);
106
  } else if (name.IsSymbol()) {
107 108 109 110 111
    return "<symbol>";
  }
  return "";
}

112 113 114 115 116 117 118 119 120 121
namespace {

inline uint32_t ComputeStringHash(const char* str, int len) {
  uint32_t raw_hash_field =
      StringHasher::HashSequentialString(str, len, kZeroHashSeed);
  return raw_hash_field >> Name::kHashShift;
}

}  // namespace

122
bool StringsStorage::Release(const char* str) {
123
  base::MutexGuard guard(&mutex_);
124
  int len = static_cast<int>(strlen(str));
125
  uint32_t hash = ComputeStringHash(str, len);
126
  base::HashMap::Entry* entry = names_.Lookup(const_cast<char*>(str), hash);
127 128 129 130 131

  // If an entry wasn't found or the address of the found entry doesn't match
  // the one passed in, this string wasn't managed by this StringsStorage
  // instance (i.e. a constant). Ignore this.
  if (!entry || entry->key != str) {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    return false;
  }

  DCHECK(entry->value);
  entry->value =
      reinterpret_cast<void*>(reinterpret_cast<size_t>(entry->value) - 1);

  if (entry->value == 0) {
    names_.Remove(const_cast<char*>(str), hash);
    DeleteArray(str);
  }
  return true;
}

size_t StringsStorage::GetStringCountForTesting() const {
  return names_.occupancy();
}

lpy's avatar
lpy committed
150
base::HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
151
  uint32_t hash = ComputeStringHash(str, len);
152
  return names_.LookupOrInsert(const_cast<char*>(str), hash);
153
}
154

155 156
}  // namespace internal
}  // namespace v8