core: inline `cib.c`

The functions in `cib.h` are only used in `msg.c`. They are very small,
and proper function calls have a bigger overhead than inlining these
functions.

The difference in .text size (worst to best):

```
+104 native
+69  qemu-i386
+32  mbed_lpc1768
+28  samr21-xpro

-54  chronos
-54  z1
-56  iot-lab_M3
-56  msb-430
-56  msb-430h
-56  msba2
-56  openmote
-56  spark-core
-56  telosb
-56  wsn430-v1_3b
-56  wsn430-v1_4
-58  arduino-mega2560
-60  airfy-beacon
-60  arduino-due
-60  f4vi1
-60  msbiot
-60  pca10000
-60  pca10005
-60  stm32f0discovery
-60  stm32f3discovery
-60  stm32f4discovery
-60  udoo
-60  yunjia-nrf51822
-72  avsextrem
-72  pttu
-120 redbee-econotag
```
dev/timer
René Kijewski 9 years ago
parent f23771c6fe
commit ab545f83b0

@ -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);
}
*/

@ -28,12 +28,14 @@
/**
* @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) ((cib_t) { 0, 0, (SIZE) - 1 })
/**
* @brief Initialize cib_t to 0 and set size.
*
@ -41,36 +43,59 @@ typedef struct cib_t {
* Must not be NULL.
* @param[in] size Size of the buffer.
*/
void cib_init(cib_t *cib, unsigned int size);
static inline void cib_init(cib_t *restrict cib, unsigned int size)
{
*cib = CIB_INIT(size);
}
/**
* @brief Get the index of the next item in buffer.
* @brief Calculates difference between cib_put() and cib_get() accesses.
*
* @param[in,out] cib corresponding *cib* to buffer.
* @param[in] cib the cib_t to check.
* Must not be NULL.
* @return index of next item, -1 on error.
* @return How often cib_get() can be called before the CIB is empty.
*/
int cib_get(cib_t *cib);
static inline unsigned int cib_avail(cib_t *restrict cib)
{
return cib->write_count - cib->read_count;
}
/**
* @brief Get index for item in buffer to put to.
* @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 item to put to, -1 on error.
* @return index of next item, -1 if the buffer is empty
*/
int cib_put(cib_t *cib);
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 Calculates difference between cib_put() and cib_get() accesses.
* @brief Get index for item in buffer to put to.
*
* @param[in] cib the cib_t to check.
* @param[in,out] cib corresponding *cib* to buffer.
* 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 index of item to put to, -1 if the buffer is full
*/
int cib_avail(cib_t *cib);
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
}

Loading…
Cancel
Save