2014-02-07 13:49:21 +00:00
|
|
|
/*
|
|
|
|
* \brief Utility for the manual placement of objects
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2014-02-07
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-02-07 13:49:21 +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.
|
2014-02-07 13:49:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__CONSTRUCT_AT_H_
|
|
|
|
#define _INCLUDE__UTIL__CONSTRUCT_AT_H_
|
|
|
|
|
|
|
|
#include <base/stdint.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 17:07:09 +00:00
|
|
|
#include <base/log.h>
|
2014-02-07 13:49:21 +00:00
|
|
|
|
|
|
|
namespace Genode {
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2014-02-07 13:49:21 +00:00
|
|
|
template <typename T, typename... ARGS>
|
|
|
|
static inline T *construct_at(void *, ARGS &&...);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct object of given type at a specific location
|
|
|
|
*
|
|
|
|
* \param T object type
|
|
|
|
* \param at desired object location
|
|
|
|
* \param args list of arguments for the object constructor
|
|
|
|
*
|
|
|
|
* \return typed object pointer
|
|
|
|
*
|
|
|
|
* We use move semantics (ARGS &&) because otherwise the compiler would create
|
|
|
|
* a temporary copy of all arguments that have a reference type and use a
|
|
|
|
* reference to this copy instead of the original within this function.
|
|
|
|
*
|
|
|
|
* There is a slight difference between the object that is constructed by this
|
|
|
|
* function and a common object of the given type. If the destructor of the
|
|
|
|
* given type or of any base of the given type is virtual, the vtable of the
|
|
|
|
* returned object references an empty delete(void *) operator for that
|
|
|
|
* destructor. However, this shouldn't be a problem as an object constructed by
|
|
|
|
* this function should never get destructed implicitely or through a delete
|
|
|
|
* expression.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... ARGS>
|
|
|
|
static inline T * Genode::construct_at(void *at, ARGS &&... args)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Utility to equip an existing type 'T' with a placement new operator
|
|
|
|
*/
|
|
|
|
struct Placeable : T
|
|
|
|
{
|
|
|
|
Placeable(ARGS &&... args) : T(args...) { }
|
|
|
|
|
2016-09-15 12:40:37 +00:00
|
|
|
void * operator new (__SIZE_TYPE__, void *ptr) { return ptr; }
|
2014-02-07 13:49:21 +00:00
|
|
|
void operator delete (void *, void *) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Standard delete operator
|
|
|
|
*
|
|
|
|
* As we explicitely define one version of the delete operator, the
|
|
|
|
* compiler won't implicitely define any delete version for this class.
|
|
|
|
* But if type T has a virtual destructor, the compiler implicitely
|
|
|
|
* defines a 'virtual ~Placeable()' which needs the following operator.
|
|
|
|
*/
|
|
|
|
void operator delete (void *)
|
|
|
|
{
|
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 17:07:09 +00:00
|
|
|
error("cxx: Placeable::operator delete (void *) not supported.");
|
2014-02-07 13:49:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the args input to this function contains rvalues, the compiler would
|
|
|
|
* use the according rvalue references as lvalues at the following call if
|
|
|
|
* we don't cast them back to rvalue references explicitely. We can not use
|
|
|
|
* lvalues here because the compiler can not bind them to rvalue references
|
|
|
|
* as expected by Placeable.
|
|
|
|
*/
|
|
|
|
return new (at) Placeable(static_cast<ARGS &&>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__CONSTRUCT_AT_H_ */
|