2014-10-15 12:48:45 +00:00
|
|
|
/*
|
2015-05-11 06:43:43 +00:00
|
|
|
* \brief IPC message buffer layout
|
2014-10-15 12:48:45 +00:00
|
|
|
* \author Norman Feske
|
2015-05-11 06:43:43 +00:00
|
|
|
* \date 2015-05-10
|
2014-10-15 12:48:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-05-11 06:43:43 +00:00
|
|
|
* Copyright (C) 2015 Genode Labs GmbH
|
2014-10-15 12:48:45 +00:00
|
|
|
*
|
|
|
|
* 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__BASE__IPC_MSGBUF_H_
|
|
|
|
#define _INCLUDE__BASE__IPC_MSGBUF_H_
|
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
#include <base/native_types.h>
|
|
|
|
|
2014-10-15 12:48:45 +00:00
|
|
|
namespace Genode {
|
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
class Msgbuf_base;
|
|
|
|
template <unsigned> struct Msgbuf;
|
|
|
|
}
|
|
|
|
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
class Genode::Msgbuf_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum { MAX_CAPS_PER_MSG = 3 };
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
protected:
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/*
|
|
|
|
* Resolve ambiguity if the header is included from a libc-using
|
|
|
|
* program.
|
|
|
|
*/
|
|
|
|
typedef Genode::size_t size_t;
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/*
|
|
|
|
* Capabilities to be transferred
|
|
|
|
*/
|
|
|
|
Native_capability _caps[MAX_CAPS_PER_MSG];
|
|
|
|
size_t _used_caps = 0;
|
|
|
|
size_t _read_cap_index = 0;
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Maximum size of plain-data message payload
|
|
|
|
*/
|
|
|
|
size_t const _size;
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Actual size of plain-data message payload
|
|
|
|
*/
|
|
|
|
size_t _used_size = 0;
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
char _msg_start[]; /* symbol marks start of message buffer data */
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/*
|
|
|
|
* No member variables are allowed beyond this point!
|
|
|
|
*/
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
Msgbuf_base(size_t size) : _size(size) { }
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
public:
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
char buf[];
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Return size of message buffer
|
|
|
|
*/
|
|
|
|
size_t size() const { return _size; };
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
void reset_caps()
|
|
|
|
{
|
|
|
|
for (Genode::size_t i = 0; i < _used_caps; i++)
|
|
|
|
_caps[i] = Native_capability();
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
_used_caps = 0;
|
|
|
|
}
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
void reset_read_cap_index()
|
|
|
|
{
|
|
|
|
_read_cap_index = 0;
|
|
|
|
}
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Return pointer to start of message-buffer content
|
|
|
|
*/
|
|
|
|
void const *data() const { return &_msg_start[0]; };
|
|
|
|
void *data() { return &_msg_start[0]; };
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Exception type
|
|
|
|
*/
|
|
|
|
class Too_many_caps : public Exception { };
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Called from '_marshal_capability'
|
|
|
|
*/
|
|
|
|
void append_cap(Native_capability const &cap)
|
|
|
|
{
|
|
|
|
if (_used_caps == MAX_CAPS_PER_MSG)
|
|
|
|
throw Too_many_caps();
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
_caps[_used_caps++] = cap;
|
|
|
|
}
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
/**
|
|
|
|
* Called from '_unmarshal_capability'
|
|
|
|
*/
|
|
|
|
Native_capability extract_cap()
|
|
|
|
{
|
|
|
|
if (_read_cap_index == _used_caps)
|
|
|
|
return Native_capability();
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
return _caps[_read_cap_index++];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return number of marshalled capabilities
|
|
|
|
*/
|
|
|
|
size_t used_caps() const { return _used_caps; }
|
|
|
|
|
|
|
|
Native_capability &cap(unsigned index)
|
|
|
|
{
|
|
|
|
return _caps[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <unsigned BUF_SIZE>
|
|
|
|
struct Genode::Msgbuf : Msgbuf_base
|
|
|
|
{
|
2014-10-15 12:48:45 +00:00
|
|
|
/**
|
|
|
|
* Pump up IPC message buffer to specified buffer size
|
2015-05-11 06:43:43 +00:00
|
|
|
*
|
|
|
|
* XXX remove padding of 16
|
2014-10-15 12:48:45 +00:00
|
|
|
*/
|
2015-05-11 06:43:43 +00:00
|
|
|
char buf[BUF_SIZE + 16];
|
2014-10-15 12:48:45 +00:00
|
|
|
|
2015-05-11 06:43:43 +00:00
|
|
|
Msgbuf() : Msgbuf_base(BUF_SIZE) { }
|
|
|
|
};
|
2014-10-15 12:48:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__BASE__IPC_MSGBUF_H_ */
|