/* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_ #define INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_ #include <assert.h> #include <stdint.h> #include <string.h> #include "perfetto/public/compiler.h" // Type of fields that can be found in a protobuf serialized message. enum PerfettoPbWireType { … }; // Creates a field tag, which encodes the field type and the field id. static inline uint32_t PerfettoPbMakeTag(int32_t field_id, enum PerfettoPbWireType wire_type) { … } enum { … }; // Encodes `value` as a VarInt into `*dst`. // // `dst` must point into a buffer big enough to represent `value`: // PERFETTO_PB_VARINT_MAX_SIZE_* can help. static inline uint8_t* PerfettoPbWriteVarInt(uint64_t value, uint8_t* dst) { … } // Encodes `value` as a fixed32 (little endian) into `*dst`. // // `dst` must point into a buffer with at least 4 bytes of space. static inline uint8_t* PerfettoPbWriteFixed32(uint32_t value, uint8_t* buf) { … } // Encodes `value` as a fixed32 (little endian) into `*dst`. // // `dst` must point into a buffer with at least 8 bytes of space. static inline uint8_t* PerfettoPbWriteFixed64(uint64_t value, uint8_t* buf) { … } // Parses a VarInt from the encoded buffer [start, end). |end| is STL-style and // points one byte past the end of buffer. // The parsed int value is stored in the output arg |value|. Returns a pointer // to the next unconsumed byte (so start < retval <= end) or |start| if the // VarInt could not be fully parsed because there was not enough space in the // buffer. static inline const uint8_t* PerfettoPbParseVarInt(const uint8_t* start, const uint8_t* end, uint64_t* out_value) { … } static inline uint32_t PerfettoPbZigZagEncode32(int32_t value) { … } static inline uint64_t PerfettoPbZigZagEncode64(int64_t value) { … } static inline int32_t PerfettoPbZigZagDecode32(uint32_t value) { … } static inline int64_t PerfettoPbZigZagDecode64(uint64_t value) { … } static inline uint64_t PerfettoPbDoubleToFixed64(double value) { … } static inline uint32_t PerfettoPbFloatToFixed32(float value) { … } #endif // INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_