|
521 | 521 | #define STK_SLEEP_TRAP_STACK_SIZE (STK_STACK_SIZE_MIN) |
522 | 522 | #endif |
523 | 523 |
|
524 | | -/*! \def STK_ALLOCATE_COUNT |
525 | | - \brief Selects a static array element count at compile time based on a mode flag. |
526 | | - \note On GCC/Clang: expands to ONTRUE if (MODE & FLAG) is non-zero, otherwise ONFALSE. |
527 | | - On MSVC/IAR: always expands to max(ONTRUE, ONFALSE) because these compilers do not support |
528 | | - zero-sized arrays. This means MSVC and IAR builds may over-allocate when the flag is not set, |
529 | | - but avoids a compile error. Configuration mistakes that would result in ONFALSE on other |
530 | | - compilers will be silently masked. |
531 | | - \param[in] MODE: Bitmask of active kernel modes (e.g. EKernelMode flags). |
532 | | - \param[in] FLAG: The specific mode bit to test. |
533 | | - \param[in] ONTRUE: Array count to use when FLAG is active. |
534 | | - \param[in] ONFALSE: Array count to use when FLAG is inactive (may be 0 on GCC/Clang). |
535 | | -*/ |
| 524 | +/*! \struct STK_ALLOCATE_COUNT |
| 525 | + \brief Selects a static array element count at compile time based on a mode flag. |
| 526 | + \note On GCC/Clang: evaluates to ONTRUE if (MODE & FLAG) is non-zero, otherwise ONFALSE. |
| 527 | + On MSVC/IAR: always evaluates to the maximum of ONTRUE and ONFALSE because these |
| 528 | + compilers do not support zero-sized arrays. |
| 529 | +
|
| 530 | + \tparam MODE Bitmask of active kernel modes (e.g., EKernelMode flags). |
| 531 | + \tparam FLAG The specific mode bit to test. |
| 532 | + \tparam ONTRUE Array count to use when FLAG is active. |
| 533 | + \tparam ONFALSE Array count to use when FLAG is inactive (may be 0 on GCC/Clang). |
| 534 | +*/ |
| 535 | +template <size_t MODE, size_t FLAG, size_t ONTRUE, size_t ONFALSE> |
| 536 | +struct STK_ALLOCATE_COUNT |
| 537 | +{ |
536 | 538 | #if defined(_MSC_VER) || defined(__ICCARM__) |
537 | | - #define STK_ALLOCATE_COUNT(MODE, FLAG, ONTRUE, ONFALSE) ((ONTRUE) > (ONFALSE) ? (ONTRUE) : (ONFALSE)) |
| 539 | + /* MSVC and IAR builds may over-allocate when the flag is not set to avoid compile errors. */ |
| 540 | + static constexpr size_t Value = ((ONTRUE > ONFALSE) ? ONTRUE : ONFALSE); |
538 | 541 | #else |
539 | | - #define STK_ALLOCATE_COUNT(MODE, FLAG, ONTRUE, ONFALSE) ((((MODE) & (FLAG)) != 0U) ? (ONTRUE) : (ONFALSE)) |
| 542 | + /* GCC and Clang support zero-sized array extensions natively. */ |
| 543 | + static constexpr size_t Value = (((MODE & FLAG) != 0U) ? ONTRUE : ONFALSE); |
540 | 544 | #endif |
| 545 | +}; |
541 | 546 |
|
542 | 547 | /*! \def STK_ENDIAN_IDX_HI |
543 | 548 | \brief Array index of the high 32-bit word when a 64-bit value is viewed as \c uint32_t[2]. |
|
577 | 582 | */ |
578 | 583 | #define STK_UNUSED(X) static_cast<void>(X) |
579 | 584 |
|
| 585 | +/*! \brief A wrapper for a built-in memcpy, redefine to your own if required. |
| 586 | + \note Can be overridden by defining _STK_CUSTOM_MEMCPY in system configuration. |
| 587 | +*/ |
| 588 | +#ifndef _STK_CUSTOM_MEMCPY |
| 589 | +#include <string.h> |
| 590 | +static __stk_forceinline void STK_MEMCPY(void *const dest, const void *const src, const size_t size) |
| 591 | +{ |
| 592 | + /* MISRA-compliant explicitly-typed call to underlying implementation */ |
| 593 | + static_cast<void>(memcpy(dest, src, size)); |
| 594 | +} |
| 595 | +#endif |
| 596 | + |
580 | 597 | /*! \namespace stk |
581 | 598 | \brief Namespace of STK package. |
582 | 599 | */ |
|
0 commit comments