Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc in an embedded system without an operating system

This query is regarding allocation of memory using malloc.

Generally what we say is malloc allocates memory from heap.

Now say I have a plain embedded system(No operating system), I have normal program loaded where I do malloc in my program.

In this case where is the memory allocated from ?

like image 941
Leo Messi Avatar asked Feb 17 '12 20:02

Leo Messi


People also ask

Does malloc need OS support?

There should be an entity that tracks which addresses are free and which are not. That's OS memory management unit. malloc()/free() will then have to call OS system calls. So no OS means no malloc()/free() .

Why malloc is not used in embedded systems?

There are a number of reasons why malloc() is not generally recommended for embedded applications: The function is commonly not re-entrant (thread friendly), so using it with a real-time operating system may be challenging.

What is the alternative to malloc?

free() is used and alternate of malloc, used to deallocate memory....

Why Dynamic allocation is not used in embedded systems?

Dynamic memory allocation makes it very difficult to debug, especially with some of the limited/primitive debug tools available on embedded system. If you statically allocate stuff then you know where stuff is all the time which means it is much easier to inspect the state of something.


1 Answers

malloc() is a function that is usually implemented by the runtime-library. You are right, if you are running on top of an operating system, then malloc will sometimes (but not every time) trigger a system-call that makes the OS map some memory into your program's address space.

If your program runs without an operating system, then you can think of your program as being the operating system. You have access to all addresses, meaning you can just assign an address to a pointer, then de-reference that pointer to read/write.

Of course you have to make sure that not other parts of your program just use the same memory, so you write your own memory-manager:

To put it simply you can set-aside a range of addresses which your "memory-manager" uses to store which address-ranges are already in use (the datastructures stored in there can be as easy as a linked list or much much more complex). Then you will write a function and call it e.g. malloc() which forms the functional part of your memory-manager. It looks into the mentioned datastructure to find an address of ranges that is as long as the argument specifies and return a pointer to it.

Now, if every function in your program calls your malloc() instead of randomly writing into custom addresses you've done the first step. You can write a free()-function which will look for the pointer it is given in the mentioned datastructure, and adapts the datastructure (in the naive linked-list it would merge two links).

like image 68
Bernd Elkemann Avatar answered Sep 22 '22 18:09

Bernd Elkemann