Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Unified Diff: src/strings-storage.cc

Issue 945873002: CpuProfiler: move StringsStorage class to separate source and header files. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/strings-storage.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/strings-storage.cc
diff --git a/src/strings-storage.cc b/src/strings-storage.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1d862d6b24be4e458476d7b918a96d0b89569610
--- /dev/null
+++ b/src/strings-storage.cc
@@ -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
« no previous file with comments | « src/strings-storage.h ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698