OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium 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 #ifndef COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ | |
6 #define COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <limits> | |
12 | |
13 #include "base/logging.h" | |
14 #include "components/tracing/tracing_export.h" | |
15 | |
16 // All non-copied strings in tracing are, by API contract, long lived | |
17 // const char* pointers. In order to save trace buffer size (and performance), | |
18 // tracing doesn't copy those strings, but keeps only an index for each event. | |
19 // The way these strings are interned is pretty straightforward: we encode the | |
20 // ptr distance from a known constant in the binary (kInternedStringBase) and | |
21 // use that as string index. | |
22 // There are two use cases for interned strings: | |
23 // 1. In the most common cases they are looked up in batch when the trace is | |
24 // finalized and trace metadata is written, to produce the final string | |
25 // table. This case always works (as long as the strings are long lived) | |
26 // in both component and static builds. We just need to do the reverse math | |
27 // to work out the const char* from the offset at finalization time. | |
28 // 2. When supporting the recovery of non-finalized traces from a crahed | |
29 // process, the same principle can be applied, but only in the case of | |
30 // non-component builds. In a monolithic build the string offset is constatnt | |
DmitrySkiba
2016/11/29 18:09:56
constatnt -> constant
| |
31 // regardless of ASLR. | |
32 | |
33 namespace tracing { | |
34 namespace v2 { | |
35 | |
36 TRACING_EXPORT extern const char kInternedStringBase[]; | |
37 | |
38 inline int64_t InternString(const char* str) { | |
DmitrySkiba
2016/11/29 18:09:56
Maybe rename "intern" to "index" or "stable offset
| |
39 // keeping the diff of two arbitrary pointers into a int64_t is safe as the | |
40 // virtual address space of all the 64-bit architectures we care about is | |
41 // << 64 bits (MMUs are typically limited at 38 bits). | |
42 ptrdiff_t offset = reinterpret_cast<intptr_t>(str) - | |
43 reinterpret_cast<intptr_t>(kInternedStringBase); | |
44 return static_cast<int64_t>(offset); | |
45 } | |
46 | |
47 inline const char* GetInternedStringValue(int64_t offset) { | |
48 intptr_t ptr = reinterpret_cast<intptr_t>(kInternedStringBase) + offset; | |
49 return reinterpret_cast<const char*>(ptr); | |
50 } | |
51 | |
52 } // namespace v2 | |
53 } // namespace tracing | |
54 | |
55 #endif // COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ | |
OLD | NEW |