v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
perfetto-utils.h
Go to the documentation of this file.
1// Copyright 2024 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#ifndef V8_TRACING_PERFETTO_UTILS_H_
6#define V8_TRACING_PERFETTO_UTILS_H_
7
8#include <cstdint>
9#include <cstring>
10#include <vector>
11
12#include "include/v8config.h"
13#include "src/base/hashing.h"
14#include "src/base/logging.h"
15#include "src/objects/string.h"
16#include "src/objects/tagged.h"
17
18namespace v8 {
19namespace internal {
20
21// Helper class to write String objects into Perfetto protos. Deals with
22// character encoding and String objects composed of multiple slices.
24 public:
25 explicit PerfettoV8String(Tagged<String> string);
26
29
32
33 bool is_one_byte() const { return is_one_byte_; }
34 template <typename Proto>
35 void WriteToProto(Proto& proto) const {
36 if (is_one_byte()) {
37 proto.set_latin1(buffer_.get(), size_);
38 } else {
39#if defined(V8_TARGET_BIG_ENDIAN)
40 proto.set_utf16_be(buffer_.get(), size_);
41#else
42 proto.set_utf16_le(buffer_.get(), size_);
43#endif
44 }
45 }
46
47 bool operator==(const PerfettoV8String& o) const {
48 return is_one_byte_ == o.is_one_byte_ && size_ == o.size_ &&
49 memcmp(buffer_.get(), o.buffer_.get(), size_) == 0;
50 }
51
52 bool operator!=(const PerfettoV8String& o) const { return !(*this == o); }
53
54 struct Hasher {
55 size_t operator()(const PerfettoV8String& s) const {
56 base::Hasher hash;
57 hash.AddRange(s.buffer_.get(), s.buffer_.get() + s.size_);
58 hash.Combine(s.is_one_byte_);
59 return hash.hash();
60 }
61 };
62
63 private:
65 size_t size_;
66 std::unique_ptr<uint8_t[]> buffer_;
67};
68
69} // namespace internal
70} // namespace v8
71
72#endif // V8_TRACING_PERFETTO_UTILS_H_
Hasher & AddRange(Iterator first, Iterator last)
Definition hashing.h:127
static constexpr size_t Combine(const T &... ts)
Definition hashing.h:144
void WriteToProto(Proto &proto) const
PerfettoV8String(Tagged< String > string)
bool operator!=(const PerfettoV8String &o) const
PerfettoV8String & operator=(const PerfettoV8String &) V8_NOEXCEPT=delete
std::unique_ptr< uint8_t[]> buffer_
bool operator==(const PerfettoV8String &o) const
PerfettoV8String(const PerfettoV8String &) V8_NOEXCEPT=delete
PerfettoV8String(PerfettoV8String &&) V8_NOEXCEPT=default
#define V8_NOEXCEPT
size_t operator()(const PerfettoV8String &s) const