2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2016-01-04 14:04:05 +01:00
|
|
|
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* 2013 Freie Universität Berlin
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2014-08-23 15:43:13 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
2014-02-11 18:15:43 +01:00
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
/**
|
2019-01-07 15:36:14 +01:00
|
|
|
* @ingroup core_util
|
2013-11-27 16:28:31 +01:00
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Circular integer buffer interface
|
2014-03-31 13:56:26 +02:00
|
|
|
* @details This structure provides an organizational interface
|
|
|
|
* and combined with an memory array forms a circular buffer.
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2016-01-04 14:04:05 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
2015-03-26 17:07:04 +01:00
|
|
|
#ifndef CIB_H
|
|
|
|
#define CIB_H
|
2010-11-26 13:15:01 +01:00
|
|
|
|
2015-10-13 15:59:38 +02:00
|
|
|
#include "assert.h"
|
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
2014-11-11 21:07:40 +01:00
|
|
|
extern "C" {
|
2014-10-09 01:18:16 +02:00
|
|
|
#endif
|
|
|
|
|
2014-03-31 13:56:26 +02:00
|
|
|
/**
|
|
|
|
* @brief circular integer buffer structure
|
|
|
|
*/
|
2014-11-07 20:42:21 +01:00
|
|
|
typedef struct {
|
2015-03-27 22:46:40 +01:00
|
|
|
unsigned int read_count; /**< number of (successful) read accesses */
|
|
|
|
unsigned int write_count; /**< number of (successful) write accesses */
|
|
|
|
unsigned int mask; /**< Size of buffer -1, i.e. mask of the bits */
|
2010-11-26 13:15:01 +01:00
|
|
|
} cib_t;
|
|
|
|
|
2014-12-04 15:11:42 +01:00
|
|
|
/**
|
|
|
|
* @brief Initialize cib_t to a given size.
|
2020-05-14 13:26:58 +02:00
|
|
|
*
|
|
|
|
* @param[in] SIZE Size of the buffer, must not exceed
|
|
|
|
* (`UINT_MAX` + 1) / 2.
|
|
|
|
* Should be equal to 0 or power of 2.
|
2014-12-04 15:11:42 +01:00
|
|
|
*/
|
2020-03-30 17:02:08 +02:00
|
|
|
#define CIB_INIT(SIZE) { 0, 0, (SIZE)-1 }
|
2014-11-07 20:42:21 +01:00
|
|
|
|
2014-03-31 13:56:26 +02:00
|
|
|
/**
|
2016-04-09 13:01:32 +02:00
|
|
|
* @brief Initialize @p cib to 0 and set buffer size to @p size.
|
2014-03-31 13:56:26 +02:00
|
|
|
*
|
|
|
|
* @param[out] cib Buffer to initialize.
|
|
|
|
* Must not be NULL.
|
2020-05-14 13:26:58 +02:00
|
|
|
* @param[in] size Size of the buffer, must not exceed
|
|
|
|
* (`UINT_MAX` + 1) / 2.
|
2015-10-13 15:59:38 +02:00
|
|
|
* Should be equal to 0 or power of 2.
|
2014-03-31 13:56:26 +02:00
|
|
|
*/
|
2015-03-27 22:46:40 +01:00
|
|
|
static inline void cib_init(cib_t *__restrict cib, unsigned int size)
|
2014-11-07 20:42:21 +01:00
|
|
|
{
|
2015-10-13 17:07:42 +02:00
|
|
|
/* check if size is a power of 2 by comparing it to its complement */
|
|
|
|
assert(!(size & (size - 1)));
|
|
|
|
|
2014-11-11 21:07:40 +01:00
|
|
|
cib_t c = CIB_INIT(size);
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2014-11-11 21:07:40 +01:00
|
|
|
*cib = c;
|
2014-11-07 20:42:21 +01:00
|
|
|
}
|
2014-03-31 13:56:26 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-07 20:42:21 +01:00
|
|
|
* @brief Calculates difference between cib_put() and cib_get() accesses.
|
2014-03-31 13:56:26 +02:00
|
|
|
*
|
2014-11-07 20:42:21 +01:00
|
|
|
* @param[in] cib the cib_t to check.
|
2014-03-31 13:56:26 +02:00
|
|
|
* Must not be NULL.
|
2016-04-09 13:01:32 +02:00
|
|
|
* @return How often cib_get() can be called before @p cib is empty.
|
2014-03-31 13:56:26 +02:00
|
|
|
*/
|
2016-01-04 14:04:05 +01:00
|
|
|
static inline unsigned int cib_avail(const cib_t *cib)
|
2014-11-07 20:42:21 +01:00
|
|
|
{
|
|
|
|
return cib->write_count - cib->read_count;
|
|
|
|
}
|
2014-03-31 13:56:26 +02:00
|
|
|
|
2016-01-04 14:04:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Check if cib is full.
|
|
|
|
*
|
|
|
|
* @param[in] cib the cib_t to check.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @return 1 if cib_put() would return "-1", 0 otherwise
|
|
|
|
*/
|
|
|
|
static inline unsigned int cib_full(const cib_t *cib)
|
|
|
|
{
|
2020-03-30 17:02:08 +02:00
|
|
|
return ((int)cib_avail(cib)) > ((int)cib->mask);
|
2016-01-04 14:04:05 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 13:56:26 +02:00
|
|
|
/**
|
2014-11-07 20:42:21 +01:00
|
|
|
* @brief Get the index of the next item in buffer.
|
2014-03-31 13:56:26 +02:00
|
|
|
*
|
|
|
|
* @param[in,out] cib corresponding *cib* to buffer.
|
|
|
|
* Must not be NULL.
|
2014-11-07 20:42:21 +01:00
|
|
|
* @return index of next item, -1 if the buffer is empty
|
2014-03-31 13:56:26 +02:00
|
|
|
*/
|
2015-03-27 22:46:40 +01:00
|
|
|
static inline int cib_get(cib_t *__restrict cib)
|
2014-11-07 20:42:21 +01:00
|
|
|
{
|
2017-11-22 17:53:16 +01:00
|
|
|
if (cib_avail(cib)) {
|
2020-03-30 17:02:08 +02:00
|
|
|
return (int)(cib->read_count++ & cib->mask);
|
2014-11-07 20:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-31 13:56:26 +02:00
|
|
|
|
2016-06-17 15:42:15 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the index of the next item in buffer without removing it.
|
|
|
|
*
|
|
|
|
* @param[in,out] cib corresponding *cib* to buffer.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @return index of next item, -1 if the buffer is empty
|
|
|
|
*/
|
|
|
|
static inline int cib_peek(cib_t *__restrict cib)
|
|
|
|
{
|
2017-11-22 17:53:16 +01:00
|
|
|
if (cib_avail(cib)) {
|
2020-03-30 17:02:08 +02:00
|
|
|
return (int)(cib->read_count & cib->mask);
|
2016-06-17 15:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-04 14:04:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the index of the next item in buffer.
|
|
|
|
*
|
|
|
|
* Unsafe version, *must not* be called if buffer is empty!
|
|
|
|
*
|
|
|
|
* @param[in,out] cib corresponding *cib* to buffer.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @return index of next item
|
|
|
|
*/
|
|
|
|
static inline int cib_get_unsafe(cib_t *cib)
|
|
|
|
{
|
2020-03-30 17:02:08 +02:00
|
|
|
return (int)(cib->read_count++ & cib->mask);
|
2016-01-04 14:04:05 +01:00
|
|
|
}
|
|
|
|
|
2014-03-31 13:56:26 +02:00
|
|
|
/**
|
2014-11-07 20:42:21 +01:00
|
|
|
* @brief Get index for item in buffer to put to.
|
2014-03-31 13:56:26 +02:00
|
|
|
*
|
2014-11-07 20:42:21 +01:00
|
|
|
* @param[in,out] cib corresponding *cib* to buffer.
|
2014-03-31 13:56:26 +02:00
|
|
|
* Must not be NULL.
|
2014-11-07 20:42:21 +01:00
|
|
|
* @return index of item to put to, -1 if the buffer is full
|
2014-03-31 13:56:26 +02:00
|
|
|
*/
|
2015-03-27 22:46:40 +01:00
|
|
|
static inline int cib_put(cib_t *__restrict cib)
|
2014-11-07 20:42:21 +01:00
|
|
|
{
|
2015-03-27 22:46:40 +01:00
|
|
|
unsigned int avail = cib_avail(cib);
|
2014-11-07 20:42:21 +01:00
|
|
|
|
|
|
|
/* We use a signed compare, because the mask is -1u for an empty CIB. */
|
2020-03-30 17:02:08 +02:00
|
|
|
if ((int)avail <= (int)cib->mask) {
|
|
|
|
return (int)(cib->write_count++ & cib->mask);
|
2014-11-07 20:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-26 13:15:01 +01:00
|
|
|
|
2016-01-04 14:04:05 +01:00
|
|
|
/**
|
|
|
|
* @brief Get index for item in buffer to put to.
|
|
|
|
*
|
|
|
|
* Unsafe version, *must not* be called if buffer is full!
|
|
|
|
*
|
|
|
|
* @param[in,out] cib corresponding *cib* to buffer.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @return index of item to put to
|
|
|
|
*/
|
|
|
|
static inline int cib_put_unsafe(cib_t *cib)
|
|
|
|
{
|
2020-03-30 17:02:08 +02:00
|
|
|
return (int)(cib->write_count++ & cib->mask);
|
2016-01-04 14:04:05 +01:00
|
|
|
}
|
|
|
|
|
2014-10-09 01:18:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 17:07:04 +01:00
|
|
|
#endif /* CIB_H */
|
2014-03-31 13:56:26 +02:00
|
|
|
/** @} */
|