Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stack buffer when tokenizing smaller buffers #26336

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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