Hello,
My program runs two kinds of RPC server in a single process. Do I need to get sliced heap for each RPC server or can I just share one sliced heap object between the rpc server?
For example, Sliced_heap sliced_heap1(env()->ram_session(), env()->rm_session()); Sliced_heap sliced_heap2(env()->ram_session(), env()->rm_session()); ... Server1_root_component server1_root(&server1_ep, &sliced_heap1, ....); Server2_root_component server1_root(&server2_ep, &sliced_heap2, ....);
OR Sliced_heap sliced_heap(env()->ram_session(), env()->rm_session()); ... Server1_root_component server1_root(&server1_ep, &sliced_heap, ....); Server2_root_component server1_root(&server2_ep, &sliced_heap, ....);
one 'Sliced_heap' is enough.
A sliced heap uses one distinct dataspace for each allocation. So each allocated memory object can be freed independently. This is suitable when using the allocator for coarse grained allocations (e.g., session-component objects). But for allocating small objects, this allocator should not be used because it would waste almost one memory page per allocation. In this case, I recommend to create a heap partition per session (hosting a 'Heap' object as member variable of the 'Session_component') and use this session-local heap instance for small allocations.
My another question is : Do I need to use Synchronized_allocator to allocate a memory from heap (for object creation) when running multiple thread? Or is it ok to call (env()->heap()) for object creation in heap?
The allocator returned by 'env()->heap()' is an instance of 'Genode::Heap', which is thread-safe.
Generally, it is ok to use (env()->heap()) for object creation. However, please keep in mind that 'env()->heap()' represents the server's own resources. If a client can trigger the server to perform allocations on the client's behalf (e.g., by performing an RPC call that has an allocation as side effect), the client may be able to indirectly exhaust the memory of the server. To prevent that from happening, I recommend the use of session-local heap partitions: To have one 'Heap' and one 'Allocator_guard' per session to limit the maximum number of bytes a client can consume at the server side.
Cheers Norman