Memory alignment with OS_CREATETASK function

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Memory alignment with OS_CREATETASK function

    Hi all !

    I'm using embOS with M16C environment and with the Tasking compiler. But I'm not sure to understand why I can't use the OS_malloc function to allocate the stack of a task.
    A small example of my problem :

    int main( void )
    {
    OS_TASK * TCB;
    void * stack;
    OS_InitKern(); /* initialize OS */
    OS_InitHW(); /* initialize Hardware for OS */

    TCB = (OS_TASK*)OS_malloc(sizeof(OS_TASK));
    stack = OS_malloc(1000);

    OS_CREATETASK( TCB, "test", process1, 1, stack );
    OS_Start(); /* Start multitasking */

    return 0;
    }

    I know that some CPU requires alignment of stack. So I would like to know if it's possible to define the stack of the task without an array declaration ?

    Thanks !
  • Hello David,

    I think you can do it this way if you have enough heap memory, but it would make only sense if you want to create and terminate the task at runtime. Otherwise you should use static arrays.

    You should check if the M16C need some alignment, I guess it does not.



    Regards,
    Daniel
  • Hi Daniel,

    Thx for your reply. As you told me, I tried to find the value where the OS_malloc function don't return a NULL pointer.

    When I call the OS_malloc function like that :

    ptr = OS_malloc(sizeof(int) * 300);

    if(ptr != NULL)
    {
    printf("NOT NULL\n");
    }
    else
    {
    printf("ptr IS NULL\n");
    }

    I have : "ptr IS NULL"

    And when I call the OS_malloc function like that :

    ptr = OS_malloc(sizeof(int) * 200);

    if(ptr != NULL)
    {
    printf("NOT NULL\n");
    }
    else
    {
    printf("ptr IS NULL\n");
    }

    I have "NOT NULL" !

    So I think the size of the heap is OK but I probably miss something.
    As you can see the requested size is multiple of sizeof(int)....So if someone can explain me what is the problem!

    I really need to allocate memory dynamically because the code is generated and I don't know how many task will be created.

    Thx in advance.
  • Hi David,
    there is no problem to use allocated memory for tasks and task stack.
    But doing so, you can not use the macro OS_CREATETASK() to create your task.
    You have to use the function OS_CreateTask().
    During task creation, the stack size has to be known and has to be passed to the OS_CreateTask() function as parameter.
    The macro OS_CREATETASK() only works with data structures of known size as parameter for the stack.
    It translates the passed parameter "stack" to sizeof(stack) as stack size.
    In your case, this results in the size of the pointer variable which is of course much to small fo a task stack.
    This point is declared in our manual in the description of the macro OS_CREATETASK().
    Using allocated memory works this way:
    #define TASK_STACK_SIZE 1000
    TCB = (OS_TASK*)OS_malloc(sizeof(OS_TASK));
    stack = OS_malloc(TASK_STACK_SIZE);

    OS_CreateTask (TCB, "test", 1, process1, stack, TASK_STACK_SIZE, 2);
    Please note that the order of parameter differs from the one used in the macro and that additional parameter are needed
    Regards,
    Armin

    The post was edited 1 time, last by SEGGER - Armin ().