LCOV - code coverage report
Current view: top level - src/profiler - strings-storage.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 50 52 96.2 %
Date: 2017-10-20 Functions: 12 12 100.0 %

          Line data    Source code
       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 <memory>
       8             : 
       9             : #include "src/objects-inl.h"
      10             : 
      11             : namespace v8 {
      12             : namespace internal {
      13             : 
      14             : 
      15     3763118 : bool StringsStorage::StringsMatch(void* key1, void* key2) {
      16     3763118 :   return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
      17     3763118 :          0;
      18             : }
      19             : 
      20             : 
      21      164162 : StringsStorage::StringsStorage(Heap* heap)
      22      328324 :     : hash_seed_(heap->HashSeed()), names_(StringsMatch) {}
      23             : 
      24             : 
      25      160877 : StringsStorage::~StringsStorage() {
      26     1875578 :   for (base::HashMap::Entry* p = names_.Start(); p != nullptr;
      27             :        p = names_.Next(p)) {
      28     1553824 :     DeleteArray(reinterpret_cast<const char*>(p->value));
      29             :   }
      30      160877 : }
      31             : 
      32             : 
      33       13354 : const char* StringsStorage::GetCopy(const char* src) {
      34       13354 :   int len = static_cast<int>(strlen(src));
      35       13354 :   base::HashMap::Entry* entry = GetEntry(src, len);
      36       13354 :   if (entry->value == nullptr) {
      37       15238 :     Vector<char> dst = Vector<char>::New(len + 1);
      38        7619 :     StrNCpy(dst, src, len);
      39        7619 :     dst[len] = '\0';
      40        7619 :     entry->key = dst.start();
      41        7619 :     entry->value = entry->key;
      42             :   }
      43       13354 :   return reinterpret_cast<const char*>(entry->value);
      44             : }
      45             : 
      46             : 
      47     2621235 : const char* StringsStorage::GetFormatted(const char* format, ...) {
      48             :   va_list args;
      49     2621235 :   va_start(args, format);
      50     2621235 :   const char* result = GetVFormatted(format, args);
      51     2621235 :   va_end(args);
      52     2621235 :   return result;
      53             : }
      54             : 
      55             : 
      56     5306768 : const char* StringsStorage::AddOrDisposeString(char* str, int len) {
      57     5306768 :   base::HashMap::Entry* entry = GetEntry(str, len);
      58     5306768 :   if (entry->value == nullptr) {
      59             :     // New entry added.
      60     1553694 :     entry->key = str;
      61     1553694 :     entry->value = str;
      62             :   } else {
      63             :     DeleteArray(str);
      64             :   }
      65     5306768 :   return reinterpret_cast<const char*>(entry->value);
      66             : }
      67             : 
      68             : 
      69     2621235 : const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
      70     2621235 :   Vector<char> str = Vector<char>::New(1024);
      71     2621235 :   int len = VSNPrintF(str, format, args);
      72     2621235 :   if (len == -1) {
      73             :     DeleteArray(str.start());
      74           0 :     return GetCopy(format);
      75             :   }
      76     2621235 :   return AddOrDisposeString(str.start(), len);
      77             : }
      78             : 
      79             : 
      80     2708422 : const char* StringsStorage::GetName(Name* name) {
      81     2708422 :   if (name->IsString()) {
      82             :     String* str = String::cast(name);
      83             :     int length = Min(kMaxNameSize, str->length());
      84     2685533 :     int actual_length = 0;
      85             :     std::unique_ptr<char[]> data = str->ToCString(
      86     2685533 :         DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
      87     5371066 :     return AddOrDisposeString(data.release(), actual_length);
      88       22889 :   } else if (name->IsSymbol()) {
      89             :     return "<symbol>";
      90             :   }
      91           0 :   return "";
      92             : }
      93             : 
      94     1799335 : const char* StringsStorage::GetName(int index) {
      95     1799335 :   return GetFormatted("%d", index);
      96             : }
      97             : 
      98      195422 : const char* StringsStorage::GetFunctionName(Name* name) {
      99      195422 :   return GetName(name);
     100             : }
     101             : 
     102       13329 : const char* StringsStorage::GetFunctionName(const char* name) {
     103       13329 :   return GetCopy(name);
     104             : }
     105             : 
     106     5320122 : base::HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
     107     5320122 :   uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
     108    10640244 :   return names_.LookupOrInsert(const_cast<char*>(str), hash);
     109             : }
     110             : 
     111             : }  // namespace internal
     112             : }  // namespace v8

Generated by: LCOV version 1.10