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.

Question about MEM_alloc and MEM_free

Hi,

  I got a question from my customer about MEM_alloc and MEM_free. In their code, it is possible that one MEM_alloc is called very close to MEM_free with same input parameters. So they would like to know if the return address of MEM_alloc will be same as the input address of MEM_free. What's the orgnization scheme of MEM module in BIOS5.x. Thanks for your help!

BR

Robert

  • Robert,

    I’m not clear on the basis of the question.  MEM_alloc() and MEM_free() take different arguments.  If they were trying to do some sort of function call optimizations there would be a mismatch.   

    Anyways, to answer the question on placement… both these functions are compiled as separate objects, each from its own C source file.  How these actually get placed in memory is not fixed, but is decided by the linker as it puts together the program image.

    Scott

  • Scott,

      Let me put it in this way. The customer's code is like as below.

      Void TaskFunc()

      {

       Ptr *addr1, *addr2;

      addr1 = MEM_alloc(SEG0, BUFSIZE, 0);

      /* do some processes here*/

      MEM_free(SEG0, addr1, BUFSIZE);

      /* do some processes but just takes small number of cycles */

      addr2 = MEM_alloc(SEG0, BUFSIZE, 0);

      .....

    }

      Is it possible that addr2 equals to addr1? If yes, how much the possibility will be? thanks!

    BR

    Robert

  • Hi Robert,

    addr2 could be the same as addr1. MEM maintains a linked list of free memory. So when the second allocation occurs, it could have grabbed the same memory. What is the possibility? Hard to say. If addr1 was the first allocation (and free), then addr2 will have the same address in your example. Since there can be fragmentation in MEM, over time, the chances of addr1 == addr2 generally goes down. This is especially true if you are allocating and freeing different size blocks of memory at non-fixed rates. 

    Todd

  • Odds get lower that they'll be the same if you have multiple threads running, too.  A different higher-priority thread may sneak in and call MEM_alloc() between your MEM_free() and 2nd MEM_alloc().

    Chris

  • Todd,

      Thanks for your explanation! Could you share with me how MEM handle the linked list of free memory? I want to know more details.

    BR

    Robert

  • MEM is a first-fit heap. As blocks are allocated and freed, fragmentation occurs. The free fragments are maintained on an internal freelist. When a new allocation occurs, the freelist is searched. Once a free block is found that satisfies the size and alignment request, that block is returned (adjusted per alignment request). If that block is larger than the requested size, the extra portion is remains on the freelist. When a free occurs, the memory before and after the freed memory is checked. If the memory before or after the freed block is also free, MEM will coalesce the memory to form a larger free block. This helps reduce fragmentation.

    On allocation, once a free block is found that satisfies the size and alignment request, that block (adjusted per alignment request) is returned and the search terminates. A best-fit algorithm would continue through the entire free-list looking for the best fit.

  • Tood,

      Thank you very much. It's helpful!

     

    BR

    Robert