Skip to content

Commit 3be8dd1

Browse files
nordic-krchanangl
authored andcommitted
[nrf fromtree] lib: os: cbprintf: Reduce stack usage when no optimization
Use single fixed size on stack array instead of compile time fitting. When compiler is optimizing it can pick smallest array but with no optimization there is an increased stack usage. Signed-off-by: Krzysztof Chruscinski <[email protected]> (cherry picked from commit 1fb8364) Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 10a0d79 commit 3be8dd1

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

include/zephyr/sys/cbprintf_internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,15 @@ union z_cbprintf_hdr {
357357
/* Allocation to avoid using VLA and alloca. Alloc frees space when leaving
358358
* a function which can lead to increased stack usage if logging is used
359359
* multiple times. VLA is not always available.
360+
*
361+
* Use large array when optimization is off to avoid increased stack usage.
360362
*/
363+
#ifdef CONFIG_NO_OPTIMIZATIONS
364+
#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
365+
__ASSERT(_len <= 32, "Too many string arguments."); \
366+
uint8_t _name##_buf32[32]; \
367+
_name = _name##_buf32
368+
#else
361369
#define Z_CBPRINTF_ON_STACK_ALLOC(_name, _len) \
362370
__ASSERT(_len <= 32, "Too many string arguments."); \
363371
uint8_t _name##_buf4[4]; \
@@ -370,6 +378,7 @@ union z_cbprintf_hdr {
370378
((_len) <= 12 ? _name##_buf12 : \
371379
((_len) <= 16 ? _name##_buf16 : \
372380
_name##_buf32)))
381+
#endif
373382

374383
/** @brief Statically package a formatted string with arguments.
375384
*

0 commit comments

Comments
 (0)