2012-05-30 18:13:09 +00:00
|
|
|
/*
|
|
|
|
* \brief IPC message buffers
|
|
|
|
* \author Martin Stein
|
2015-05-19 12:18:40 +00:00
|
|
|
* \author Stefan Kalkowski
|
2012-05-30 18:13:09 +00:00
|
|
|
* \date 2012-01-03
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2015-05-19 12:18:40 +00:00
|
|
|
* Copyright (C) 2012-2015 Genode Labs GmbH
|
2012-05-30 18:13:09 +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-19 12:18:40 +00:00
|
|
|
#include <base/native_capability.h>
|
2016-03-15 19:01:59 +00:00
|
|
|
#include <base/stdint.h>
|
2015-05-19 12:18:40 +00:00
|
|
|
#include <util/string.h>
|
|
|
|
|
2016-03-15 19:01:59 +00:00
|
|
|
namespace Genode {
|
|
|
|
|
2015-05-19 12:18:40 +00:00
|
|
|
class Native_utcb;
|
|
|
|
|
2016-03-15 19:01:59 +00:00
|
|
|
class Ipc_marshaller;
|
|
|
|
|
2012-05-30 18:13:09 +00:00
|
|
|
/**
|
|
|
|
* IPC message buffer layout
|
|
|
|
*/
|
2015-05-19 12:18:40 +00:00
|
|
|
class Msgbuf_base;
|
2012-05-30 18:13:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of IPC message buffer with specified buffer size
|
|
|
|
*
|
|
|
|
* 'Msgbuf_base' must be the last class this class inherits from.
|
|
|
|
*/
|
2015-05-19 12:18:40 +00:00
|
|
|
template <unsigned BUF_SIZE> class Msgbuf;
|
2012-05-30 18:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-19 12:18:40 +00:00
|
|
|
class Genode::Msgbuf_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum { MAX_CAP_ARGS = 4 };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
friend class Native_utcb;
|
2016-03-15 19:01:59 +00:00
|
|
|
friend class Ipc_marshaller;
|
2015-05-19 12:18:40 +00:00
|
|
|
|
2016-03-15 19:01:59 +00:00
|
|
|
size_t const _capacity; /* buffer size in bytes */
|
|
|
|
size_t _data_size = 0; /* marshalled data in bytes */
|
|
|
|
Native_capability _caps[MAX_CAP_ARGS]; /* capability buffer */
|
|
|
|
size_t _snd_cap_cnt = 0; /* capability counter */
|
|
|
|
size_t _rcv_cap_cnt = 0; /* capability counter */
|
2015-05-19 12:18:40 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
** 'buf' must be the last member of this class **
|
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
char buf[]; /* begin of actual message buffer */
|
|
|
|
|
2016-03-15 19:01:59 +00:00
|
|
|
Msgbuf_base(size_t capacity) : _capacity(capacity) { }
|
2015-05-19 12:18:40 +00:00
|
|
|
|
|
|
|
void const * base() const { return &buf; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return size of message buffer
|
|
|
|
*/
|
2016-03-15 19:01:59 +00:00
|
|
|
size_t capacity() const { return _capacity; }
|
2015-05-19 12:18:40 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-13 21:55:48 +00:00
|
|
|
* Return pointer of message data payload
|
2015-05-19 12:18:40 +00:00
|
|
|
*/
|
2016-03-15 19:01:59 +00:00
|
|
|
void *data() { return &buf[0]; }
|
|
|
|
void const *data() const { return &buf[0]; }
|
|
|
|
|
|
|
|
size_t data_size() const { return _data_size; }
|
2015-05-19 12:18:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset capability buffer.
|
|
|
|
*/
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_snd_cap_cnt = 0;
|
|
|
|
_rcv_cap_cnt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return how many capabilities are accepted by this message buffer
|
|
|
|
*/
|
|
|
|
size_t cap_rcv_window() { return _rcv_cap_cnt; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set how many capabilities are accepted by this message buffer
|
|
|
|
*/
|
|
|
|
void cap_rcv_window(size_t cnt) { _rcv_cap_cnt = cnt; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add capability to buffer
|
|
|
|
*/
|
|
|
|
void cap_add(Native_capability const &cap)
|
|
|
|
{
|
|
|
|
if (_snd_cap_cnt < MAX_CAP_ARGS)
|
|
|
|
_caps[_snd_cap_cnt++] = cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return last capability from buffer.
|
|
|
|
*/
|
|
|
|
Native_capability cap_get()
|
|
|
|
{
|
|
|
|
return (_rcv_cap_cnt < _snd_cap_cnt)
|
|
|
|
? _caps[_rcv_cap_cnt++] : Native_capability();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <unsigned BUF_SIZE>
|
|
|
|
class Genode::Msgbuf : public Genode::Msgbuf_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**************************************************
|
|
|
|
** 'buf' must be the first member of this class **
|
|
|
|
**************************************************/
|
|
|
|
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
Msgbuf() : Msgbuf_base(BUF_SIZE) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__BASE__IPC_MSGBUF_H_ */
|