1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #1970 from Kijewski/optimize-cib

core: inline `cib.c`
This commit is contained in:
Ludwig Ortmann 2014-12-03 17:12:48 +01:00
commit e6c808d5fa
3 changed files with 89 additions and 99 deletions

View File

@ -1,67 +0,0 @@
/*
* Copyright (C) 2013 Freie Universität Berlin
*
* 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.
*/
/**
* @ingroup core_util
* @{
*
* @file cib.c
* @brief Circular integer buffer implementation
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include "cib.h"
void cib_init(cib_t *cib, unsigned int size)
{
cib->read_count = 0;
cib->write_count = 0;
cib->complement = 0 - size;
}
int cib_avail(cib_t *cib)
{
return (int)(cib->write_count - cib->read_count);
}
int cib_get(cib_t *cib)
{
int avail = cib_avail(cib);
if (avail > 0) {
return (int)(cib->read_count++ & ~cib->complement);
}
return -1;
}
int cib_put(cib_t *cib)
{
int avail = cib_avail(cib);
if ((int)(avail + cib->complement) < 0) {
return (int)(cib->write_count++ & ~(cib->complement));
}
return -1;
}
/*
int main() {
cib_t cib;
cib_init(&cib, 0);
int res = cib_get(&cib);
printf("%i\n", res);
}
*/

View File

@ -22,55 +22,81 @@
#define __CIB_H
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
/**
* @brief circular integer buffer structure
*/
typedef struct cib_t {
unsigned int read_count; /**< count read accesses */
unsigned int write_count; /**< count write accesses */
unsigned int complement; /**< hold complement */
typedef struct {
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 */
} cib_t;
#define CIB_INIT(SIZE) { 0, 0, (SIZE) - 1 }
/**
* @brief Initialize cib_t to 0 and set size.
*
* @param[out] cib Buffer to initialize.
* Must not be NULL.
* @param[in] size Size of the buffer.
* @param[in] size Size of the buffer, must not exceed MAXINT/2.
*/
void cib_init(cib_t *cib, unsigned int size);
/**
* @brief Get the index of the next item in buffer.
*
* @param[in,out] cib corresponding *cib* to buffer.
* Must not be NULL.
* @return index of next item, -1 on error.
*/
int cib_get(cib_t *cib);
/**
* @brief Get index for item in buffer to put to.
*
* @param[in,out] cib corresponding *cib* to buffer.
* Must not be NULL.
* @return index of item to put to, -1 on error.
*/
int cib_put(cib_t *cib);
static inline void cib_init(cib_t *__restrict cib, unsigned int size)
{
cib_t c = CIB_INIT(size);
*cib = c;
}
/**
* @brief Calculates difference between cib_put() and cib_get() accesses.
*
* @param[in] cib the cib_t to check.
* Must not be NULL.
* @return Negative number for #cib_get > #cib_put
* @return 0 for #cib_get == #cib_put
* @return positive number for #cib_get < #cib_put
* @return How often cib_get() can be called before the CIB is empty.
*/
int cib_avail(cib_t *cib);
static inline unsigned int cib_avail(cib_t *__restrict cib)
{
return cib->write_count - cib->read_count;
}
/**
* @brief Get the index of the next item in buffer.
*
* @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_get(cib_t *__restrict cib)
{
unsigned int avail = cib_avail(cib);
if (avail > 0) {
return (int) (cib->read_count++ & cib->mask);
}
return -1;
}
/**
* @brief Get index for item in buffer to put to.
*
* @param[in,out] cib corresponding *cib* to buffer.
* Must not be NULL.
* @return index of item to put to, -1 if the buffer is full
*/
static inline int cib_put(cib_t *__restrict cib)
{
unsigned int avail = cib_avail(cib);
/* We use a signed compare, because the mask is -1u for an empty CIB. */
if ((int) avail <= (int) cib->mask) {
return (int) (cib->write_count++ & cib->mask);
}
return -1;
}
#ifdef __cplusplus
}

View File

@ -14,16 +14,15 @@
#define TEST_CIB_SIZE 2
cib_t cib;
static cib_t cib;
static void set_up(void)
{
cib_init(&cib, 2);
cib_init(&cib, TEST_CIB_SIZE);
}
static void test_cib_put(void)
{
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(1, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_put(&cib));
@ -46,12 +45,44 @@ static void test_cib_avail(void)
TEST_ASSERT_EQUAL_INT(2, cib_avail(&cib));
}
static void test_cib_put_and_get(void)
{
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(1, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_put(&cib));
}
static void test_empty_cib(void)
{
cib_init(&cib, 0);
TEST_ASSERT_EQUAL_INT(0, cib_avail(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_put(&cib));
}
static void test_singleton_cib(void)
{
cib_init(&cib, 1);
TEST_ASSERT_EQUAL_INT(0, cib_avail(&cib));
TEST_ASSERT_EQUAL_INT(-1, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_put(&cib));
TEST_ASSERT_EQUAL_INT(1, cib_avail(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_get(&cib));
TEST_ASSERT_EQUAL_INT(0, cib_avail(&cib));
}
Test *tests_core_cib_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_cib_put),
new_TestFixture(test_cib_get),
new_TestFixture(test_cib_avail),
new_TestFixture(test_cib_put_and_get),
new_TestFixture(test_empty_cib),
new_TestFixture(test_singleton_cib),
};
EMB_UNIT_TESTCALLER(core_cib_tests, set_up, NULL, fixtures);