genode/repos/base/src/core/io_mem_session_component.cc
Norman Feske 6b289a1423 base/core: use references instead of pointers
This patch replaces the former prominent use of pointers by references
wherever feasible. This has the following benefits:

* The contract between caller and callee becomes more obvious. When
  passing a reference, the contract says that the argument cannot be
  a null pointer. The caller is responsible to ensure that. Therefore,
  the use of reference eliminates the need to add defensive null-pointer
  checks at the callee site, which sometimes merely exist to be on the
  safe side. The bottom line is that the code becomes easier to follow.

* Reference members must be initialized via an object initializer,
  which promotes a programming style that avoids intermediate object-
  construction states. Within core, there are still a few pointers
  as member variables left though. E.g., caused by the late association
  of 'Platform_thread' objects with their 'Platform_pd' objects.

* If no pointers are present as member variables, we don't need to
  manually provide declarations of a private copy constructor and
  an assignment operator to avoid -Weffc++ errors "class ... has
  pointer data members [-Werror=effc++]".

This patch also changes a few system bindings on NOVA and Fiasco.OC,
e.g., the return value of the global 'cap_map' accessor has become a
reference. Hence, the patch touches a few places outside of core.

Fixes #3135
2019-02-12 10:33:13 +01:00

108 lines
3.1 KiB
C++

/*
* \brief Core implementation of the IO_MEM session interface
* \author Christian Helmuth
* \date 2006-08-01
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#include <util/string.h>
#include <util/arg_string.h>
#include <root/root.h>
#include <dataspace_component.h>
#include <io_mem_session_component.h>
#include <base/allocator_avl.h>
#include "util.h"
using namespace Genode;
Io_mem_session_component::Dataspace_attr
Io_mem_session_component::_prepare_io_mem(const char *args,
Range_allocator &ram_alloc)
{
addr_t req_base = Arg_string::find_arg(args, "base").ulong_value(0);
size_t req_size = Arg_string::find_arg(args, "size").ulong_value(0);
/* align base and size on page boundaries */
addr_t end = align_addr(req_base + req_size, get_page_size_log2());
addr_t base = req_base & ~(get_page_size() - 1);
size_t size = end - base;
_cacheable = UNCACHED;
Arg a = Arg_string::find_arg(args, "wc");
if (a.valid() && a.bool_value(0))
_cacheable = WRITE_COMBINED;
/* check for RAM collision */
int ret;
if ((ret = ram_alloc.remove_range(base, size))) {
error("I/O memory ", Hex_range<addr_t>(base, size), " "
"used by RAM allocator (", ret, ")");
return Dataspace_attr();
}
/* allocate region */
switch (_io_mem_alloc.alloc_addr(req_size, req_base).value) {
case Range_allocator::Alloc_return::RANGE_CONFLICT:
error("I/O memory ", Hex_range<addr_t>(req_base, req_size), " not available");
return Dataspace_attr();
case Range_allocator::Alloc_return::OUT_OF_METADATA:
error("I/O memory allocator ran out of meta data");
return Dataspace_attr();
case Range_allocator::Alloc_return::OK: break;
}
/* request local mapping */
addr_t local_addr = _map_local(base, size);
return Dataspace_attr(size, local_addr, base, _cacheable, req_base);
}
Io_mem_session_component::Io_mem_session_component(Range_allocator &io_mem_alloc,
Range_allocator &ram_alloc,
Rpc_entrypoint &ds_ep,
const char *args)
:
_io_mem_alloc(io_mem_alloc),
_ds(_prepare_io_mem(args, ram_alloc)),
_ds_ep(ds_ep)
{
if (!_ds.valid()) {
error("Local MMIO mapping failed!");
_ds_cap = Io_mem_dataspace_capability();
throw Service_denied();
}
_ds_cap = static_cap_cast<Io_mem_dataspace>(_ds_ep.manage(&_ds));
}
Io_mem_session_component::~Io_mem_session_component()
{
/* dissolve IO_MEM dataspace from service entry point */
_ds_ep.dissolve(&_ds);
/* flush local mapping of IO_MEM */
_unmap_local(_ds.core_local_addr(), _ds.size());
/*
* The Dataspace will remove itself from all RM sessions when its
* destructor is called. Thereby, it will get unmapped from all RM
* clients that currently have the dataspace attached.
*/
/* free region in IO_MEM allocator */
_io_mem_alloc.free(reinterpret_cast<void *>(_ds.req_base));
}