Stack consumption

I use the following code snippet to get the stack consumption (assuming that both interrupt stack and user stack are used):

uint16_t iStackConsumed = 0;
uint16_t uStackConsumed = 0;
void main(void)
{
// Fill user stack and interrupt stack with 0xa5 pattern
    uint8_t *p;
    for (p = __sectop("SU"); p < get_usp(); p++) *p = 0xa5;
    for (p = __sectop("SI"); p < get_isp(); p++) *p = 0xa5;
    
// [...]
}

// [...]
// Update iStackConsumed and uStackConsumed
uint8_t *p;
for (p = __sectop("SU"); p < get_usp(); p++) if (*p != 0xa5) break;
uStackConsumed = (uint8_t*)__secend("SU") - p;
for (p = __sectop("SI"); p < get_isp(); p++) if (*p != 0xa5) break;
iStackConsumed = (uint8_t*)__secend("SI") - p;
// [...]