mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
GH-1713: Avoid unneeded reallocs in SerializationFormat::WriteData
This commit is contained in:
parent
2476e36634
commit
9c29c8ddfc
1 changed files with 9 additions and 1 deletions
|
@ -82,9 +82,17 @@ bool SerializationFormat::ReadData(void* b, size_t count)
|
||||||
bool SerializationFormat::WriteData(const void* b, size_t count)
|
bool SerializationFormat::WriteData(const void* b, size_t count)
|
||||||
{
|
{
|
||||||
// Increase buffer if necessary.
|
// Increase buffer if necessary.
|
||||||
|
bool size_changed = false;
|
||||||
while ( output_pos + count > output_size )
|
while ( output_pos + count > output_size )
|
||||||
|
{
|
||||||
output_size *= GROWTH_FACTOR;
|
output_size *= GROWTH_FACTOR;
|
||||||
|
size_changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The glibc standard states explicitly that calling realloc with the same
|
||||||
|
// size is a no-op, but the same claim can't be made on other platforms.
|
||||||
|
// There's really no reason to do that though.
|
||||||
|
if ( size_changed )
|
||||||
output = (char*)util::safe_realloc(output, output_size);
|
output = (char*)util::safe_realloc(output, output_size);
|
||||||
|
|
||||||
memcpy(output + output_pos, b, count);
|
memcpy(output + output_pos, b, count);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue