aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/include
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-03-06 12:49:31 +0100
committerGitHub <[email protected]>2024-03-06 12:49:31 +0100
commit151b0a7766a115103416e48a6c9e0bf5acc2a5aa (patch)
treef41cc33637185b68315bb1ee9198df2974d02fe4 /src/zencore/include
parentalso update validate to avoid code sign problems (diff)
downloadzen-151b0a7766a115103416e48a6c9e0bf5acc2a5aa.tar.xz
zen-151b0a7766a115103416e48a6c9e0bf5acc2a5aa.zip
Add WriteMeasuredVarUInt to avoid measuring ints twice before writing (#665)
Avoid double resize of buffer in CbWriter::SetName and CbWriter::AddBinary Add WriteMeasuredVarUInt to avoid measuring ints twice before writing
Diffstat (limited to 'src/zencore/include')
-rw-r--r--src/zencore/include/zencore/varint.h46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/zencore/include/zencore/varint.h b/src/zencore/include/zencore/varint.h
index c0d63d814..ae9aceed6 100644
--- a/src/zencore/include/zencore/varint.h
+++ b/src/zencore/include/zencore/varint.h
@@ -181,17 +181,17 @@ ReadVarInt(const void* InData, uint32_t& OutByteCount)
}
/**
- * Write a variable-length unsigned integer.
+ * Write a pre-measured variable-length unsigned integer.
*
* @param InValue An unsigned integer to encode.
+ * @param ByteCount The number of bytes the integer requires to be encoded
* @param OutData A buffer of at least 5 bytes to write the output to.
* @return The number of bytes used in the output.
*/
-inline uint32_t
-WriteVarUInt(uint32_t InValue, void* OutData)
+inline void
+WriteMeasuredVarUInt(uint32_t InValue, uint32_t ByteCount, void* OutData)
{
- const uint32_t ByteCount = MeasureVarUInt(InValue);
- uint8_t* OutBytes = static_cast<uint8_t*>(OutData) + ByteCount - 1;
+ uint8_t* OutBytes = static_cast<uint8_t*>(OutData) + ByteCount - 1;
switch (ByteCount - 1)
{
case 4:
@@ -214,21 +214,35 @@ WriteVarUInt(uint32_t InValue, void* OutData)
break;
}
*OutBytes = uint8_t(0xff << (9 - ByteCount)) | uint8_t(InValue);
- return ByteCount;
}
/**
* Write a variable-length unsigned integer.
*
* @param InValue An unsigned integer to encode.
- * @param OutData A buffer of at least 9 bytes to write the output to.
+ * @param OutData A buffer of at least 5 bytes to write the output to.
* @return The number of bytes used in the output.
*/
inline uint32_t
-WriteVarUInt(uint64_t InValue, void* OutData)
+WriteVarUInt(uint32_t InValue, void* OutData)
{
const uint32_t ByteCount = MeasureVarUInt(InValue);
- uint8_t* OutBytes = static_cast<uint8_t*>(OutData) + ByteCount - 1;
+ WriteMeasuredVarUInt(InValue, MeasureVarUInt(InValue), OutData);
+ return ByteCount;
+}
+
+/**
+ * Write a pre-measured variable-length unsigned integer.
+ *
+ * @param InValue An unsigned integer to encode.
+ * @param ByteCount The number of bytes the integer requires to be encoded
+ * @param OutData A buffer of at least 9 bytes to write the output to.
+ * @return The number of bytes used in the output.
+ */
+inline void
+WriteMeasuredVarUInt(uint64_t InValue, uint32_t ByteCount, void* OutData)
+{
+ uint8_t* OutBytes = static_cast<uint8_t*>(OutData) + ByteCount - 1;
switch (ByteCount - 1)
{
case 8:
@@ -267,6 +281,20 @@ WriteVarUInt(uint64_t InValue, void* OutData)
break;
}
*OutBytes = uint8_t(0xff << (9 - ByteCount)) | uint8_t(InValue);
+}
+
+/**
+ * Write a variable-length unsigned integer.
+ *
+ * @param InValue An unsigned integer to encode.
+ * @param OutData A buffer of at least 9 bytes to write the output to.
+ * @return The number of bytes used in the output.
+ */
+inline uint32_t
+WriteVarUInt(uint64_t InValue, void* OutData)
+{
+ const uint32_t ByteCount = MeasureVarUInt(InValue);
+ WriteMeasuredVarUInt(InValue, ByteCount, OutData);
return ByteCount;
}