1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

tsrb: change input type to uint8_t

Having the input type `char` makes the output of `tsrb_get_one()`
incomparable to the input of `tsrb_add_one()`. By changing it to
`uint8_t` we not only definitively fix it to an octet, but also ensure
that the input and output are the same.
This commit is contained in:
Martine Lenders 2019-06-05 12:25:12 +02:00
parent 8c5433d5db
commit d361fc52a0
2 changed files with 14 additions and 11 deletions

View File

@ -28,6 +28,7 @@
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
@ -37,7 +38,7 @@ extern "C" {
* @brief thread-safe ringbuffer struct
*/
typedef struct tsrb {
char *buf; /**< Buffer to operate on. */
uint8_t *buf; /**< Buffer to operate on. */
unsigned int size; /**< Size of buffer, must be power of 2. */
volatile unsigned reads; /**< total number of reads */
volatile unsigned writes; /**< total number of writes */
@ -46,7 +47,9 @@ typedef struct tsrb {
/**
* @brief Static initializer
*/
#define TSRB_INIT(BUF) { (BUF), sizeof (BUF), 0, 0 }
/* XXX remove this implicit cast as soon as possible (requires API change
* to isrpipe)*/
#define TSRB_INIT(BUF) { (uint8_t *)(BUF), sizeof (BUF), 0, 0 }
/**
* @brief Initialize a tsrb.
@ -54,7 +57,7 @@ typedef struct tsrb {
* @param[in] buffer Buffer to use by tsrb.
* @param[in] bufsize `sizeof (buffer)`, must be power of 2.
*/
static inline void tsrb_init(tsrb_t *rb, char *buffer, unsigned bufsize)
static inline void tsrb_init(tsrb_t *rb, uint8_t *buffer, unsigned bufsize)
{
/* make sure bufsize is a power of two.
* http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
@ -125,7 +128,7 @@ int tsrb_get_one(tsrb_t *rb);
* @param[in] n max number of bytes to write to @p dst
* @return nr of bytes written to @p dst
*/
int tsrb_get(tsrb_t *rb, char *dst, size_t n);
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n);
/**
* @brief Drop bytes from ringbuffer
@ -142,7 +145,7 @@ int tsrb_drop(tsrb_t *rb, size_t n);
* @return 0 on success
* @return -1 if no space available
*/
int tsrb_add_one(tsrb_t *rb, char c);
int tsrb_add_one(tsrb_t *rb, uint8_t c);
/**
* @brief Add bytes to ringbuffer
@ -151,7 +154,7 @@ int tsrb_add_one(tsrb_t *rb, char c);
* @param[in] n max number of bytes to read from @p src
* @return nr of bytes read from @p src
*/
int tsrb_add(tsrb_t *rb, const char *src, size_t n);
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n);
#ifdef __cplusplus
}

View File

@ -19,12 +19,12 @@
#include "tsrb.h"
static void _push(tsrb_t *rb, char c)
static void _push(tsrb_t *rb, uint8_t c)
{
rb->buf[rb->writes++ & (rb->size - 1)] = c;
}
static char _pop(tsrb_t *rb)
static uint8_t _pop(tsrb_t *rb)
{
return rb->buf[rb->reads++ & (rb->size - 1)];
}
@ -39,7 +39,7 @@ int tsrb_get_one(tsrb_t *rb)
}
}
int tsrb_get(tsrb_t *rb, char *dst, size_t n)
int tsrb_get(tsrb_t *rb, uint8_t *dst, size_t n)
{
size_t tmp = n;
while (tmp && !tsrb_empty(rb)) {
@ -59,7 +59,7 @@ int tsrb_drop(tsrb_t *rb, size_t n)
return (n - tmp);
}
int tsrb_add_one(tsrb_t *rb, char c)
int tsrb_add_one(tsrb_t *rb, uint8_t c)
{
if (!tsrb_full(rb)) {
_push(rb, c);
@ -70,7 +70,7 @@ int tsrb_add_one(tsrb_t *rb, char c)
}
}
int tsrb_add(tsrb_t *rb, const char *src, size_t n)
int tsrb_add(tsrb_t *rb, const uint8_t *src, size_t n)
{
size_t tmp = n;
while (tmp && !tsrb_full(rb)) {