genode/repos/libports/include/libc/component.h

141 lines
3.7 KiB
C
Raw Normal View History

/*
* \brief Hook functions for bootstrapping a libc-using Genode component
* \author Norman Feske
* \date 2016-12-23
*
* This interface is implemented by components that use both Genode's API and
* the libc. For such components, the libc provides the 'Component::construct'
* function that takes the precautions needed for letting the application use
* blocking I/O via POSIX functions like 'read' or 'select'. The libc's
* 'Component::construct' function finally passes control to the application by
* calling the application-provided 'Libc::Component::construct' function.
*/
/*
* Copyright (C) 2016-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__LIBC__COMPONENT_H_
#define _INCLUDE__LIBC__COMPONENT_H_
#include <util/meta.h>
#include <base/env.h>
#include <base/stdint.h>
#include <util/xml_node.h>
namespace Libc { class Env; }
namespace Vfs { struct File_system; }
/**
* Interface to be provided by the component implementation
*/
class Libc::Env : public Genode::Env
{
private:
virtual Genode::Xml_node _config_xml() const = 0;
public:
/**
* Component configuration
*/
template <typename FUNC>
void config(FUNC const &func) const {
func(_config_xml()); }
/**
* Virtual File System configured for this component
*/
virtual Vfs::File_system &vfs() = 0;
/**
* Libc configuration for this component
*/
virtual Genode::Xml_node libc_config() = 0;
};
namespace Libc { namespace Component {
/**
* Return stack size of the component's initial entrypoint
*/
Genode::size_t stack_size();
/**
* Construct component
*
* \param env extended interface to the component's execution environment
*/
void construct(Libc::Env &env);
} }
namespace Libc {
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
2017-12-21 15:42:15 +01:00
class Application_code : Genode::Interface
{
protected:
/*
* Helper to distinguish void from non-void return type
*/
template <typename FUNC, typename RET>
void _execute(RET &ret_out, FUNC const &func) { ret_out = func(); }
template <typename FUNC>
void _execute(Genode::Meta::Empty &, FUNC const &func) { func(); }
public:
virtual void execute() = 0;
};
void execute_in_application_context(Application_code &);
/**
* Execute 'FUNC' lambda in the libc application context
*
* In order to invoke the libc's I/O functions (in particular 'select',
* 'read', 'write', or other functions like 'socket' that indirectly call
* them), the libc-calling application code must be executed under the
* supervision of the libc runtime. This is not the case for signal
* handlers or RPC functions that are executed in the context of the
* 'Genode::Entrypoint'. The 'with_libc' function allows such handlers to
* interact with the libc by deliberately subjecting the specified function
* ('func') to the libc runtime.
*/
template <typename FUNC>
auto with_libc(FUNC const &func)
-> typename Genode::Trait::Call_return
<typename Genode::Trait::Functor
<decltype(&FUNC::operator())>::Return_type>::Type
{
using Functor = Genode::Trait::Functor<decltype(&FUNC::operator())>;
using Return_type = typename Genode::Trait::Call_return<typename Functor::Return_type>::Type;
/*
* Implementation of the 'Application_code' interface that executes
* the code in the 'func' lambda.
*/
struct Application_code_func : Application_code
{
FUNC const &func;
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
2017-12-21 15:42:15 +01:00
Return_type retval { };
void execute() override { _execute(retval, func); }
Application_code_func(FUNC const &func) : func(func) { }
} application_code_func { func };
execute_in_application_context(application_code_func);
return application_code_func.retval;
}
}
#endif /* _INCLUDE__LIBC__COMPONENT_H_ */