Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/trace_event/memory_allocator_dump_guid.h" | 5 #include "base/trace_event/memory_allocator_dump_guid.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/hash.h" | 8 #include "base/sha1.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 namespace trace_event { | 12 namespace trace_event { |
| 13 | 13 |
| 14 namespace { | |
| 15 uint64 HashString(const std::string& str) { | |
| 16 unsigned char sha1_bytes[kSHA1Length]; | |
| 17 SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.data()), str.size(), | |
| 18 &sha1_bytes[0]); | |
| 19 uint64 hash = 0; | |
| 20 for (size_t i = 0; i < sizeof(uint64); ++i) { | |
|
petrcermak
2015/06/23 13:13:43
nit: This relies on the fact that sizeof(uint64) <
Primiano Tucci (use gerrit)
2015/06/23 13:52:07
Ehh, if either uint64 or SHA1 change their size, E
| |
| 21 hash |= sha1_bytes[i]; | |
| 22 hash <<= 8; | |
|
petrcermak
2015/06/23 13:13:43
This order "wastes" 1 byte completely, because the
Primiano Tucci (use gerrit)
2015/06/23 13:52:07
Actually picksi@ had a better idea here about usin
| |
| 23 } | |
|
picksi
2015/06/23 13:35:51
Apart from reversing the bytes, isn't this just th
| |
| 24 return hash; | |
| 25 } | |
| 26 } // namespace | |
| 27 | |
| 14 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid(uint64 guid) : guid_(guid) { | 28 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid(uint64 guid) : guid_(guid) { |
| 15 } | 29 } |
| 16 | 30 |
| 17 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid() | 31 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid() |
| 18 : MemoryAllocatorDumpGuid(0u) { | 32 : MemoryAllocatorDumpGuid(0u) { |
| 19 } | 33 } |
| 20 | 34 |
| 21 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid(const std::string& guid_str) | 35 MemoryAllocatorDumpGuid::MemoryAllocatorDumpGuid(const std::string& guid_str) |
| 22 : MemoryAllocatorDumpGuid(Hash(guid_str)) { | 36 : MemoryAllocatorDumpGuid(HashString(guid_str)) { |
| 23 } | 37 } |
| 24 | 38 |
| 25 std::string MemoryAllocatorDumpGuid::ToString() const { | 39 std::string MemoryAllocatorDumpGuid::ToString() const { |
| 26 return StringPrintf("%" PRIx64, guid_); | 40 return StringPrintf("%" PRIx64, guid_); |
| 27 } | 41 } |
| 28 | 42 |
| 29 } // namespace trace_event | 43 } // namespace trace_event |
| 30 } // namespace base | 44 } // namespace base |
| OLD | NEW |