2016-11-06 13:23:28 +00:00
|
|
|
/*
|
|
|
|
* \brief ID name space
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2016-10-10
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2016-2017 Genode Labs GmbH
|
2016-11-06 13:23:28 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-11-06 13:23:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__BASE__ID_SPACE_H_
|
|
|
|
#define _INCLUDE__BASE__ID_SPACE_H_
|
|
|
|
|
|
|
|
#include <util/noncopyable.h>
|
2019-08-16 09:39:34 +00:00
|
|
|
#include <util/meta.h>
|
2016-11-06 13:23:28 +00:00
|
|
|
#include <base/lock.h>
|
|
|
|
#include <base/log.h>
|
|
|
|
#include <util/avl_tree.h>
|
|
|
|
|
|
|
|
namespace Genode { template <typename T> class Id_space; }
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Genode::Id_space : public Noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
struct Id
|
|
|
|
{
|
|
|
|
unsigned long value;
|
|
|
|
|
|
|
|
bool operator == (Id const &other) const { return value == other.value; }
|
|
|
|
|
|
|
|
void print(Output &out) const { Genode::print(out, value); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Out_of_ids : Exception { };
|
|
|
|
class Conflicting_id : Exception { };
|
|
|
|
|
2018-01-24 13:02:35 +00:00
|
|
|
class Element : public Avl_node<Element>
|
2016-11-06 13:23:28 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
T &_obj;
|
|
|
|
Id_space &_id_space;
|
|
|
|
Id _id { 0 };
|
|
|
|
|
|
|
|
friend class Id_space;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the tree for the element with the given ID
|
|
|
|
*/
|
|
|
|
Element *_lookup(Id id)
|
|
|
|
{
|
|
|
|
if (id.value == _id.value) return this;
|
|
|
|
|
|
|
|
Element *e = Avl_node<Element>::child(id.value > _id.value);
|
|
|
|
|
|
|
|
return e ? e->_lookup(id) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ARG, typename FUNC>
|
|
|
|
void _for_each(FUNC const &fn) const
|
|
|
|
{
|
|
|
|
if (Avl_node<Element>::child(Avl_node_base::LEFT))
|
2018-10-25 06:38:06 +00:00
|
|
|
Avl_node<Element>::child(Avl_node_base::LEFT)->template _for_each<ARG>(fn);
|
2016-11-06 13:23:28 +00:00
|
|
|
|
|
|
|
fn(static_cast<ARG &>(_obj));
|
|
|
|
|
|
|
|
if (Avl_node<Element>::child(Avl_node_base::RIGHT))
|
2018-10-25 06:38:06 +00:00
|
|
|
Avl_node<Element>::child(Avl_node_base::RIGHT)->template _for_each<ARG>(fn);
|
2016-11-06 13:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \throw Out_of_ids ID space is exhausted
|
|
|
|
*/
|
|
|
|
Element(T &obj, Id_space &id_space)
|
|
|
|
:
|
|
|
|
_obj(obj), _id_space(id_space)
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_id_space._lock);
|
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 14:42:15 +00:00
|
|
|
_id = id_space._unused_id();
|
2016-11-06 13:23:28 +00:00
|
|
|
_id_space._elements.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \throw Conflicting_id 'id' is already present in ID space
|
|
|
|
*/
|
|
|
|
Element(T &obj, Id_space &id_space, Id_space::Id id)
|
|
|
|
:
|
|
|
|
_obj(obj), _id_space(id_space), _id(id)
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_id_space._lock);
|
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 14:42:15 +00:00
|
|
|
_id_space._check_conflict(id);
|
2016-11-06 13:23:28 +00:00
|
|
|
_id_space._elements.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Element()
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_id_space._lock);
|
|
|
|
_id_space._elements.remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avl_node interface
|
|
|
|
*/
|
|
|
|
bool higher(Element *other) { return other->_id.value > _id.value; }
|
|
|
|
|
|
|
|
Id id() const { return _id; }
|
|
|
|
|
|
|
|
void print(Output &out) const { Genode::print(out, _id); }
|
|
|
|
};
|
|
|
|
|
|
|
|
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 14:42:15 +00:00
|
|
|
Lock mutable _lock { }; /* protect '_elements' and '_cnt' */
|
|
|
|
Avl_tree<Element> _elements { };
|
2016-11-06 13:23:28 +00:00
|
|
|
unsigned long _cnt = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return ID that does not exist within the ID space
|
|
|
|
*
|
|
|
|
* \return ID assigned to the element within the ID space
|
|
|
|
* \throw Out_of_ids
|
|
|
|
*/
|
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 14:42:15 +00:00
|
|
|
Id _unused_id()
|
2016-11-06 13:23:28 +00:00
|
|
|
{
|
|
|
|
unsigned long _attempts = 0;
|
|
|
|
for (; _attempts < ~0UL; _attempts++, _cnt++) {
|
|
|
|
|
|
|
|
Id const id { _cnt };
|
|
|
|
|
|
|
|
/* another attempt if is already in use */
|
|
|
|
if (_elements.first() && _elements.first()->_lookup(id))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
throw Out_of_ids();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if ID is already in use
|
|
|
|
*
|
|
|
|
* \throw Conflicting_id
|
|
|
|
*/
|
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 14:42:15 +00:00
|
|
|
void _check_conflict(Id id)
|
2016-11-06 13:23:28 +00:00
|
|
|
{
|
|
|
|
if (_elements.first() && _elements.first()->_lookup(id))
|
|
|
|
throw Conflicting_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
class Unknown_id : Exception { };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply functor 'fn' to each ID present in the ID space
|
|
|
|
*
|
|
|
|
* \param ARG argument type passed to 'fn', must be convertible
|
|
|
|
* from 'T' via a 'static_cast'
|
|
|
|
*
|
|
|
|
* This function is called with the ID space locked. Hence, it is not
|
|
|
|
* possible to modify the ID space from within 'fn'.
|
|
|
|
*/
|
|
|
|
template <typename ARG, typename FUNC>
|
|
|
|
void for_each(FUNC const &fn) const
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
|
|
|
if (_elements.first())
|
|
|
|
_elements.first()->template _for_each<ARG>(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply functor 'fn' to object with given ID
|
|
|
|
*
|
|
|
|
* See 'for_each' for a description of the 'ARG' argument.
|
|
|
|
*
|
|
|
|
* \throw Unknown_id
|
|
|
|
*/
|
|
|
|
template <typename ARG, typename FUNC>
|
2017-06-20 12:23:22 +00:00
|
|
|
auto apply(Id id, FUNC const &fn)
|
|
|
|
-> typename Trait::Functor<decltype(&FUNC::operator())>::Return_type
|
2016-11-06 13:23:28 +00:00
|
|
|
{
|
|
|
|
T *obj = nullptr;
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
|
|
|
if (!_elements.first())
|
|
|
|
throw Unknown_id();
|
|
|
|
|
|
|
|
if (Element *e = _elements.first()->_lookup(id))
|
|
|
|
obj = &e->_obj;
|
|
|
|
}
|
|
|
|
if (obj)
|
2017-06-20 12:23:22 +00:00
|
|
|
return fn(static_cast<ARG &>(*obj));
|
2016-11-06 13:23:28 +00:00
|
|
|
else
|
|
|
|
throw Unknown_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply functor 'fn' to an arbitrary ID present in the ID space
|
|
|
|
*
|
|
|
|
* See 'for_each' for a description of the 'ARG' argument.
|
|
|
|
*
|
|
|
|
* The functor is called with a reference to the managed object as
|
|
|
|
* argument. This method is designated for the destruction of ID
|
|
|
|
* spaces. It allows the caller to remove all IDs by subseqently
|
|
|
|
* calling this function and destructing the object in 'fn'.
|
|
|
|
*
|
|
|
|
* \return true if 'fn' was applied, or
|
|
|
|
* false if the ID space is empty.
|
|
|
|
*/
|
|
|
|
template <typename ARG, typename FUNC>
|
|
|
|
bool apply_any(FUNC const &fn)
|
|
|
|
{
|
|
|
|
T *obj = nullptr;
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
|
|
|
if (_elements.first())
|
|
|
|
obj = &_elements.first()->_obj;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fn(static_cast<ARG &>(*obj));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Id_space()
|
|
|
|
{
|
|
|
|
if (_elements.first())
|
|
|
|
error("ID space not empty at destruction time");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__BASE__ID_SPACE_H_ */
|