Skip to content

Instantly share code, notes, and snippets.

@Lauszus
Last active November 30, 2023 11:32
Show Gist options
  • Save Lauszus/3251ef567475e5b59e1f8b61f6cd5edf to your computer and use it in GitHub Desktop.
Save Lauszus/3251ef567475e5b59e1f8b61f6cd5edf to your computer and use it in GitHub Desktop.
FreeRTOS get stack size
/**
* task.h
* <PRE>UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask );</PRE>
*
* Returns the stack size associated with xTask. That is, the stack
* size (in words, so on a 32 bit machine a value of 1 means 4 bytes) of the task.
*
* @param xTask Handle of the task associated with the stack to be checked.
* Set xTask to NULL to check the stack of the calling task.
*
* @return The stack size (in words) associated with xTask.
*/
UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
UBaseType_t uxTaskGetStackSize( TaskHandle_t xTask )
{
TCB_t *pxTCB;
UBaseType_t uxReturn;
pxTCB = prvGetTCBFromHandle( xTask );
#if( portSTACK_GROWTH < 0 )
{
uxReturn = pxTCB->pxEndOfStack - pxTCB->pxStack + 1UL;
}
#else /* portSTACK_GROWTH */
{
uxReturn = pxTCB->pxStack - pxTCB->pxEndOfStack + 1UL;
}
#endif /* portSTACK_GROWTH */
return uxReturn;
}
@ljungholm
Copy link

ljungholm commented Nov 29, 2023

Both pxEndOfStack and pxStack are both StackType_t in the kernel,

pxEndOfStack and pxStack are both StackType_t* not StackType_t. Subtracting them will give you the distance apart of the two pointers in units of StackType_t which may be bigger than what can be contained in StackType_t. Compare to uxTaskGetStackHighWaterMark that returns UBaseType_t.

@Lauszus
Copy link
Author

Lauszus commented Nov 29, 2023

Thanks guys! I've now fixed it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment