genode/repos/base/include/base/ipc.h

163 lines
3.8 KiB
C
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Generic IPC infrastructure
* \author Norman Feske
* \date 2006-06-12
2011-12-22 16:19:25 +01:00
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
#ifndef _INCLUDE__BASE__IPC_H_
#define _INCLUDE__BASE__IPC_H_
2011-12-22 16:19:25 +01:00
#include <base/ipc_msgbuf.h>
#include <base/rpc_args.h>
#include <base/log.h>
#include <util/meta.h>
2011-12-22 16:19:25 +01:00
namespace Genode {
2011-12-22 16:19:25 +01:00
struct Ipc_error : Exception { };
class Ipc_unmarshaller;
/**
* Invoke capability to call an RPC function
*
* \param rcv_caps number of capabilities expected as result
* \throw Blocking_canceled
*
* \noapi
*
* The 'rcv_caps' value is used on kernels like NOVA to allocate the
* receive window for incoming capability selectors.
*/
Rpc_exception_code ipc_call(Native_capability dst,
Msgbuf_base &snd_msg, Msgbuf_base &rcv_msg,
size_t rcv_caps);
}
/**
* Unmarshal arguments from receive buffer
*/
class Genode::Ipc_unmarshaller : Noncopyable
{
protected:
Msgbuf_base &_rcv_msg;
unsigned _read_offset = 0;
unsigned _read_cap_index = 0;
private:
char *_rcv_buf = (char *)_rcv_msg.data();
size_t const _rcv_buf_size = _rcv_msg.capacity();
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
/*
* Noncopyable
*/
Ipc_unmarshaller(Ipc_unmarshaller const &);
Ipc_unmarshaller &operator = (Ipc_unmarshaller const &);
public:
/**
* Obtain typed capability from message buffer
*/
template <typename IT>
void extract(Capability<IT> &typed_cap)
{
typed_cap = extract(Meta::Overload_selector<Capability<IT> >());
}
template<typename IT>
Capability<IT> extract(Meta::Overload_selector<Capability<IT> >)
{
Native_capability untyped_cap =
extract(Meta::Overload_selector<Native_capability>());
return reinterpret_cap_cast<IT>(untyped_cap);
}
/**
* Obtain capability from message buffer
*/
void extract(Native_capability &cap)
{
cap = extract(Meta::Overload_selector<Native_capability>());
}
Native_capability extract(Meta::Overload_selector<Native_capability>)
{
Native_capability cap = _read_cap_index < _rcv_msg.used_caps()
? _rcv_msg.cap(_read_cap_index) : Native_capability();
_read_cap_index++;
return cap;
}
/**
* Read 'Rpc_in_buffer' from receive buffer
*/
template <size_t SIZE>
void extract(Rpc_in_buffer<SIZE> &b)
{
b = Rpc_in_buffer<SIZE>(0, 0);
/*
* Check receive buffer range
*
* Note: The addr of the Rpc_in_buffer_base is a null pointer when this
* condition triggers.
*/
try {
b = extract(Meta::Overload_selector<Rpc_in_buffer<SIZE> >());
} catch (Ipc_error) { }
}
template<size_t SIZE>
Rpc_in_buffer<SIZE> extract(Meta::Overload_selector<Rpc_in_buffer<SIZE> >)
{
size_t size = extract(Meta::Overload_selector<size_t>());
if (_read_offset + size > _rcv_buf_size) {
error("message buffer overrun");
throw Ipc_error();
}
Rpc_in_buffer<SIZE> buf(&_rcv_buf[_read_offset], size);
_read_offset += align_natural(size);
return buf;
}
/**
* Read value of type T from buffer
*/
template <typename T>
void extract(T &value)
{
value = extract(Meta::Overload_selector<T>());
}
template <typename T>
T extract(Meta::Overload_selector<T>)
{
/* check receive buffer range */
if (_read_offset + sizeof(T) > _rcv_buf_size) throw Ipc_error();
/* return value from receive buffer */
T value = *reinterpret_cast<T *>(&_rcv_buf[_read_offset]);
/* increment read pointer to next dword-aligned value */
_read_offset += align_natural(sizeof(T));
return value;
}
Ipc_unmarshaller(Msgbuf_base &rcv_msg) : _rcv_msg(rcv_msg) { }
};
#endif /* _INCLUDE__BASE__IPC_H_ */