genode/repos/os/include/cli_monitor/ram.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

141 lines
3.1 KiB
C++

/*
* \brief RAM management
* \author Norman Feske
* \date 2013-10-14
*/
/*
* Copyright (C) 2013-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__CLI_MONITOR__RAM_H_
#define _INCLUDE__CLI_MONITOR__RAM_H_
/* Genode includes */
#include <pd_session/client.h>
namespace Cli_monitor { class Ram; }
class Cli_monitor::Ram
{
private:
typedef Genode::size_t size_t;
Genode::Pd_session &_pd;
Genode::Pd_session_capability _pd_cap;
Genode::Lock mutable _lock { };
Genode::Signal_context_capability _yield_sigh;
Genode::Signal_context_capability _resource_avail_sigh;
size_t _preserve;
void _validate_preservation()
{
if (_pd.avail_ram().value < _preserve)
Genode::Signal_transmitter(_yield_sigh).submit();
/* verify to answer outstanding resource requests too */
if (_pd.avail_ram().value > _preserve)
Genode::Signal_transmitter(_resource_avail_sigh).submit();
}
public:
struct Status
{
size_t quota, used, avail, preserve;
Status(size_t quota, size_t used, size_t avail, size_t preserve)
: quota(quota), used(used), avail(avail), preserve(preserve) { }
};
Ram(Genode::Pd_session &pd,
Genode::Pd_session_capability pd_cap,
size_t preserve,
Genode::Signal_context_capability yield_sigh,
Genode::Signal_context_capability resource_avail_sigh)
:
_pd(pd), _pd_cap(pd_cap),
_yield_sigh(yield_sigh),
_resource_avail_sigh(resource_avail_sigh),
_preserve(preserve)
{ }
size_t preserve() const
{
Genode::Lock::Guard guard(_lock);
return _preserve;
}
void preserve(size_t preserve)
{
Genode::Lock::Guard guard(_lock);
_preserve = preserve;
_validate_preservation();
}
Status status() const
{
Genode::Lock::Guard guard(_lock);
return Status(_pd.ram_quota().value, _pd.used_ram().value,
_pd.avail_ram().value, _preserve);
}
void validate_preservation()
{
Genode::Lock::Guard guard(_lock);
_validate_preservation();
}
/**
* Exception type
*/
class Transfer_quota_failed { };
/**
* \throw Transfer_quota_failed
*/
void withdraw_from(Genode::Ram_session_capability from, size_t amount)
{
using namespace Genode;
Lock::Guard guard(_lock);
try { Pd_session_client(from).transfer_quota(_pd_cap, Ram_quota{amount}); }
catch (...) { throw Transfer_quota_failed(); }
Signal_transmitter(_resource_avail_sigh).submit();
}
/**
* \throw Transfer_quota_failed
*/
void transfer_to(Genode::Ram_session_capability to, size_t amount)
{
Genode::Lock::Guard guard(_lock);
if (_pd.avail_ram().value < (_preserve + amount)) {
Genode::Signal_transmitter(_yield_sigh).submit();
throw Transfer_quota_failed();
}
try { _pd.transfer_quota(to, Genode::Ram_quota{amount}); }
catch (...) { throw Transfer_quota_failed(); }
}
size_t avail() const { return _pd.avail_ram().value; }
};
#endif /* _INCLUDE__CLI_MONITOR__RAM_H_ */