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
*/
L4::Cap<L4Re::Dataspace> ds = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>();
if (!ds.is_valid())
{
printf("Could not get capability slot!\n");
return 1;
}
/*
* Alloc server notifier IRQ cap slot
*/
L4::Cap<L4::Irq> irq = L4Re::Util::cap_alloc.alloc<L4::Irq>();
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(),
L4Re::Rm::F::Search_addr | L4Re::Rm::F::RW,
L4::Ipc::make_cap_rw(ds));
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;
}
L4Re::Dataspace
Interface for memory-like objects.
Definition dataspace:53
L4Re::Env::env
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4Re::Env::rm
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:120
L4Re::Env::get_cap
L4::Cap< T > get_cap(char const *name, unsigned l) const noexcept
Get the capability selector for the object named name.
Definition env:197
L4::Cap_base::is_valid
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition capability.h:57
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
L4::Irq
C++ Irq interface, see IRQs for the C interface.
Definition irq:121
dataspace
Dataspace interface.
env
Environment interface.
l4_cache_clean_data
L4_BEGIN_DECLS 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:70
L4Re::Util::cap_alloc
_Cap_alloc & cap_alloc
Capability allocator.
L4::Ipc::make_cap_rw
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:795
rm
Region mapper interface.
L4Re::Rm::F::RW
@ RW
Readable and writable region.
Definition rm:139
L4Re::Rm::F::Search_addr
@ Search_addr
Search for a suitable address range.
Definition rm:114
cap_alloc
Capability allocator.