1 /* 2 * Copyright (C) 2019 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 #ifndef INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_ 18 #define INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_ 19 20 #include <inttypes.h> 21 #include <math.h> 22 #include <string.h> 23 #include <cmath> 24 #include <cstdlib> 25 #include <limits> 26 27 #include "perfetto/base/logging.h" 28 #include "perfetto/ext/base/string_view.h" 29 30 namespace perfetto { 31 namespace base { 32 33 // A helper class which writes formatted data to a string buffer. 34 // This is used in the trace processor where we write O(GBs) of strings and 35 // sprintf is too slow. 36 class StringWriter { 37 public: 38 // Creates a string buffer from a char buffer and length. StringWriter(char * buffer,size_t size)39 StringWriter(char* buffer, size_t size) : buffer_(buffer), size_(size) {} 40 41 // Appends n instances of a char to the buffer. 42 void AppendChar(char in, size_t n = 1) { 43 PERFETTO_DCHECK(pos_ + n <= size_); 44 memset(&buffer_[pos_], in, n); 45 pos_ += n; 46 } 47 48 // Appends a length delimited string to the buffer. AppendString(const char * in,size_t n)49 void AppendString(const char* in, size_t n) { 50 PERFETTO_DCHECK(pos_ + n <= size_); 51 memcpy(&buffer_[pos_], in, n); 52 pos_ += n; 53 } 54 AppendStringView(StringView sv)55 void AppendStringView(StringView sv) { AppendString(sv.data(), sv.size()); } 56 57 // Appends a null-terminated string literal to the buffer. 58 template <size_t N> AppendLiteral(const char (& in)[N])59 inline void AppendLiteral(const char (&in)[N]) { 60 AppendString(in, N - 1); 61 } 62 63 // Appends a StringView to the buffer. AppendString(StringView data)64 void AppendString(StringView data) { AppendString(data.data(), data.size()); } 65 66 // Appends an integer to the buffer. AppendInt(int64_t value)67 void AppendInt(int64_t value) { AppendPaddedInt<'0', 0>(value); } 68 69 // Appends an integer to the buffer, padding with |padchar| if the number of 70 // digits of the integer is less than |padding|. 71 template <char padchar, uint64_t padding> AppendPaddedInt(int64_t sign_value)72 void AppendPaddedInt(int64_t sign_value) { 73 const bool negate = std::signbit(static_cast<double>(sign_value)); 74 uint64_t absolute_value = static_cast<uint64_t>(std::abs(sign_value)); 75 AppendPaddedInt<padchar, padding>(absolute_value, negate); 76 } 77 AppendUnsignedInt(uint64_t value)78 void AppendUnsignedInt(uint64_t value) { 79 AppendPaddedUnsignedInt<'0', 0>(value); 80 } 81 82 // Appends an unsigned integer to the buffer, padding with |padchar| if the 83 // number of digits of the integer is less than |padding|. 84 template <char padchar, uint64_t padding> AppendPaddedUnsignedInt(uint64_t value)85 void AppendPaddedUnsignedInt(uint64_t value) { 86 AppendPaddedInt<padchar, padding>(value, false); 87 } 88 89 // Appends a hex integer to the buffer. 90 template <typename IntType> AppendHexInt(IntType value)91 void AppendHexInt(IntType value) { 92 // TODO(lalitm): trying to optimize this is premature given we almost never 93 // print hex ints. Reevaluate this in the future if we do print them more. 94 size_t res = static_cast<size_t>( 95 snprintf(buffer_ + pos_, size_ - pos_, "%" PRIx64, value)); 96 PERFETTO_DCHECK(pos_ + res <= size_); 97 pos_ += res; 98 } 99 100 // Appends a double to the buffer. AppendDouble(double value)101 void AppendDouble(double value) { 102 // TODO(lalitm): trying to optimize this is premature given we almost never 103 // print doubles. Reevaluate this in the future if we do print them more. 104 size_t res = static_cast<size_t>( 105 snprintf(buffer_ + pos_, size_ - pos_, "%lf", value)); 106 PERFETTO_DCHECK(pos_ + res <= size_); 107 pos_ += res; 108 } 109 AppendBool(bool value)110 void AppendBool(bool value) { 111 if (value) { 112 AppendLiteral("true"); 113 return; 114 } 115 AppendLiteral("false"); 116 } 117 GetStringView()118 StringView GetStringView() { 119 PERFETTO_DCHECK(pos_ <= size_); 120 return StringView(buffer_, pos_); 121 } 122 CreateStringCopy()123 char* CreateStringCopy() { 124 char* dup = reinterpret_cast<char*>(malloc(pos_ + 1)); 125 if (dup) { 126 strncpy(dup, buffer_, pos_); 127 dup[pos_] = '\0'; 128 } 129 return dup; 130 } 131 pos()132 size_t pos() const { return pos_; } size()133 size_t size() const { return size_; } reset()134 void reset() { pos_ = 0; } 135 136 private: 137 template <char padchar, uint64_t padding> AppendPaddedInt(uint64_t absolute_value,bool negate)138 void AppendPaddedInt(uint64_t absolute_value, bool negate) { 139 // Need to add 2 to the number of digits to account for minus sign and 140 // rounding down of digits10. 141 constexpr auto kMaxDigits = std::numeric_limits<uint64_t>::digits10 + 2; 142 constexpr auto kSizeNeeded = kMaxDigits > padding ? kMaxDigits : padding; 143 PERFETTO_DCHECK(pos_ + kSizeNeeded <= size_); 144 145 char data[kSizeNeeded]; 146 147 size_t idx; 148 for (idx = kSizeNeeded - 1; absolute_value >= 10;) { 149 char digit = absolute_value % 10; 150 absolute_value /= 10; 151 data[idx--] = digit + '0'; 152 } 153 data[idx--] = static_cast<char>(absolute_value) + '0'; 154 155 if (padding > 0) { 156 size_t num_digits = kSizeNeeded - 1 - idx; 157 for (size_t i = num_digits; i < padding; i++) { 158 data[idx--] = padchar; 159 } 160 } 161 162 if (negate) 163 buffer_[pos_++] = '-'; 164 AppendString(&data[idx + 1], kSizeNeeded - idx - 1); 165 } 166 167 char* buffer_ = nullptr; 168 size_t size_ = 0; 169 size_t pos_ = 0; 170 }; 171 172 } // namespace base 173 } // namespace perfetto 174 175 #endif // INCLUDE_PERFETTO_EXT_BASE_STRING_WRITER_H_ 176