Stack used for function calls

dev
2013-01-27
2013-09-16
  • dev
    dev
    2013-01-27

    As I know every task get assigned some stack(configured by user) from heap.
    Does function call from a task uses the same stack which has been assigned to particular stack ? (Yes, Got from same website while browsing through other questions)
    also what about nested function ? (Yes , assumption)
    Can you please give some pointer(Book, threads) to know more about memory allocation for function calls?

     
  • MEdwards
    MEdwards
    2013-01-27

    How memory is allocated in functions has nothing to do with FreeRTOS, and everything to do with your compiler. If you want to learn about this stuff you would find the answers in a C book not an RTOS book.

    If you declare a local non static (does not use the static key word) variable inside a function then the compiler will wither allocate space for the variable on the stack or just hold the variable in a register. In most cases it will be on the stack if the compiler optimization is low, and in a register if the optimization is high. In both cases every task that calls the function will get a different copy of the variable and the function is said to be re-entrant.

    If you put the word static in front of the variable declaration then the variable will instead be held in a global data pool just like global and file local variables. In this case every task that calls the function will use the same variable, and the function is said to not be re-entrant.

     
  • dev
    dev
    2013-01-27

    I am a novice never worked on RTOS , please bear with me
    Please clarify below doubt
    Task#1  - Has been allocated with Stack#1 from configTOTAL_HEAP_SIZE
    Task#2 - Has been allocated with  Stack#2  from configTOTAL_HEAP_SIZE

    If task#1 has called a function#1 (the called function#1 may call another function#2) , the local variables of function#1 and function#2 will be placed in which stack ? Stack#1 ? or some FreeRTOS uses some global stacks to be used for these things.

     
  • Richard Damon
    Richard Damon
    2013-01-27

    Tasks are no different than any other function in your program (including main). When one function calls another, they use the same stack, so if the function that implements task#1 calls function#1, that function (for this call) will use the stack space from task#1. If the function that implements task#2 calls the same function, then for that call, it will use task#2 stack.

    There are some ports that will move the Interrupt stack to a different space, so that you do not need to reserve space for the interrupts in every task, but that is something different.

     
  • dev
    dev
    2013-01-28

    Thanks, now i got clear picture