[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: [rtl] New Subject: malloc/free -> kmalloc/kfree for ctor/dtor



> As I understand new and delete operator, they call malloc and free routine
> for memory allocation and after call constructor and destructor.

According to Bjarne Stroustrup new and delete should not call malloc and free, they
should use a more efficient algorithm. Anyway this detail does not change the
substance of things.

> So if malloc and free are not reentrant, did kmalloc and kfree are ??
> I mean, because rt-task preempt the kernel, did all new and delete should
> be call in kernel context and not in rt context ?

When you ask for a block of size s, malloc walks the heap for you looking for a block
os size s or just a little bigger. Now it has found it; it has checked it and the
block is free; it is going to mark it as used but... it is preempted by another task,
calling malloc for a block of size s. What happens? **The block will be given to
both**, unless there is a mechanism - say a semaphore, I'm only guessing -  which puts
the second task to wait until the first one has completed the acquisition.As
semaphores or whatever else do not work in rt tasks, if you call malloc from an rt
task what I can imagine happens (I never tried really) is:
a) the waiting primitive crashes;
b) the waiting primitive is bypassed and the rt task gets the same block of the user
task.
What about kmalloc? Kernel is probably not pleased to wait until a user task is done,
so I guess that it has its own heap and its own protection  - or maybe no protection;
I don't know about the synchronization of kernel threads. In both cases, if you call
kmalloc from an rt task, which can preempt the kernel, in my opinion you have some
probability to crash, or to share your piece of heap with the kernel.
If I am right (mine are only hypotheses), the only way to use dynamic memory in an rt
task is to use a separate heap and homebrewed allocation/deallocation routines.

Daniele

--- [rtl] ---
For more information on Real-Time Linux see:
http://rtlinux.cs.nmt.edu/