Strange thing in “pingpong“ example

3 messages Options
Embed this post
Permalink
Alexander Valitov

Strange thing in “pingpong“ example

Reply Threaded More More options
Print post
Permalink
Hello all,

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];

They are array of pointers to “kind of descriptors” for allocated UTCB and KCAP objects. For each element of array corresponding malloc() function is called allocating memory for descriptor from heap. Size of descriptors is about 4 bytes. So each call to malloc() allocates 4 bytes.

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?

Best regards, Alexander Valitov
Josh Matthews

Re: Strange thing in pingpong example

Reply Threaded More More options
Print post
Permalink
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


_______________________________________________
Developer mailing list
[hidden email]
https://lists.okl4.org/mailman/listinfo/developer
Alexander Valitov

Re: Strange thing in pingpong example

Reply Threaded More More options
Print post
Permalink
Hi Josh,

Thanks for your answer.
Josh Matthews wrote:
There's no fundamental reason it was done this way, and your analysis
above of the overhead of using malloc is correct.
That's exactly what I'd like to know.
Just had to make sure that I don't miss something important in okl4lib usage.

Best Regards,
Alexander Valitov