[okl4-developer] Strange thing in pingpong example

Josh Matthews jmatthews at ok-labs.com
Sat Jan 17 07:47:58 EST 2009


Hi Alexander,

On Thu, January 15, 2009 3:26 am, Alexander Valitov wrote:

> Now I'm playing around with OKL4 3.0. I've been trying to get into
> details of pingpong example and have found one strange thing that I do
> not understand.
>
> It is about variables in main() function defined as:
>   struct okl4_utcb_item *utcb_item[THREADS];
>   struct okl4_kcap_item *kcap_item[THREADS];
<..>
> From the first look it's just waste of memory and CPU time. Because
> memory allocation performed by malloc() function consumes not only some
> CPU time and required amount of memory but also some additional memory so
> future deallocation will be possible. If we have these 'descriptors'
> allocated on stack or data section as follow:
>   struct okl4_utcb_item utcb_item[THREADS];
>   struct okl4_kcap_item kcap_item[THREADS];
> It will save us 4 calls to malloc() and 4*4=16 bytes plus additional
> memory consumed by malloc() structures.
>
> Maybe there is some reason that I do not see for why I'd like to have
> these descriptors allocated from heap?

There's no fundamental reason it was done this way, and your analysis
above of the overhead of using malloc is correct. I think perhaps it was
just that the example wasn't focused on performance or memory overhead -
your suggestion of using the stack or data section to store these objects
will certainly work (obviously you'll just need to modify the few lines
that use these structures under the assumption they are pointers to use
the address of the structure instead).

Best regards,

Josh




More information about the Developer mailing list