Hello
I recently stumbled over a missing placement new operator in the Genode
framework. I needed an object to be shared between a client and a
server. The server should construct the object. The client should
connect to the server, and in response receive a capability allowing the
client to access the data held by the object. The class defining the
object provides a constructor to do some necessary initialisation of the
object. It looks like this:
class Object {
...
Object(...) : ... { ... }
};
The chosen approach is to create a dataspace capability with the size of
the object. On the server side the object is instantiated:
Dataspace_capability cap = env()->ram_session()->alloc(sizeof(Object));
Object *obj = static_cast<Object *>(env()->rm_session()->attach(cap));
However, this does not work as expected, since this way Object's
constructor is never called. To achieve this applying the new operator
is required:
Dataspace_capability cap = env()->ram_session()->alloc(sizeof(Object));
Object *obj = new(env()->rm_session()->attach(cap)) Object();
However, this does not work either, because it leads to a type error
with the placement parameter. The reason is that the only publicly
available placement new operator is:
void *operator new(Genode::size_t size, Genode::Allocator
*allocator)
And worse, its implementation tries to effectively allocate the object's
space, something which is not wanted in the given context. What is
needed here is a new operator which just returns the placement parameter
as the address of the object so that its constructor is invoked when
applying the operator. Something like this:
void *operator new(Genode::size_t size, void *addr) {
return addr; }
I found such an operator locally defined in some Genode source files
(allocator_avl.cc for instance). I wonder why such an operator is not
publicly available although its usefulness is obvious. For the time
being I created a local inplementation for my purpose by cloning it from
an exisiting one. It would be helpful to have it generally thru a system
include file.
Regards
Frank