This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

use calloc in codec



env: EVM6446 + XDS560

I define an array in codec using malloc, and then I debug my codec, but when I step at "to free the array" code line, I find that I can't debug any more, because I can not find the "yellow arrow" in CCS, I don't know where it is after stepping at "free" code line.

here is my code example in codec:

XDAS_Int32  XXX_XXX_process (....)

{

          .........................

          f1();

         ..........................

}

 

void f1()

{

    //here I want to define an array using malloc

   char* tmparray = 0;

   tmparray = (char*) calloc(720*576,sizeof(char));          /*I'm sure that "tmparray" is not NULL here*/

  // here I use "tmparray" to calculate some values.

 ..................

 // After using it, I use "free" function to avoid memory leak.

 free(tmparray);

// But after stepping at " free(tmparray);", I press "F10" and then the "yellow arrow" disappears.

// and I can not debug the codec.

}

 

So I wonder I should consider to use other function to apply blocks of memory space in codec.

 

[edit] I've read a post in community, from Bernie's first response I learn that I can use malloc or calloc to apply memory space in c64+. Do I need to free these memory spaces after using it to calculate?

         I think if I need to free them, my strange question above will occur.

 

Merry Christmas  :)

  • Though you can use malloc or calloc on the C64x+ to get contiguous memory, if you are in a XDM/XDAIS compliant algorithm you should not be doing this within the algorithm, you should be requesting memory through MEMtab so that the user of your algorithm can allocate the memory for you, otherwise the user of your algorithm has no control over the allocation and they can run into failure issues if multiple codecs are allocating too much off of the heap. This is just a bit of a side track though as this should not effect your ability to free the memory. You do not need to free the memory unless you intend to use it elsewhere, that is to malloc more later to the point you would run out of heap space.

    As to finding the yellow arrow you may want to take a look at your disassembly window, if something during the free call caused the program counter to get lost it could be in a place where there is no C code, you can also more manually locate the program counter with the view register command, the PC value should indicate where in memory the yellow arrow belongs.

  • I've read DSP Algorithm Standard API Reference , this article tells me how to set Memtab in algAlloc function. But I still have a question about it.

    I'll use "videnc_copy" as example:

    Int VIDENCCOPY_TI_alloc(const IALG_Params *algParams,
        IALG_Fxns **pf, IALG_MemRec memTab[]){

         /* Request memory for my object */
        memTab[0].size = sizeof(VIDENCCOPY_TI_Obj);
        memTab[0].alignment = 0;
        memTab[0].space = IALG_EXTERNAL;
        memTab[0].attrs = IALG_PERSIST;

       /*Here I write a segment of code example to explain my question*/

        memTab[1].size = algParams->extArribute * sizeof(int);   // I extend IALG_Params structure, algParams->extArribute = 20;

        memTab[1].alignment = 0;

        memTab[1].space = IALG_EXTERNAL;

        memTab[1].attrs = IALG_SCRATCH;

    }

     

    XDAS_Int32 VIDENCCOPY_TI_process(IVIDENC_Handle h, XDM_BufDesc *inBufs,
        XDM_BufDesc *outBufs, IVIDENC_InArgs *inArgs, IVIDENC_OutArgs *outArgs){

        // Here I want to know how to use the memory applied in VIDENCCOPY_TI_alloc function.

       // Could I use " calloc(20,sizeof(int)):"  or other functions?

    }

     

    [edit] I think I don't understand the relationship between memtab and memory space that I can use in "xxxx.xxxxx.process" function.

              Could The memory spaces applied by memtab[1]  be used only for one variant or even more?

              Which functions should I call when I want to use the memory spaces applied by memtab[1] while coding in "xxxxx_xxxx_process" function?

     

    Thanks.

     

  • After reading DSP Algorithm Standard API Reference again,  I understand the framework for allocating and using memory spaces. I'll write something as I've understood, I'd like to invite experts to check whether it is right or not. Thanks.

    1. while client call XDM compliant alg, "algAlloc" will be called first, in this function client can decide how many memory spaces he want to use. "memtab [icount]  " will be used for allocate memories.

    2.Then "algInit" function will be called after calling "algAlloc", in "algInit" function you can get base address you applied in "algAlloc", for example:

    ....algAlloc(........)

    {

           memTab[0].....

             .............

           memTab[1].size = 20 * sizeof(int);

           memTab[1].type = IALG_PERSIST;

          memTab[1].space = IALG_DARAM;

    }

    After calling "algAlloc", you will get something in "algInit".

    ...algInit(.....)

    {

        ..........

       VIDENCCOPY_TI_Obj (*inst) = (VIDENCCOPY_TI_Obj *)handle;

        handle->attbuf = memTab[1].base;      // this is the base address you just applied in "algAlloc". "attbut" is an attribute in structure "VIDENCCOPY_TI_Obj" I extend.

        handle->attsize = memTab[1].size;

       ......

    }

     

    3. When you call "xxxx_xxxx_process" function in codec, you can convert "handle" into an object of "VIDENCCOPY_TI_Obj", and now you can use "attbuf" and "attsize" to generate an array to help to calculate something in "process" function.

    4. Of course, you need to coding in "algFree" to collect memories allocated in "algAlloc" function.

     

    In brief:

        a. To allocate using "algAlloc"

        b. To get memory information you just allocated using "algInit"

        c. To use the memory spaces to process in "xxxxx_xxxxx_process" function.

    So I think this framework allocaing and using memory spaces will be better than just calling "calloc" or "malloc" while processing.

     

    Thanks.

     

     

  • It sounds like you have the right idea, this looks to be a good explanation of memory allocation in a xdais algorithm, thank you for posting this.

    Just as a reference for the readers of this thread, there is a lot of good xdais documentation through this wiki page, which goes into detail on what these functions do.

  • I'm a newer,and I'm glad to understand the use of MEMTAB,but  I want to know if some "mallc" in my function,what can I do?I can allocate storage in the MEMTAB using "malloc" or some other method?