malloc/free functions.

Norman Feske norman.feske at ...1...
Tue May 10 07:37:42 CEST 2011


Hello Chen

> I found that malloc/free functions have been implemented in at least
> three different libraries: cxx, mini_c and libc. I assume cxx is used
> for genode base and core implementations, mini_c for demos and libc for
> porting. However, I am not able to use any of them in my own

the malloc/free implementation of the cxx library is meant only for
internal use of the C++ support library (i.e., needed by libsupc++).
This implementation is private to the 'cxx' library and not visible
outside this library. This is by intention to prevent Genode components
from unknowingly depending on this kind of libc functionality. The
implementation provided by 'mini_c' is just there to support libpng and
libz that we use for the Scout tutorial browser. It is part of the
'demo' repository. I'd recommend not to use this library outside this
repository. Now for the libc, this is the official way to use
malloc/free on Genode.

The absence of malloc/free in the Genode API is by intention for two
reasons. First, Genode is C++, so using new/delete (on Genode, 'new'
takes an 'Allocator' argument, delete is replaced by 'Genode::destroy')
is the way to go for dynamic memory allocation. Second, the Genode
concept devises the use of non-anonymous memory by explicitly stating an
allocator when allocating/freeing memory. This way, resource usage can
be tracked (and limited) at a fine granularity. To support this concept,
there is an abstract 'Allocator' interface and a number of
implementations (e.g., the 'Slab', 'Allocator_avl', and 'Heap' classes).
Anyway, in some cases, anonymous memory allocations are needed. The
easiest way is:

  void *p = Genode::env()->heap()->alloc(size);

> application. When using malloc defined in mini_c or libc, I got a
> compiler error  “ reference to ‘size_t’ is ambiguous”.  It looks like
> both stdlib.h in mini_c (or libc) directory and stdint.h in base
> directory have defined “size_t” type. And I cannot get rid of the use of
> stdint.h as it has been included in many genode base and core files.

This error occurs because you are including the said C header file in
your C++ program and also stating 'using namespace Genode' in your
program. To solve this problem, you may prefer stating 'using namespace
Genode' locally within selected functions instead of stating this at the
global scope of the file. The best would be to avoid 'using namespace
Genode' altogether for files that include both libc headers and Genode
headers. Instead, use the Genode namespace prefix explicitly, wherever
needed.

> On the other hand, if I wanted to use the malloc in cxx by putting “cxx”
> into LIB in my application’s target.mk,  malloc cannot be recognized by
> the compiler.   To solve this problem, I currently have to define my own
> malloc/free by copying the implementation from cxx and it works.
> However,  I really wonder which malloc/free defined in genode should I
> use and how? By the way, the implementation of heap()->alloc() is
> thread-safe, right?

Can you please elaborate why you need malloc/free in addition to
'env()->heap()'? As you may have noticed, the 'cxx' version uses the
Genode heap as back end anyway. It is just a wrapper to make libsupc++
happy.

Cheers
Norman

-- 
Dr.-Ing. Norman Feske
Genode Labs

http://www.genode-labs.com · http://genode.org

Genode Labs GmbH · Amtsgericht Dresden · HRB 28424 · Sitz Dresden
Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth




More information about the users mailing list