genode/repos/os/include/os/child_policy_dynamic_rom.h
Norman Feske eba9c15746 Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:

* A class with virtual functions can no longer publicly inherit base
  classed without a vtable. The inherited object may either be moved
  to a member variable, or inherited privately. The latter would be
  used for classes that inherit 'List::Element' or 'Avl_node'. In order
  to enable the 'List' and 'Avl_tree' to access the meta data, the
  'List' must become a friend.

* Instead of adding a virtual destructor to abstract base classes,
  we inherit the new 'Interface' class, which contains a virtual
  destructor. This way, single-line abstract base classes can stay
  as compact as they are now. The 'Interface' utility resides in
  base/include/util/interface.h.

* With the new warnings enabled, all member variables must be explicitly
  initialized. Basic types may be initialized with '='. All other types
  are initialized with braces '{ ... }' or as class initializers. If
  basic types and non-basic types appear in a row, it is nice to only
  use the brace syntax (also for basic types) and align the braces.

* If a class contains pointers as members, it must now also provide a
  copy constructor and assignment operator. In the most cases, one
  would make them private, effectively disallowing the objects to be
  copied. Unfortunately, this warning cannot be fixed be inheriting
  our existing 'Noncopyable' class (the compiler fails to detect that
  the inheriting class cannot be copied and still gives the error).
  For now, we have to manually add declarations for both the copy
  constructor and assignment operator as private class members. Those
  declarations should be prepended with a comment like this:

        /*
         * Noncopyable
         */
        Thread(Thread const &);
        Thread &operator = (Thread const &);

  In the future, we should revisit these places and try to replace
  the pointers with references. In the presence of at least one
  reference member, the compiler would no longer implicitly generate
  a copy constructor. So we could remove the manual declaration.

Issue #465
2018-01-17 12:14:35 +01:00

237 lines
6.1 KiB
C++

