Skip to content

Commit

Permalink
Use stack buffer when tokenizing smaller buffers (#26336)
Browse files Browse the repository at this point in the history
* Only allocate logging buffer when required buffer size is larger than 32 bytes

* Fix CI issue

* Fix compiler warning
  • Loading branch information
tehampson authored and pull[bot] committed Oct 19, 2023
1 parent 8859a52 commit 41ec577
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/lib/support/logging/CHIPLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,41 @@ void HandleTokenizedLog(uint32_t levels, pw_tokenizer_Token token, pw_tokenizer_
pw_tokenizer_EncodeArgs(types, args, encoded_message + sizeof(token), sizeof(encoded_message) - sizeof(token));
va_end(args);

uint8_t log_category = levels >> 8 & 0xFF;
uint8_t log_module = levels & 0xFF;
char * buffer = (char *) chip::Platform::MemoryAlloc(2 * encoded_size + 1);
uint8_t log_category = levels >> 8 & 0xFF;
uint8_t log_module = levels & 0xFF;
char * logging_buffer = nullptr;

if (buffer)
// To reduce the number of alloc/free that is happening we will use a stack
// buffer when buffer required to log is small.
char stack_buffer[32];
char * allocated_buffer = nullptr;
size_t required_buffer_size = 2 * encoded_size + 1;

if (required_buffer_size > sizeof(stack_buffer))
{
for (int i = 0; i < encoded_size; i++)
allocated_buffer = (char *) chip::Platform::MemoryAlloc(required_buffer_size);
if (allocated_buffer)
{
sprintf(buffer + 2 * i, "%02x", encoded_message[i]);
logging_buffer = allocated_buffer;
}
buffer[2 * encoded_size] = '\0';
Log(log_module, log_category, "%s", buffer);
chip::Platform::MemoryFree(buffer);
}
else
{
logging_buffer = stack_buffer;
}

if (logging_buffer)
{
for (size_t i = 0; i < encoded_size; i++)
{
sprintf(logging_buffer + 2 * i, "%02x", encoded_message[i]);
}
logging_buffer[2 * encoded_size] = '\0';
Log(log_module, log_category, "%s", logging_buffer);
}
if (allocated_buffer)
{
chip::Platform::MemoryFree(allocated_buffer);
}
}

Expand Down

0 comments on commit 41ec577

Please sign in to comment.