CpuProfiler: move StringsStorage class to separate source and header files.
Mechanical change. This will break dependency between profiler-generator and heap-profiler-generator. Later this will help us to reuse SourcePosition in cpu-profiler. BUG=452067 LOG=n Review URL: https://codereview.chromium.org/945873002 Cr-Commit-Position: refs/heads/master@{#26780}
This commit is contained in:
parent
0aa1d2af37
commit
9b02dc5b5f
2
BUILD.gn
2
BUILD.gn
@ -941,6 +941,8 @@ source_set("v8_base") {
|
||||
"src/string-search.h",
|
||||
"src/string-stream.cc",
|
||||
"src/string-stream.h",
|
||||
"src/strings-storage.cc",
|
||||
"src/strings-storage.h",
|
||||
"src/strtod.cc",
|
||||
"src/strtod.h",
|
||||
"src/token.cc",
|
||||
|
@ -5,7 +5,7 @@
|
||||
#ifndef V8_HEAP_SNAPSHOT_GENERATOR_H_
|
||||
#define V8_HEAP_SNAPSHOT_GENERATOR_H_
|
||||
|
||||
#include "src/profile-generator-inl.h"
|
||||
#include "src/strings-storage.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
@ -18,120 +18,6 @@ namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
|
||||
bool StringsStorage::StringsMatch(void* key1, void* key2) {
|
||||
return strcmp(reinterpret_cast<char*>(key1),
|
||||
reinterpret_cast<char*>(key2)) == 0;
|
||||
}
|
||||
|
||||
|
||||
StringsStorage::StringsStorage(Heap* heap)
|
||||
: hash_seed_(heap->HashSeed()), names_(StringsMatch) {
|
||||
}
|
||||
|
||||
|
||||
StringsStorage::~StringsStorage() {
|
||||
for (HashMap::Entry* p = names_.Start();
|
||||
p != NULL;
|
||||
p = names_.Next(p)) {
|
||||
DeleteArray(reinterpret_cast<const char*>(p->value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetCopy(const char* src) {
|
||||
int len = static_cast<int>(strlen(src));
|
||||
HashMap::Entry* entry = GetEntry(src, len);
|
||||
if (entry->value == NULL) {
|
||||
Vector<char> dst = Vector<char>::New(len + 1);
|
||||
StrNCpy(dst, src, len);
|
||||
dst[len] = '\0';
|
||||
entry->key = dst.start();
|
||||
entry->value = entry->key;
|
||||
}
|
||||
return reinterpret_cast<const char*>(entry->value);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
HashMap::Entry* entry = GetEntry(str, len);
|
||||
if (entry->value == NULL) {
|
||||
// New entry added.
|
||||
entry->key = str;
|
||||
entry->value = str;
|
||||
} else {
|
||||
DeleteArray(str);
|
||||
}
|
||||
return reinterpret_cast<const char*>(entry->value);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
DeleteArray(str.start());
|
||||
return GetCopy(format);
|
||||
}
|
||||
return AddOrDisposeString(str.start(), len);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetName(Name* name) {
|
||||
if (name->IsString()) {
|
||||
String* str = String::cast(name);
|
||||
int length = Min(kMaxNameSize, str->length());
|
||||
int actual_length = 0;
|
||||
SmartArrayPointer<char> data =
|
||||
str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
|
||||
&actual_length);
|
||||
return AddOrDisposeString(data.Detach(), actual_length);
|
||||
} else if (name->IsSymbol()) {
|
||||
return "<symbol>";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetName(int index) {
|
||||
return GetFormatted("%d", index);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetFunctionName(Name* name) {
|
||||
return GetName(name);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetFunctionName(const char* name) {
|
||||
return GetCopy(name);
|
||||
}
|
||||
|
||||
|
||||
size_t StringsStorage::GetUsedMemorySize() const {
|
||||
size_t size = sizeof(*this);
|
||||
size += sizeof(HashMap::Entry) * names_.capacity();
|
||||
for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
|
||||
size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
|
||||
uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
|
||||
return names_.Lookup(const_cast<char*>(str), hash, true);
|
||||
}
|
||||
|
||||
|
||||
JITLineInfoTable::JITLineInfoTable() {}
|
||||
|
||||
|
||||
|
@ -9,42 +9,13 @@
|
||||
#include "include/v8-profiler.h"
|
||||
#include "src/allocation.h"
|
||||
#include "src/hashmap.h"
|
||||
#include "src/strings-storage.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
struct OffsetRange;
|
||||
|
||||
// Provides a storage of strings allocated in C++ heap, to hold them
|
||||
// forever, even if they disappear from JS heap or external storage.
|
||||
class StringsStorage {
|
||||
public:
|
||||
explicit StringsStorage(Heap* heap);
|
||||
~StringsStorage();
|
||||
|
||||
const char* GetCopy(const char* src);
|
||||
const char* GetFormatted(const char* format, ...);
|
||||
const char* GetVFormatted(const char* format, va_list args);
|
||||
const char* GetName(Name* name);
|
||||
const char* GetName(int index);
|
||||
const char* GetFunctionName(Name* name);
|
||||
const char* GetFunctionName(const char* name);
|
||||
size_t GetUsedMemorySize() const;
|
||||
|
||||
private:
|
||||
static const int kMaxNameSize = 1024;
|
||||
|
||||
static bool StringsMatch(void* key1, void* key2);
|
||||
const char* AddOrDisposeString(char* str, int len);
|
||||
HashMap::Entry* GetEntry(const char* str, int len);
|
||||
|
||||
uint32_t hash_seed_;
|
||||
HashMap names_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(StringsStorage);
|
||||
};
|
||||
|
||||
|
||||
// Provides a mapping from the offsets within generated code to
|
||||
// the source line.
|
||||
class JITLineInfoTable : public Malloced {
|
||||
|
123
src/strings-storage.cc
Normal file
123
src/strings-storage.cc
Normal file
@ -0,0 +1,123 @@
|
||||
// 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.
|
||||
|
||||
#include "src/v8.h"
|
||||
|
||||
#include "src/strings-storage.h"
|
||||
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
|
||||
bool StringsStorage::StringsMatch(void* key1, void* key2) {
|
||||
return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
|
||||
0;
|
||||
}
|
||||
|
||||
|
||||
StringsStorage::StringsStorage(Heap* heap)
|
||||
: hash_seed_(heap->HashSeed()), names_(StringsMatch) {}
|
||||
|
||||
|
||||
StringsStorage::~StringsStorage() {
|
||||
for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
|
||||
DeleteArray(reinterpret_cast<const char*>(p->value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetCopy(const char* src) {
|
||||
int len = static_cast<int>(strlen(src));
|
||||
HashMap::Entry* entry = GetEntry(src, len);
|
||||
if (entry->value == NULL) {
|
||||
Vector<char> dst = Vector<char>::New(len + 1);
|
||||
StrNCpy(dst, src, len);
|
||||
dst[len] = '\0';
|
||||
entry->key = dst.start();
|
||||
entry->value = entry->key;
|
||||
}
|
||||
return reinterpret_cast<const char*>(entry->value);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
HashMap::Entry* entry = GetEntry(str, len);
|
||||
if (entry->value == NULL) {
|
||||
// New entry added.
|
||||
entry->key = str;
|
||||
entry->value = str;
|
||||
} else {
|
||||
DeleteArray(str);
|
||||
}
|
||||
return reinterpret_cast<const char*>(entry->value);
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
DeleteArray(str.start());
|
||||
return GetCopy(format);
|
||||
}
|
||||
return AddOrDisposeString(str.start(), len);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetName(Name* name) {
|
||||
if (name->IsString()) {
|
||||
String* str = String::cast(name);
|
||||
int length = Min(kMaxNameSize, str->length());
|
||||
int actual_length = 0;
|
||||
SmartArrayPointer<char> data = str->ToCString(
|
||||
DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
|
||||
return AddOrDisposeString(data.Detach(), actual_length);
|
||||
} else if (name->IsSymbol()) {
|
||||
return "<symbol>";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetName(int index) {
|
||||
return GetFormatted("%d", index);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetFunctionName(Name* name) {
|
||||
return GetName(name);
|
||||
}
|
||||
|
||||
|
||||
const char* StringsStorage::GetFunctionName(const char* name) {
|
||||
return GetCopy(name);
|
||||
}
|
||||
|
||||
|
||||
size_t StringsStorage::GetUsedMemorySize() const {
|
||||
size_t size = sizeof(*this);
|
||||
size += sizeof(HashMap::Entry) * names_.capacity();
|
||||
for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
|
||||
size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
|
||||
uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
|
||||
return names_.Lookup(const_cast<char*>(str), hash, true);
|
||||
}
|
||||
}
|
||||
} // namespace v8::internal
|
47
src/strings-storage.h
Normal file
47
src/strings-storage.h
Normal file
@ -0,0 +1,47 @@
|
||||
// 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.
|
||||
|
||||
#ifndef V8_STRINGS_STORAGE_H_
|
||||
#define V8_STRINGS_STORAGE_H_
|
||||
|
||||
#include "src/allocation.h"
|
||||
#include "src/hashmap.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
|
||||
struct OffsetRange;
|
||||
|
||||
// Provides a storage of strings allocated in C++ heap, to hold them
|
||||
// forever, even if they disappear from JS heap or external storage.
|
||||
class StringsStorage {
|
||||
public:
|
||||
explicit StringsStorage(Heap* heap);
|
||||
~StringsStorage();
|
||||
|
||||
const char* GetCopy(const char* src);
|
||||
const char* GetFormatted(const char* format, ...);
|
||||
const char* GetVFormatted(const char* format, va_list args);
|
||||
const char* GetName(Name* name);
|
||||
const char* GetName(int index);
|
||||
const char* GetFunctionName(Name* name);
|
||||
const char* GetFunctionName(const char* name);
|
||||
size_t GetUsedMemorySize() const;
|
||||
|
||||
private:
|
||||
static const int kMaxNameSize = 1024;
|
||||
|
||||
static bool StringsMatch(void* key1, void* key2);
|
||||
const char* AddOrDisposeString(char* str, int len);
|
||||
HashMap::Entry* GetEntry(const char* str, int len);
|
||||
|
||||
uint32_t hash_seed_;
|
||||
HashMap names_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(StringsStorage);
|
||||
};
|
||||
}
|
||||
} // namespace v8::internal
|
||||
|
||||
#endif // V8_STRINGS_STORAGE_H_
|
@ -849,6 +849,8 @@
|
||||
'../../src/string-search.h',
|
||||
'../../src/string-stream.cc',
|
||||
'../../src/string-stream.h',
|
||||
'../../src/strings-storage.cc',
|
||||
'../../src/strings-storage.h',
|
||||
'../../src/strtod.cc',
|
||||
'../../src/strtod.h',
|
||||
'../../src/ic/stub-cache.cc',
|
||||
|
Loading…
Reference in New Issue
Block a user