/*
* \brief Child policy helper for supplying dynamic ROM modules
* \author Norman Feske
* \date 2012-04-04
*
* \deprecated use 'Local_service::Single_session_service' combined with
* 'Dynamic_rom_session' instead
*/
/*
* Copyright (C) 2012-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.
*/
#ifndef _INCLUDE__OS__CHILD_POLICY_DYNAMIC_ROM_H_
#define _INCLUDE__OS__CHILD_POLICY_DYNAMIC_ROM_H_
#include <ram_session/ram_session.h>
#include <rom_session/rom_session.h>
#include <base/rpc_server.h>
#include <base/attached_ram_dataspace.h>
namespace Genode { class Child_policy_dynamic_rom_file; }
class Genode::Child_policy_dynamic_rom_file : public Rpc_object<Rom_session>,
public Service
{
private:
/*
* Noncopyable
*/
Child_policy_dynamic_rom_file(Child_policy_dynamic_rom_file const &);
Child_policy_dynamic_rom_file &operator = (Child_policy_dynamic_rom_file const &);
Ram_session *_ram;
Region_map &_rm;
/*
* The ROM module may be written and consumed by different threads,
* e.g., written by the main thread and consumed by the child's
* entrypoint that manages the local ROM service for handing out a
* dynamic config. Hence, the '_lock' is used to synchronize the
* 'load' and 'dataspace' methods.
*/
Lock _lock { };
/*
* We keep two dataspaces around. The foreground ('_fg') dataspace
* is the one we present to the client. While the foreground
* dataspace is in use, we perform all modifications of the data
* in the background dataspace (which is invisible to the client).
* Once the client calls 'dataspace()', we promote the old
* background dataspace to the new foreground and thereby hand out
* the former background dataspace.
*/
Attached_ram_dataspace _fg;
Attached_ram_dataspace _bg;
bool _bg_has_pending_data;
Signal_context_capability _sigh_cap { };
Rpc_entrypoint &_ep;
Rom_session_capability _rom_session_cap;
Session_label _module_name;
public:
/**
* Constructor
*
* \param ram RAM session used to allocate the backing store
* for buffering ROM module data
*
* \throw Out_of_ram
* \throw Out_of_caps
*
* If 'ram' is 0, the child policy is ineffective.
*/
Child_policy_dynamic_rom_file(Region_map &rm,
char const *module_name,
Rpc_entrypoint &ep,
Ram_session *ram)
:
Service("ROM"),
_ram(ram), _rm(rm),
_fg(*_ram, _rm, 0), _bg(*_ram, _rm, 0),
_bg_has_pending_data(false),
_ep(ep),
_rom_session_cap(_ep.manage(this)),
_module_name(module_name)
{ }
/**
* Constructor
*
* \param ram RAM session used to allocate the backing store
* for buffering ROM module data
*
* \deprecated
*
* If 'ram' is 0, the child policy is ineffective.
*/
Child_policy_dynamic_rom_file(char const *module_name,
Rpc_entrypoint &ep,
Ram_session *ram) __attribute__((deprecated))
:
Service("ROM"),
_ram(ram), _rm(*env_deprecated()->rm_session()),
_fg(*_ram, _rm, 0), _bg(*_ram, _rm, 0),
_bg_has_pending_data(false),
_ep(ep),
_rom_session_cap(_ep.manage(this)),
_module_name(module_name)
{ }
/**
* Destructor
*/
~Child_policy_dynamic_rom_file() { _ep.dissolve(this); }
/**
* Load new content into ROM module
*
* \throw Out_of_ram
* \throw Out_of_caps
* \throw Region_map::Region_conflict
* \throw Region_map::Invalid_dataspace
*/
void load(void const *data, size_t data_len)
{
Lock::Guard guard(_lock);
if (!_ram) {
Genode::error("no backing store for loading ROM data");
return;
}
/* let background buffer grow if needed */
if (_bg.size() < data_len)
_bg.realloc(_ram, data_len);
memcpy(_bg.local_addr<void>(), data, data_len);
_bg_has_pending_data = true;
if (_sigh_cap.valid())
Signal_transmitter(_sigh_cap).submit();
}
/***************************
** ROM session interface **
***************************/
Rom_dataspace_capability dataspace()
{
Lock::Guard guard(_lock);
if (!_fg.size() && !_bg_has_pending_data) {
Genode::error("no data loaded");
return Rom_dataspace_capability();
}
/*
* Keep foreground if no background exists. Otherwise, use
* old background as new foreground.
*/
if (_bg_has_pending_data) {
_fg.swap(_bg);
_bg_has_pending_data = false;
}
Dataspace_capability ds_cap = _fg.cap();
return static_cap_cast<Rom_dataspace>(ds_cap);
}
void sigh(Signal_context_capability cap) { _sigh_cap = cap; }
/***********************
** Service interface **
***********************/
void initiate_request(Session_state &session) override
{
switch (session.phase) {
case Session_state::CREATE_REQUESTED:
session.cap = _rom_session_cap;
session.phase = Session_state::AVAILABLE;
break;
case Session_state::UPGRADE_REQUESTED:
session.phase = Session_state::CAP_HANDED_OUT;
session.confirm_ram_upgrade();
break;
case Session_state::CLOSE_REQUESTED:
session.phase = Session_state::CLOSED;
break;
case Session_state::SERVICE_DENIED:
case Session_state::INSUFFICIENT_RAM_QUOTA:
case Session_state::INSUFFICIENT_CAP_QUOTA:
case Session_state::AVAILABLE:
case Session_state::CAP_HANDED_OUT:
case Session_state::CLOSED:
break;
}
}
/**********************
** Policy interface **
**********************/
Service *resolve_session_request(const char *service_name,
const char *args)
{
if (!_ram) return 0;
/* ignore session requests for non-ROM services */
if (strcmp(service_name, "ROM")) return 0;
/* drop out if request refers to another module name */
Session_label const label = label_from_args(args);
return _module_name == label.last_element() ? this : 0;
}
};
#endif /* _INCLUDE__OS__CHILD_POLICY_DYNAMIC_ROM_H_ */