2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Reference-counting smart pointer
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2011-02-17
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2011-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NOUX__SHARED_POINTER_PTR_H_
|
|
|
|
#define _NOUX__SHARED_POINTER_PTR_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/lock.h>
|
|
|
|
#include <base/allocator.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* Noux includes */
|
|
|
|
#include <noux_session/sysio.h>
|
|
|
|
|
|
|
|
namespace Noux {
|
2017-01-06 12:12:39 +01:00
|
|
|
class Reference_counter;
|
2011-12-22 16:19:25 +01:00
|
|
|
class Shared_pointer_base;
|
2017-01-06 12:12:39 +01:00
|
|
|
template <typename T> class Shared_pointer;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
class Noux::Reference_counter
|
|
|
|
{
|
|
|
|
private:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2019-01-18 16:18:19 +01:00
|
|
|
Lock _lock { };
|
2017-01-06 12:12:39 +01:00
|
|
|
long _value;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
friend class Shared_pointer_base;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
void _inc_ref_count()
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
_value++;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
/**
|
|
|
|
* \return reference counter after decrement
|
|
|
|
*/
|
|
|
|
long _dec_ref_count()
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
return --_value;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
public:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
Reference_counter() : _value(0) { }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
class Noux::Shared_pointer_base
|
|
|
|
{
|
|
|
|
protected:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
Reference_counter *_ref_counter;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
Shared_pointer_base(Reference_counter *ref_counter)
|
|
|
|
: _ref_counter(ref_counter) { }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
void _inc_ref_count() {
|
|
|
|
if (_ref_counter) _ref_counter->_inc_ref_count(); }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
bool _dec_ref_count() {
|
|
|
|
return _ref_counter && (_ref_counter->_dec_ref_count() == 0); }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
long count() const { return _ref_counter ? _ref_counter->_value : -99; }
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
template <typename T>
|
|
|
|
class Noux::Shared_pointer : public Shared_pointer_base
|
|
|
|
{
|
|
|
|
private:
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
T *_ptr;
|
|
|
|
Allocator *_alloc;
|
2012-03-21 14:31:39 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
void _dec_ref_count()
|
|
|
|
{
|
|
|
|
if (Shared_pointer_base::_dec_ref_count()) {
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
destroy(_alloc, _ptr);
|
|
|
|
_ptr = 0;
|
|
|
|
_alloc = 0;
|
|
|
|
_ref_counter = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
2017-01-06 12:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Shared_pointer() : Shared_pointer_base(0), _ptr(0), _alloc(0) { }
|
|
|
|
|
|
|
|
Shared_pointer(T *ptr, Allocator &alloc)
|
|
|
|
: Shared_pointer_base(ptr), _ptr(ptr), _alloc(&alloc)
|
|
|
|
{
|
|
|
|
_inc_ref_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared_pointer(Shared_pointer const & from)
|
|
|
|
:
|
|
|
|
Shared_pointer_base(from._ref_counter),
|
|
|
|
_ptr(from._ptr), _alloc(from._alloc)
|
|
|
|
{
|
|
|
|
_inc_ref_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shared_pointer & operator=(const Shared_pointer& from)
|
|
|
|
{
|
|
|
|
/* check for self assignment */
|
|
|
|
if (_ptr == from._ptr)
|
|
|
|
return *this;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
/* forget about original pointed-to object */
|
|
|
|
_dec_ref_count();
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
_ref_counter = from._ref_counter;
|
|
|
|
_ptr = from._ptr;
|
|
|
|
_alloc = from._alloc;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
/* account for newly assigned pointed-to object */
|
|
|
|
_inc_ref_count();
|
2012-02-24 23:32:08 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
~Shared_pointer()
|
|
|
|
{
|
|
|
|
_dec_ref_count();
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
T * operator -> () { return _ptr; }
|
|
|
|
T const* operator -> () const { return _ptr; }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
operator bool () const { return _ptr != 0; }
|
2012-05-08 12:16:13 +02:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
bool operator== (const Shared_pointer &other)
|
|
|
|
{
|
|
|
|
return (_ptr == other._ptr);
|
|
|
|
}
|
2013-10-17 12:48:45 +02:00
|
|
|
|
2017-01-06 12:12:39 +01:00
|
|
|
template<typename To>
|
|
|
|
Shared_pointer<To> dynamic_pointer_cast()
|
|
|
|
{
|
|
|
|
return Shared_pointer<To>(dynamic_cast<To *>(_ptr), _alloc);
|
|
|
|
}
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
#endif /* _NOUX__SHARED_POINTER_H_ */
|