LCOV - code coverage report
Current view: top level - src/profiler - strings-storage.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 50 57 87.7 %
Date: 2017-04-26 Functions: 12 13 92.3 %

          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     5171434 : bool StringsStorage::StringsMatch(void* key1, void* key2) {
      16     5171434 :   return strcmp(reinterpret_cast<char*>(key1), reinterpret_cast<char*>(key2)) ==
      17     5171434 :          0;
      18             : }
      19             : 
      20             : 
      21      181794 : StringsStorage::StringsStorage(Heap* heap)
      22      363587 :     : hash_seed_(heap->HashSeed()), names_(StringsMatch) {}
      23             : 
      24             : 
      25      178781 : StringsStorage::~StringsStorage() {
      26     2187386 :   for (base::HashMap::Entry* p = names_.Start(); p != NULL;
      27             :        p = names_.Next(p)) {
      28     1829824 :     DeleteArray(reinterpret_cast<const char*>(p->value));
      29             :   }
      30      178781 : }
      31             : 
      32             : 
      33       29515 : const char* StringsStorage::GetCopy(const char* src) {
      34       29515 :   int len = static_cast<int>(strlen(src));
      35       29515 :   base::HashMap::Entry* entry = GetEntry(src, len);
      36       29515 :   if (entry->value == NULL) {
      37       25380 :     Vector<char> dst = Vector<char>::New(len + 1);
      38       12690 :     StrNCpy(dst, src, len);
      39       12690 :     dst[len] = '\0';
      40       12690 :     entry->key = dst.start();
      41       12690 :     entry->value = entry->key;
      42             :   }
      43       29515 :   return reinterpret_cast<const char*>(entry->value);
      44             : }
      45             : 
      46             : 
      47     3894240 : const char* StringsStorage::GetFormatted(const char* format, ...) {
      48             :   va_list args;
      49     3894240 :   va_start(args, format);
      50     3894240 :   const char* result = GetVFormatted(format, args);
      51     3894240 :   va_end(args);
      52     3894240 :   return result;
      53             : }
      54             : 
      55             : 
      56     6975044 : const char* StringsStorage::AddOrDisposeString(char* str, int len) {
      57     6975044 :   base::HashMap::Entry* entry = GetEntry(str, len);
      58     6975044 :   if (entry->value == NULL) {
      59             :     // New entry added.
      60     1825286 :     entry->key = str;
      61     1825286 :     entry->value = str;
      62             :   } else {
      63             :     DeleteArray(str);
      64             :   }
      65     6975044 :   return reinterpret_cast<const char*>(entry->value);
      66             : }
      67             : 
      68             : 
      69     3894240 : const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
      70     3894240 :   Vector<char> str = Vector<char>::New(1024);
      71     3894240 :   int len = VSNPrintF(str, format, args);
      72     3894240 :   if (len == -1) {
      73             :     DeleteArray(str.start());
      74           0 :     return GetCopy(format);
      75             :   }
      76     3894240 :   return AddOrDisposeString(str.start(), len);
      77             : }
      78             : 
      79             : 
      80     3104407 : const char* StringsStorage::GetName(Name* name) {
      81     3104407 :   if (name->IsString()) {
      82             :     String* str = String::cast(name);
      83             :     int length = Min(kMaxNameSize, str->length());
      84     3080804 :     int actual_length = 0;
      85             :     std::unique_ptr<char[]> data = str->ToCString(
      86     3080804 :         DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length, &actual_length);
      87     6161608 :     return AddOrDisposeString(data.release(), actual_length);
      88       23603 :   } else if (name->IsSymbol()) {
      89             :     return "<symbol>";
      90             :   }
      91           0 :   return "";
      92             : }
      93             : 
      94             : 
      95     2931412 : const char* StringsStorage::GetName(int index) {
      96     2931412 :   return GetFormatted("%d", index);
      97             : }
      98             : 
      99             : 
     100      201858 : const char* StringsStorage::GetFunctionName(Name* name) {
     101      201858 :   return GetName(name);
     102             : }
     103             : 
     104             : 
     105       29485 : const char* StringsStorage::GetFunctionName(const char* name) {
     106       29485 :   return GetCopy(name);
     107             : }
     108             : 
     109             : 
     110           0 : size_t StringsStorage::GetUsedMemorySize() const {
     111             :   size_t size = sizeof(*this);
     112           0 :   size += sizeof(base::HashMap::Entry) * names_.capacity();
     113           0 :   for (base::HashMap::Entry* p = names_.Start(); p != NULL;
     114             :        p = names_.Next(p)) {
     115           0 :     size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
     116             :   }
     117           0 :   return size;
     118             : }
     119             : 
     120     7004559 : base::HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
     121     7004559 :   uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
     122    14009118 :   return names_.LookupOrInsert(const_cast<char*>(str), hash);
     123             : }
     124             : }  // namespace internal
     125             : }  // namespace v8

Generated by: LCOV version 1.10