-
-
Save Lauszus/3251ef567475e5b59e1f8b61f6cd5edf to your computer and use it in GitHub Desktop.
/** | |
* 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; | |
} |
Hi :)
uxReturn type should be UBaseType_t because this variable stores the difference of the addresses. StackType_t is uint8_t usually, so it is not wide enough (e.g. in 32 bit system).
Both pxEndOfStack
and pxStack
are both StackType_t
in the kernel, so the difference should always fit in StackType_t
. See: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c
pxStack and pxEndOfStack are pointers to uint8_t, so the uxReturn contains the stack size in bytes, not in words.
That depends on the size of StackType_t
. On a 32-bit system it will return the size in words. I kept is like this, as that is the way it's used in the FreeRTOS API. For instance the stack size is set in words on a 32-bit system.
at uxReturn calculation you should add sizeof(UBaseType_t) instead of 1UL, because this is the size of one lastly pointed word.
This is only correct if you return the result in bytes. In my case it is not for the reasons explained above.
Return type should be UBaseType_t instead of StackType_t
I have to disagree for the reasons explained above :)
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.
Thanks guys! I've now fixed it :)
Hi @Lauszus,
I think there is a problem with your calculation. Please check the fix on my fork.
Please feel free to use my modification....if you wish :)