base: Add 'construct_at' utility

This utility allows for the manual placement of objects without the need
to have a global placement new operation nor the need for type-specific
new operators.

Issue #989
This commit is contained in:
Norman Feske 2014-02-07 14:49:21 +01:00
parent cdb5030cbb
commit 6f9e15aff8
4 changed files with 131 additions and 16 deletions

View File

@ -0,0 +1,84 @@
/*
* \brief Utility for the manual placement of objects
* \author Norman Feske
* \date 2014-02-07
*/
/*
* Copyright (C) 2014 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#ifndef _INCLUDE__UTIL__CONSTRUCT_AT_H_
#define _INCLUDE__UTIL__CONSTRUCT_AT_H_
#include <base/stdint.h>
#include <base/printf.h>
namespace Genode {
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...) { }
void * operator new (size_t, void *ptr) { return ptr; }
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 *)
{
PERR("cxx: Placeable::operator delete (void *) not supported.");
}
};
/*
* 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_ */

View File

@ -14,6 +14,7 @@
#ifndef _INCLUDE__UTIL__VOLATILE_OBJECT_H_
#define _INCLUDE__UTIL__VOLATILE_OBJECT_H_
#include <util/construct_at.h>
#include <base/printf.h>
#include <base/stdint.h>
@ -39,21 +40,6 @@ class Genode::Volatile_object
{
private:
/**
* Utility to equip an existing type 'T' with a placement new operator
*/
template <typename T>
struct Placeable : T
{
template <typename... ARGS>
Placeable(ARGS &&... args)
:
T(args...)
{ }
void *operator new (size_t, void *ptr) { return ptr; }
};
/**
* Static reservation of memory for the embedded object
*/
@ -66,7 +52,7 @@ class Genode::Volatile_object
template <typename... ARGS> void _do_construct(ARGS &&... args)
{
new (_space) Placeable<MT>(args...);
construct_at<MT>(_space, args...);
_constructed = true;
}

View File

@ -59,6 +59,11 @@ compare_output_to {
[init -> test-volatile_object] destruct Member_with_reference
[init -> test-volatile_object] destruct Object 2
[init -> test-volatile_object] destruct Object 1
[init -> test-volatile_object] -- construct Throwing object
[init -> test-volatile_object] construct Throwing -> don't throw
[init -> test-volatile_object] destruct Throwing
[init -> test-volatile_object] construct Throwing -> throw exception
[init -> test-volatile_object] -- catched exception as expected
[init -> test-volatile_object] --- test-volatile_object finished ---
}

View File

@ -75,6 +75,35 @@ struct Compound
};
struct Bool
{
bool b;
Bool(Bool const &) = delete;
Bool(bool const &b) : b(b) { }
};
struct Throwing
{
Throwing(Bool const &throws)
{
if (throws.b) {
PLOG("construct Throwing -> throw exception");
throw -1;
} else {
PLOG("construct Throwing -> don't throw");
}
}
virtual ~Throwing()
{
PLOG("destruct Throwing");
}
};
static void call_const_method(Compound const &compound)
{
compound.member->reference.const_method();
@ -125,6 +154,17 @@ int main(int, char **)
printf("-- destruct Compound and Objects 1 and 2 --\n");
}
try {
printf("-- construct Throwing object\n");
Bool const b_false(false), b_true(true);
Volatile_object<Throwing> inst(b_false);
inst.construct(b_true);
PERR("expected contructor to throw");
} catch (int i) {
printf("-- catched exception as expected\n");
}
printf("--- test-volatile_object finished ---\n");
return 0;