L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
examples/libs/l4re/c++/shared_ds/ds_clnt.cc

Sharing memory between applications, client side.

Sharing memory between applications, client side.

/*
* (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
* Alexander Warg <warg@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*/
#include <l4/re/util/cap_alloc> // L4::Cap
#include <l4/re/dataspace> // L4Re::Dataspace
#include <l4/re/rm> // L4::Rm
#include <l4/re/env> // L4::Env
#include <l4/sys/cache.h>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include "interface.h"
int main()
{
/*
* Try to get server interface cap.
*/
L4::Cap<My_interface> svr = L4Re::Env::env()->get_cap<My_interface>("shm");
if (!svr.is_valid())
{
printf("Could not get the server capability\n");
return 1;
}
/*
* Alloc data space cap slot
*/
if (!ds.is_valid())
{
printf("Could not get capability slot!\n");
return 1;
}
/*
* Alloc server notifier IRQ cap slot
*/
if (!irq.is_valid())
{
printf("Could not get capability slot!\n");
return 1;
}
/*
* Request shared data-space cap.
*/
if (svr->get_shared_buffer(ds, irq))
{
printf("Could not get shared memory dataspace!\n");
return 1;
}
/*
* Attach to arbitrary region
*/
char *addr = 0;
int err = L4Re::Env::env()->rm()->attach(&addr, ds->size(),
if (err < 0)
{
printf("Error attaching data space: %s\n", l4sys_errtostr(err));
return 1;
}
printf("Content: %s\n", addr);
// wait a bit for the demo effect
printf("Sleeping a bit...\n");
sleep(1);
/*
* Fill in new stuff
*/
memset(addr, 0, ds->size());
char const * const msg = "Hello from client, too!";
printf("Setting new content in shared memory\n");
snprintf(addr, strlen(msg)+1, msg);
l4_cache_clean_data((unsigned long)addr,
(unsigned long)addr + strlen(msg) + 1);
// notify the server
irq->trigger();
/*
* Detach region containing addr, result should be Detached_ds (other results
* only apply if we split regions etc.).
*/
err = L4Re::Env::env()->rm()->detach(addr, 0);
if (err)
printf("Failed to detach region\n");
/* Free objects and capabilties, just for completeness. */
L4Re::Util::cap_alloc.free(ds, L4Re::This_task);
L4Re::Util::cap_alloc.free(irq, L4Re::This_task);
return 0;
}
Interface for memory-like objects.
Definition dataspace:64
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:103
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:127
L4::Cap< T > get_cap(char const *name, unsigned l) const noexcept
Get the capability selector for the object named name.
Definition env:204
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition capability.h:57
C++ interface for capabilities.
Definition capability.h:219
C++ Irq interface, see IRQs for the C interface.
Definition irq:132
Dataspace interface.
Environment interface.
int l4_cache_clean_data(unsigned long start, unsigned long end) L4_NOTHROW
Cache clean a range in D-cache; writes back to PoC.
Definition cache.h:81
_Cap_alloc & cap_alloc
Capability allocator.
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
Definition ipc_types:659
Region mapper interface.
@ RW
Readable and writable region.
Definition rm:150
@ Search_addr
Search for a suitable address range.
Definition rm:125
Capability allocator.