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.

Buffer of structures in MSP430

Hi,

I would like to implement a buffer of structures. The code will make use of malloc and Free functions, to allocate memory and free memory during run-time.

Is this possible? memory efficient?

Thanks for your feedback.

regards

  • Never used functions like malloc or free on a micro...
  • Malloc is no problem. I often use it in my code. It's free that causes problems. Especially if you malloc different sizes.
    On the GOES OS I used some memory management method that was similar to malloc, except that you had to lock allocated memory before use. If it wasn't locked, the memory blocks could be shifted/rearranged to consolidate space. malloc returned a handle and only lock returned the physical address pointer, which was invalid after an unlock.
    Using a method like this, dynamically allocated memory is no problem even on an MCU.
    (GEOS was an OS that provided preemptive multitasking and a graphical UI on a PC/XT with only 512k ram, so efficient memory handling was crucial)
  • Jens,

    could you give an example when to use malloc() on the MSP? And why?

    Dennis
  • The original post mentioned that the user will use Free as well as Malloc.

    An example of a memory-pool implementation can be found at:

    https://github.com/quarkng/MemPool_MSP430

    Please note, there are MANY different ways to implement mem-pools.  The above is just an example, not necessarily the best for all applications.

  • An example for using malloc is my multi threading code. Usually, you start multiple threads but then they run and never end. (of course you can create short-lived threads too, but that's an application I never met so far)
    So if I start a thread, I allocate stack space and space for the management structure on the heap using malloc.
    So the main code can determine depending on runtime configuration which threads to start instead of doing a fixed allocation for all space ever needed for all possible threads.
    Of course, enough memory must be available for all threads that could ever run simultaneously. But in my projects, never all of them are enabled together.
    If using short-live threads, it is still usable. Once allocates structures of ended threads remain in the management chain (with an 'ignore' flag set) and can be re-used for the next thread started. Same for the stack space (same-sized stack requirements presumed)

    The main problem with malloc/free is when you alloc and free diferent-sized memory blocks in changing order. If you free them in reverse order, it is no problem (assuming a 'smart' implementation of malloc and free, joining adjacent free space)

    BTW: the same applies to dynamically created objects in C++.

**Attention** This is a public forum