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.

osal_mem_alloc or static allocation ?

Hi All

Is there any special reason to dynamically allocate a buffer which is used only in a function ?
I can see for instance in TI BLE examples:

void myFunction(void)
{
   uint8 *temp_buf = osal_mem_alloc(numBytes);
   (void)NPI_ReadTransport(temp_buf, numBytes);
   ...
   osal_mem_free(temp_buf);
}

Can't this be replaced by a static allocation if temp_buf is used only inside the function ?
Something like:

uint8 temp_buf[128];
(void)NPI_ReadTransport(&temp_buf[0], numBytes);

Since I'm sure numBytes is lower or equal to 128.


Thanks
Jerome

  • Hello Jerome,

    There is no reason that I can see why you can't.  I think it all boils down to programming style and planning.  Using the API provides a simple way to allocate and deallocate memory, allowing for a somewhat better approach to memory management (Heap).

    For your static allocation, you have committed 128 bytes of memory at compile time.  Doing it dynamically only commits the number of bytes you need.  Plus I believe the API returns a value for the function so that you can tell when you have exceeded the memory available..

    There are other pros and cons to using mem_alloc in C, so you may want to search the web for what impact it has in your application and for when/where best to use it.

    Thanks,