Other Parts Discussed in Thread: CC1310, CCSTUDIO, SYSBIOS
Hi all,
I'm working with CC1310 but the question is more related to TI-RTOS in general.
I'm trying to figure out how to write my code so that I can reduce thread's stack usage.
Since I heavily use structs and struct pointers I would like to understand their impact on code.
Assume I have a struct like this one:
typedef struct MyStruct_S
{
char _arr[128];
char _v;
}MyStruct_T;
char getV(PeerDescriptor_T *peerPtr)
{
char myV = peerPtr->_v;
return myV;
}
I made some experiments with my code by looking at ROV. And this I what I get
experiment 1)
void myThreadFunc(void *arg)
{
...
//stack peak is 60
MyStruct_T *myPtr = (MyStruct_T*) malloc(sizeof(MyStruct_T));
//now peak is 268
char v = getV(myPtr);
//now peak is still 268
...
}
experiment 2) Struct allocated outiside the thread and accessed inside the thread
static MyStruct_T myStruct;
...
void myThreadFunc(void *arg)
{
//stack peak is 60
char v = getV(& myStruct);
//stack peak is 112
...
}
experiment 3) same as 2) but with a modified struct field order.
typedef struct MyStruct_S
{
char _v;
char _arr[128];
}MyStruct_T;
void myThreadFunc(void *arg)
{
//stack peak is 60
char v = getV(& myStruct);
//stack peak is 60 again
...
}
I really cannot understand the results. So I have these questions:
- By what amount calling malloc() inside a task increases stack usage? I mean, does it take sizeof(MyStruct_T) bytes in heap and sizeof(MyStruct_T) bytes in stack?
- When a struct field is accessed by struct pointer, does the whole struct is loaded into task stack? How struct field order changes stack usage?
Thank you very much