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 group when the trace is | |
24 // finalized and trace metadata is written, to produce the final string | |
25 // table. This case works always (as long as the strings are long lived) | |
oystein (OOO til 10th of July)
2016/09/09 00:27:34
s/works always/always works/
Primiano Tucci (use gerrit)
2016/09/13 14:40:24
Done.
| |
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. In future, when supporting the recovery of non-finalized traces from a | |
29 // crahed process, the same principle can be applied, but only in the case | |
30 // of non-component builds. In a monolithic build string offset is constatnt | |
oystein (OOO til 10th of July)
2016/09/09 00:27:34
s/build string offset is constatnt/build the strin
Primiano Tucci (use gerrit)
2016/09/13 14:40:24
Done.
| |
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) { | |
39 ptrdiff_t offset = reinterpret_cast<intptr_t>(str) - | |
40 reinterpret_cast<intptr_t>(kInternedStringBase); | |
41 // keeping the diff of two arbitrary pointers into a int64_t is safe as the | |
42 // virtual address space of all the architectures we care about is << 64 bits. | |
43 DCHECK(offset >= std::numeric_limits<int64_t>::min() && | |
kraynov
2016/09/08 16:10:01
Static assert sizeof(ptrdiff_t) <= 8
Primiano Tucci (use gerrit)
2016/09/13 14:40:24
I had to remove this DCHECK completely as it was a
| |
44 offset <= std::numeric_limits<int64_t>::max()); | |
45 return static_cast<int64_t>(offset); | |
46 } | |
47 | |
48 inline const char* GetInternedStringValue(int64_t offset) { | |
49 intptr_t ptr = reinterpret_cast<intptr_t>(kInternedStringBase) + offset; | |
50 return reinterpret_cast<const char*>(ptr); | |
51 } | |
52 | |
53 } // namespace v2 | |
54 } // namespace tracing | |
55 | |
56 #endif // COMPONENTS_TRACING_CORE_STRING_INTERNING_H_ | |
OLD | NEW |