v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
output-stream-writer.h
Go to the documentation of this file.
1// Copyright 2022 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_PROFILER_OUTPUT_STREAM_WRITER_H_
6#define V8_PROFILER_OUTPUT_STREAM_WRITER_H_
7
8#include <algorithm>
9#include <charconv>
10#include <string>
11#include <system_error>
12
13#include "include/v8-profiler.h"
14#include "include/v8config.h"
15#include "src/base/logging.h"
16#include "src/base/vector.h"
17#include "src/utils/memcopy.h"
18
19namespace v8 {
20namespace internal {
21
23 public:
25 : stream_(stream),
26 chunk_size_(stream->GetChunkSize()),
28 chunk_pos_(0),
29 aborted_(false) {
31 }
32 bool aborted() { return aborted_; }
33 void AddCharacter(char c) {
34 DCHECK_NE(c, '\0');
36 chunk_[chunk_pos_++] = c;
38 }
39 void AddString(const char* s) {
40 size_t len = strlen(s);
41 DCHECK_GE(kMaxInt, len);
42 const char* s_end = s + len;
43 while (s < s_end) {
44 int s_chunk_size =
45 std::min(chunk_size_ - chunk_pos_, static_cast<int>(s_end - s));
46 DCHECK_GT(s_chunk_size, 0);
47 MemCopy(chunk_.begin() + chunk_pos_, s, s_chunk_size);
48 s += s_chunk_size;
49 chunk_pos_ += s_chunk_size;
51 }
52 }
53 template <typename T>
54 void AddNumber(T n) {
55 std::to_chars_result result =
56 std::to_chars(chunk_.begin() + chunk_pos_, chunk_.end(), n);
57 if (V8_LIKELY(result.ec == std::errc{})) {
58 chunk_pos_ = static_cast<int>(result.ptr - chunk_.begin());
60 } else {
61 // Expected to be the only possible reason for `to_chars` to fail.
62 CHECK(result.ec == std::errc::value_too_large);
63 // Write the current chunk and try again if we haven't already.
65 "Chunk size insufficient to serialize number");
66 WriteChunk();
67 AddNumber(n);
68 }
69 }
70 void Finalize() {
71 if (aborted_) return;
73 if (chunk_pos_ != 0) {
74 WriteChunk();
75 }
77 }
78
79 private:
82 if (chunk_pos_ == chunk_size_) {
83 WriteChunk();
84 }
85 }
86 void WriteChunk() {
87 if (aborted_) return;
90 aborted_ = true;
91 chunk_pos_ = 0;
92 }
93
99};
100
101} // namespace internal
102} // namespace v8
103
104#endif // V8_PROFILER_OUTPUT_STREAM_WRITER_H_
virtual WriteResult WriteAsciiChunk(char *data, int size)=0
virtual void EndOfStream()=0
constexpr T * begin() const
Definition vector.h:96
constexpr T * end() const
Definition vector.h:103
base::ScopedVector< char > chunk_
OutputStreamWriter(v8::OutputStream *stream)
ZoneVector< RpoNumber > & result
constexpr int kMaxInt
Definition globals.h:374
void MemCopy(void *dest, const void *src, size_t size)
Definition memcopy.h:124
#define CHECK(condition)
Definition logging.h:124
#define CHECK_WITH_MSG(condition, message)
Definition logging.h:118
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define V8_LIKELY(condition)
Definition v8config.h:661