xref: /aosp_15_r20/external/perfetto/src/shared_lib/stream_writer.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/shared_lib/stream_writer.h"
18 
19 #include <algorithm>
20 
21 #include "perfetto/base/compiler.h"
22 #include "perfetto/protozero/contiguous_memory_range.h"
23 #include "perfetto/protozero/scattered_stream_writer.h"
24 #include "perfetto/public/abi/stream_writer_abi.h"
25 
PerfettoStreamWriterUpdateWritePtr(struct PerfettoStreamWriter * w)26 void PerfettoStreamWriterUpdateWritePtr(struct PerfettoStreamWriter* w) {
27   auto* sw = reinterpret_cast<protozero::ScatteredStreamWriter*>(w->impl);
28   sw->set_write_ptr(w->write_ptr);
29 }
30 
PerfettoStreamWriterNewChunk(struct PerfettoStreamWriter * w)31 void PerfettoStreamWriterNewChunk(struct PerfettoStreamWriter* w) {
32   auto* sw = reinterpret_cast<protozero::ScatteredStreamWriter*>(w->impl);
33   sw->set_write_ptr(w->write_ptr);
34   sw->Extend();
35   perfetto::UpdateStreamWriter(*sw, w);
36 }
37 
PerfettoStreamWriterAnnotatePatch(struct PerfettoStreamWriter * w,uint8_t * patch_addr)38 uint8_t* PerfettoStreamWriterAnnotatePatch(struct PerfettoStreamWriter* w,
39                                            uint8_t* patch_addr) {
40   auto* sw = reinterpret_cast<protozero::ScatteredStreamWriter*>(w->impl);
41   static_assert(PERFETTO_STREAM_WRITER_PATCH_SIZE ==
42                     protozero::ScatteredStreamWriter::Delegate::kPatchSize,
43                 "Size mismatch");
44   memset(patch_addr, 0, PERFETTO_STREAM_WRITER_PATCH_SIZE);
45   return sw->AnnotatePatch(patch_addr);
46 }
47 
PerfettoStreamWriterAppendBytesSlowpath(struct PerfettoStreamWriter * w,const uint8_t * src,size_t size)48 void PerfettoStreamWriterAppendBytesSlowpath(struct PerfettoStreamWriter* w,
49                                              const uint8_t* src,
50                                              size_t size) {
51   auto* sw = reinterpret_cast<protozero::ScatteredStreamWriter*>(w->impl);
52   sw->set_write_ptr(w->write_ptr);
53   sw->WriteBytesSlowPath(src, size);
54   perfetto::UpdateStreamWriter(*sw, w);
55 }
56 
PerfettoStreamWriterReserveBytesSlowpath(struct PerfettoStreamWriter * w,size_t size)57 void PerfettoStreamWriterReserveBytesSlowpath(struct PerfettoStreamWriter* w,
58                                               size_t size) {
59   auto* sw = reinterpret_cast<protozero::ScatteredStreamWriter*>(w->impl);
60   sw->set_write_ptr(w->write_ptr);
61   sw->ReserveBytes(size);
62   perfetto::UpdateStreamWriter(*sw, w);
63 }
64