malloc vs Paritions

Chris Johns cjohns at cybertec.com.au
Wed May 22 12:32:31 UTC 2002


Kiran C wrote:
> 
> How are memory partitions different from malloc?
>

Malloc allows variable size allocation, while the partition manager
creates fixed size partitions. I suggest you look at the RTEMS C User
manual.

> When should memory paritions be used  and when should malloc be used.

Memory partitions exist in many forms under different names. For example
mbufs in the BSD network stack are a kind of partition system as they
are fixed size buffers. Fixed size buffers are great for networking
software or object pools where you are prepared to trade possible memory
waste for performance.

Malloc is great for applications that have some form of user
configuration that is defined when the software is created. Many tiny
malloc allocations can result in fragmentation of the heap the malloc
allocator uses.

> What are the advantages and disadvantages of using  one of them?

The answer depends on what the use is. For example the BSD stack in
RTEMS uses malloc to allocate memory for socket data when a socket is
open while the network traffic travels in mbufs that are preallocated
when the stack is initialised. Both live together.

> I have heard the in embedded development all memory is preallocated...
> why is this so?

Again this depends on the application and varies. Typically performance
is the main issue, plus the fact you only have a fixed amount of memory.
Pre-allocating make a faster system.

> Any such preallocation will be a wastage of system resource,
> take for example I need to process some user supplied data whose size is un
> known to me
> at compile time but known to me at run time....

This seems no different to a network stack to me. In the BSD stack they
chain buffers together depending on the amount of memory to move. The
size of the buffer is a trade off of the size verses the performance to
chain buffers.

> The which is a better option ...malloc the required size 'or'
> use a memory parition of with the worst case size.

Performance and fragmentation are usually the main issue with malloc. 

-- 
 Chris Johns, cjohns at cybertec.com.au



More information about the users mailing list