genode/repos/base-hw/src/core/kernel/object.cc
Norman Feske 6b289a1423 base/core: use references instead of pointers
This patch replaces the former prominent use of pointers by references
wherever feasible. This has the following benefits:

* The contract between caller and callee becomes more obvious. When
  passing a reference, the contract says that the argument cannot be
  a null pointer. The caller is responsible to ensure that. Therefore,
  the use of reference eliminates the need to add defensive null-pointer
  checks at the callee site, which sometimes merely exist to be on the
  safe side. The bottom line is that the code becomes easier to follow.

* Reference members must be initialized via an object initializer,
  which promotes a programming style that avoids intermediate object-
  construction states. Within core, there are still a few pointers
  as member variables left though. E.g., caused by the late association
  of 'Platform_thread' objects with their 'Platform_pd' objects.

* If no pointers are present as member variables, we don't need to
  manually provide declarations of a private copy constructor and
  an assignment operator to avoid -Weffc++ errors "class ... has
  pointer data members [-Werror=effc++]".

This patch also changes a few system bindings on NOVA and Fiasco.OC,
e.g., the return value of the global 'cap_map' accessor has become a
reference. Hence, the patch touches a few places outside of core.

Fixes #3135
2019-02-12 10:33:13 +01:00

106 lines
2.3 KiB
C++

#include <kernel/object.h>
#include <kernel/pd.h>
#include <kernel/kernel.h>
#include <util/construct_at.h>
using namespace Kernel;
/************
** Object **
************/
Object::~Object()
{
for (Object_identity * oi = first(); oi; oi = first())
oi->invalidate();
}
/*********************
** Object_identity **
*********************/
void Object_identity::invalidate()
{
for (Object_identity_reference * oir = first(); oir; oir = first())
oir->invalidate();
if (_object) {
_object->remove(this);
_object = nullptr;
}
}
Object_identity::Object_identity(Object & object)
: _object(&object) { _object->insert(this); }
Object_identity::~Object_identity() { invalidate(); }
/*******************************
** Object_identity_reference **
*******************************/
Object_identity_reference *
Object_identity_reference::find(Pd &pd)
{
if (!_identity) return nullptr;
for (Object_identity_reference * oir = _identity->first();
oir; oir = oir->next())
if (&pd == &(oir->_pd)) return oir;
return nullptr;
}
Object_identity_reference *
Object_identity_reference::find(capid_t capid)
{
using Avl_node_base = Genode::Avl_node<Object_identity_reference>;
if (capid == _capid) return this;
Object_identity_reference * subtree =
Avl_node_base::child(capid > _capid);
return (subtree) ? subtree->find(capid) : nullptr;
}
Object_identity_reference * Object_identity_reference::factory(void * dst,
Pd & pd)
{
using namespace Genode;
return !_identity ?
nullptr : construct_at<Object_identity_reference>(dst, _identity, pd);
}
void Object_identity_reference::invalidate() {
if (_identity) _identity->remove(this);
_identity = nullptr;
}
Object_identity_reference::Object_identity_reference(Object_identity *oi,
Pd &pd)
: _capid(pd.capid_alloc().alloc()), _identity(oi), _pd(pd), _in_utcbs(0)
{
if (_identity) _identity->insert(this);
_pd.cap_tree().insert(this);
}
Object_identity_reference::~Object_identity_reference()
{
invalidate();
_pd.cap_tree().remove(this);
_pd.capid_alloc().free(_capid);
}
Object_identity_reference * Object_identity_reference_tree::find(capid_t id) {
return (first()) ? first()->find(id) : nullptr; }