Files
genode/repos/base/include/base/object_pool.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

186 lines
4.0 KiB
C++

/*
* \brief Object pool - map capabilities to objects
* \author Norman Feske
* \author Alexander Boettcher
* \author Stafen Kalkowski
* \date 2006-06-26
*/
/*
* 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.
*/
#ifndef _INCLUDE__BASE__OBJECT_POOL_H_
#define _INCLUDE__BASE__OBJECT_POOL_H_
#include <util/avl_tree.h>
#include <util/noncopyable.h>
#include <base/capability.h>
#include <base/weak_ptr.h>
namespace Genode { template <typename> class Object_pool; }
/**
* Map capabilities to local objects
*
* \param OBJ_TYPE object type (must be inherited from Object_pool::Entry)
*
* The local names of a capabilities are used to differentiate multiple server
* objects managed by one and the same object pool.
*/
template <typename OBJ_TYPE>
class Genode::Object_pool : Interface, Noncopyable
{
public:
class Entry : Avl_node<Entry>
{
private:
friend class Object_pool;
friend class Avl_tree<Entry>;
friend class Avl_node<Entry>;
struct Entry_lock : Weak_object<Entry_lock>, Noncopyable
{
Entry &obj;
Entry_lock(Entry &e) : obj(e) { }
~Entry_lock() {
Weak_object<Entry_lock>::lock_for_destruction(); }
};
Untyped_capability _cap { };
Entry_lock _lock { *this };
inline unsigned long _obj_id() { return _cap.local_name(); }
public:
Entry() { }
Entry(Untyped_capability cap) : _cap(cap) { }
virtual ~Entry() { }
/**
* Avl_node interface
*/
bool higher(Entry *e) { return e->_obj_id() > _obj_id(); }
void recompute() { } /* for gcc-3.4 compatibility */
/**
* Support for object pool
*/
Entry *find_by_obj_id(unsigned long obj_id)
{
if (obj_id == _obj_id()) return this;
Entry *obj = this->child(obj_id > _obj_id());
return obj ? obj->find_by_obj_id(obj_id) : 0;
}
/**
* Assign capability to object pool entry
*/
void cap(Untyped_capability c) { _cap = c; }
Untyped_capability const cap() const { return _cap; }
};
private:
Avl_tree<Entry> _tree { };
Lock _lock { };
protected:
bool empty()
{
Lock::Guard lock_guard(_lock);
return _tree.first() == nullptr;
}
public:
void insert(OBJ_TYPE *obj)
{
Lock::Guard lock_guard(_lock);
_tree.insert(obj);
}
void remove(OBJ_TYPE *obj)
{
Lock::Guard lock_guard(_lock);
_tree.remove(obj);
}
template <typename FUNC>
auto apply(unsigned long capid, FUNC func)
-> typename Trait::Functor<decltype(&FUNC::operator())>::Return_type
{
using Functor = Trait::Functor<decltype(&FUNC::operator())>;
using Object_pointer = typename Functor::template Argument<0>::Type;
using Weak_ptr = Weak_ptr<typename Entry::Entry_lock>;
using Locked_ptr = Locked_ptr<typename Entry::Entry_lock>;
Weak_ptr ptr;
{
Lock::Guard lock_guard(_lock);
Entry * entry = _tree.first() ?
_tree.first()->find_by_obj_id(capid) : nullptr;
if (entry) ptr = entry->_lock.weak_ptr();
}
{
Locked_ptr lock_ptr(ptr);
Object_pointer op = lock_ptr.valid()
? dynamic_cast<Object_pointer>(&lock_ptr->obj) : nullptr;
return func(op);
}
}
template <typename FUNC>
auto apply(Untyped_capability cap, FUNC func)
-> typename Trait::Functor<decltype(&FUNC::operator())>::Return_type
{
return apply(cap.local_name(), func);
}
template <typename FUNC>
void remove_all(FUNC func)
{
using Weak_ptr = Weak_ptr<typename Entry::Entry_lock>;
using Locked_ptr = Locked_ptr<typename Entry::Entry_lock>;
for (;;) {
OBJ_TYPE * obj;
{
Lock::Guard lock_guard(_lock);
if (!((obj = (OBJ_TYPE*) _tree.first()))) return;
Weak_ptr ptr = obj->_lock.weak_ptr();
{
Locked_ptr lock_ptr(ptr);
if (!lock_ptr.valid()) return;
_tree.remove(obj);
}
}
func(obj);
}
}
};
#endif /* _INCLUDE__BASE__OBJECT_POOL_H_ */