Home | History | Annotate | Download | only in profiler
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/profiler/strings-storage.h"
      6 
      7 #include "src/base/smart-pointers.h"
      8 #include "src/objects-inl.h"
      9 
     10 namespace v8 {
     11 namespace internal {
     12 
     13 
     14 bool StringsStorage::StringsMatch(void* key1, void* key2) {
     15   return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
     16          0;
     17 }
     18 
     19 
     20 StringsStorage::StringsStorage(Heap* heap)
     21     : hash_seed_(heap->HashSeed()), names_(StringsMatch) {}
     22 
     23 
     24 StringsStorage::~StringsStorage() {
     25   for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
     26     DeleteArray(reinterpret_cast<const char*>(p->value));
     27   }
     28 }
     29 
     30 
     31 const char* StringsStorage::GetCopy(const char* src) {
     32   int len = static_cast<int>(strlen(src));
     33   HashMap::Entry* entry = GetEntry(src, len);
     34   if (entry->value == NULL) {
     35     Vector<char> dst = Vector<char>::New(len + 1);
     36     StrNCpy(dst, src, len);
     37     dst[len] = '\0';
     38     entry->key = dst.start();
     39     entry->value = entry->key;
     40   }
     41   return reinterpret_cast<const char*>(entry->value);
     42 }
     43 
     44 
     45 const char* StringsStorage::GetFormatted(const char* format, ...) {
     46   va_list args;
     47   va_start(args, format);
     48   const char* result = GetVFormatted(format, args);
     49   va_end(args);
     50   return result;
     51 }
     52 
     53 
     54 const char* StringsStorage::AddOrDisposeString(char* str, int len) {
     55   HashMap::Entry* entry = GetEntry(str, len);
     56   if (entry->value == NULL) {
     57     // New entry added.
     58     entry->key = str;
     59     entry->value = str;
     60   } else {
     61     DeleteArray(str);
     62   }
     63   return reinterpret_cast<const char*>(entry->value);
     64 }
     65 
     66 
     67 const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
     68   Vector<char> str = Vector<char>::New(1024);
     69   int len = VSNPrintF(str, format, args);
     70   if (len == -1) {
     71     DeleteArray(str.start());
     72     return GetCopy(format);
     73   }
     74   return AddOrDisposeString(str.start(), len);
     75 }
     76 
     77 
     78 const char* StringsStorage::GetName(Name* name) {
     79   if (name->IsString()) {
     80     String* str = String::cast(name);
     81     int length = Min(kMaxNameSize, str->length());
     82     int actual_length = 0;
     83     base::SmartArrayPointer<char> data = str->ToCString(
     84         DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
     85     return AddOrDisposeString(data.Detach(), actual_length);
     86   } else if (name->IsSymbol()) {
     87     return "<symbol>";
     88   }
     89   return "";
     90 }
     91 
     92 
     93 const char* StringsStorage::GetName(int index) {
     94   return GetFormatted("%d", index);
     95 }
     96 
     97 
     98 const char* StringsStorage::GetFunctionName(Name* name) {
     99   return GetName(name);
    100 }
    101 
    102 
    103 const char* StringsStorage::GetFunctionName(const char* name) {
    104   return GetCopy(name);
    105 }
    106 
    107 
    108 size_t StringsStorage::GetUsedMemorySize() const {
    109   size_t size = sizeof(*this);
    110   size += sizeof(HashMap::Entry) * names_.capacity();
    111   for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
    112     size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
    113   }
    114   return size;
    115 }
    116 
    117 
    118 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
    119   uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
    120   return names_.LookupOrInsert(const_cast<char*>(str), hash);
    121 }
    122 }  // namespace internal
    123 }  // namespace v8
    124