genode/repos/base/src/core/include/trace/policy_registry.h

159 lines
3.3 KiB
C
Raw Normal View History

/*
* \brief Registry containing tracing policy modules
* \author Norman Feske
* \date 2013-08-12
*/
/*
* 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 _CORE__INCLUDE__TRACE__POLICY_REGISTRY_H_
#define _CORE__INCLUDE__TRACE__POLICY_REGISTRY_H_
/* Genode includes */
#include <base/lock.h>
#include <util/list.h>
namespace Genode { namespace Trace {
class Policy_owner;
class Policy;
class Policy_registry;
} }
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
2017-12-21 15:42:15 +01:00
class Genode::Trace::Policy_owner : Interface { };
class Genode::Trace::Policy : public Genode::List<Genode::Trace::Policy>::Element
{
friend class Policy_registry;
private:
Policy_owner const &_owner;
Allocator &_md_alloc;
Policy_id const _id;
Dataspace_capability _ds;
size_t const _size;
/**
* Constructor
*
* \param md_alloc allocator that holds the 'Policy' object
*/
Policy(Policy_owner const &owner, Policy_id const id,
Allocator &md_alloc, Dataspace_capability ds, size_t size)
:
_owner(owner), _md_alloc(md_alloc), _id(id), _ds(ds), _size(size)
{ }
Allocator &md_alloc() { return _md_alloc; }
bool owned_by(Policy_owner const &owner) const
{
return &_owner == &owner;
}
bool has_id(Policy_id id) const { return id == _id; }
public:
Dataspace_capability dataspace() const { return _ds; }
size_t size() const { return _size; }
};
/**
* Global policy registry
*/
class Genode::Trace::Policy_registry
{
private:
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
2017-12-21 15:42:15 +01:00
Lock _lock { };
List<Policy> _policies { };
Policy &_unsynchronized_lookup(Policy_owner const &owner, Policy_id id)
{
for (Policy *p = _policies.first(); p; p = p->next())
if (p->owned_by(owner) && p->has_id(id))
return *p;
throw Nonexistent_policy();
}
Policy *_any_policy_owned_by(Policy_owner const &owner)
{
for (Policy *p = _policies.first(); p; p = p->next())
if (p->owned_by(owner))
return p;
return nullptr;
}
public:
~Policy_registry()
{
Lock::Guard guard(_lock);
while (Policy *p = _policies.first())
_policies.remove(p);
}
void insert(Policy_owner const &owner, Policy_id const id,
Allocator &md_alloc, Dataspace_capability ds, size_t size)
{
Lock::Guard guard(_lock);
Policy &policy = *new (&md_alloc) Policy(owner, id, md_alloc, ds, size);
_policies.insert(&policy);
}
void remove(Policy_owner &owner, Policy_id id)
{
Lock::Guard guard(_lock);
for (Policy *p = _policies.first(); p; ) {
Policy *tmp = p;
p = p->next();
if (tmp->owned_by(owner) && tmp->has_id(id)) {
_policies.remove(tmp);
destroy(&tmp->md_alloc(), tmp);
}
}
}
void destroy_policies_owned_by(Policy_owner const &owner)
{
Lock::Guard guard(_lock);
while (Policy *p = _any_policy_owned_by(owner)) {
_policies.remove(p);
destroy(&p->md_alloc(), p);
}
}
Dataspace_capability dataspace(Policy_owner &owner, Policy_id id)
{
Lock::Guard guard(_lock);
return _unsynchronized_lookup(owner, id).dataspace();
}
size_t size(Policy_owner &owner, Policy_id id)
{
Lock::Guard guard(_lock);
return _unsynchronized_lookup(owner, id).size();
}
};
#endif /* _CORE__INCLUDE__TRACE__POLICY_REGISTRY_H_ */