6

I am working with an embedded system. The application is running on AT91SAMxxxx and cortex m3 lpc17xxx. I am looking into dynamic memory allocation as it would dramatically change the face of the application (and give me more Power).

I think the my only real route is to set out an area of memory for a heap and design a tailored malloc that best fits (pun) my purpose.

When looking at different algorithms for memory allocation you cant not stumble upon Doug Lea's malloc. I take it this has been used in embedded systems such as mine where there is no OS and tailored versions of say the sbrk() function have been provided to accomplish this. I'm trying to find good examples of this being achieved to maybe try a proof of concept before I jump into writing my own.

Is it possible to use dlmalloc in a system such as mine ?

If yes can anyone point me to a relevant resource ? (not found many that help me)

Is it better to go and write my own malloc that is tailored to my needs ?

And apologies most of my research so far has been on designing a malloc not using doug's which is a different challenge. Guess I'm trying to find out is looking into this route in more depth a waste of time.

Edit:

moral of the story: looking at dlmalloc in my case pointless.

  • Do you really need dynamic memory allocation in your embedded application? – Jabberwocky Dec 22 '15 at 10:09
  • 2
    What are the actual characteristics of the allocations you need to support? By being more specific perhaps you/we can come up with a more ideal solution. – John Zwinck Dec 22 '15 at 10:09
  • 2
    It greatly depends on the nature of your allocations. In many cases, it is sufficient to dynamically allocate blocks of fixed size. This would be much more easy to implement than a generic allocation method (which would have much more overhead in terms of time and memroy, need to merge free blocks etc). – Ctx Dec 22 '15 at 10:10
  • @MichaelWalz this is debatable I guess the company have been in business longer than I'm on the planet and survived without it but they have been tailoring applications for each customer. A few years before I got there work started on a more generic architecture and the stumbling block that myself and designer of this architecture see is no dynamic memory allocator. To achieve the truly generic I believe it is needed. –  Dec 22 '15 at 10:18
  • You shouldn't use malloc on bare metal MCU applications because it [doesn't make any sense](http://electronics.stackexchange.com/questions/171257/realloc-wasting-lots-of-space-in-my-mcu/171581#171581), simple as that. – Lundin Dec 22 '15 at 10:20
  • @JohnZwinck you very correct I should of been clearer. The memory that will be allocated for the most part is fairly well defined in respect to size ( a handful of structures that make up the architecture of the app ). The user will however be defining text strings of different lengths with which fragmentation is a worry. All of this is done outside critical interrupts so speed isn't the most important factor but it cannot be very slow either. –  Dec 22 '15 at 10:26
  • 2
    @Shane everything you are describing can be solved without dynamic allocation. What do you gain from simply not using some of your memory? – Magisch Dec 22 '15 at 10:29
  • 1
    You could also implement a simple [buddy memory allocator](https://en.wikipedia.org/wiki/Buddy_memory_allocation), for details see Knuth §2.5. – fuz Dec 22 '15 at 10:32

1 Answers1

4

For your situation your own implementation of malloc or dlmalloc is definately possible, but not advisable.

On very low level embedded systems, bare metal MCU's etc, using malloc is pointless.

You will be running your app and only your app on them, you know how much memory you have and can use, and you are fully able to tailor your program to fit such needs. With malloc you save memory, but that is pointless here. If your program at its highest memory usage does not exceed the memory available on the device, and your program is the only one running, there is no reason to use malloc, and no reason to let any memory go unused.

tl;dr Its possible but exceedingly pointless.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • Do you mean dlmalloc or dynamic memory allocation in all forms? and I might edit my question to add more specifics to my app but memory allocation at runtime is most definitely not pointless in my case. –  Dec 22 '15 at 11:17
  • sorry you stated my own disregard the comment above –  Dec 22 '15 at 11:19