lib: move ring_buffer from misc/ to lib/

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
Anas Nashif 2017-12-14 13:57:03 -05:00 committed by Anas Nashif
parent 6daf046e8f
commit e2122cbf89
13 changed files with 231 additions and 217 deletions

View file

@ -173,7 +173,7 @@ A data item is removed from a ring buffer by calling
APIs
****
The following ring buffer APIs are provided by :file:`include/misc/ring_buffer.h`:
The following ring buffer APIs are provided by :file:`include/ring_buffer.h`:
* :cpp:func:`SYS_RING_BUF_DECLARE_POW2()`
* :cpp:func:`SYS_RING_BUF_DECLARE_SIZE()`

View file

@ -9,7 +9,7 @@
#include <errno.h>
#include <kernel.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
#include <misc/printk.h>
#include <stdio.h>
#include <ipm.h>

View file

@ -11,7 +11,7 @@
#include <kernel.h>
#include <device.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
#ifdef __cplusplus
extern "C" {

View file

@ -22,7 +22,7 @@ extern "C" {
#include <kernel.h>
#include <errno.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
struct event_logger {
struct k_sem sync_sema;

View file

@ -1,216 +1,13 @@
/* ring_buffer.h: Simple ring buffer API */
/*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file */
#ifndef RING_BUFFER_DEP_H__
#define RING_BUFFER_DEP_H__
#ifndef __RING_BUFFER_H__
#define __RING_BUFFER_H__
#warning "This header file has moved, include <ring_buffer.h> instead."
#include <kernel.h>
#include <debug/object_tracing_common.h>
#include <misc/util.h>
#include <errno.h>
#include <ring_buffer.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SIZE32_OF(x) (sizeof((x))/sizeof(u32_t))
/**
* @brief A structure to represent a ring buffer
*/
struct ring_buf {
u32_t head; /**< Index in buf for the head element */
u32_t tail; /**< Index in buf for the tail element */
u32_t dropped_put_count; /**< Running tally of the number of failed
* put attempts
*/
u32_t size; /**< Size of buf in 32-bit chunks */
u32_t *buf; /**< Memory region for stored entries */
u32_t mask; /**< Modulo mask if size is a power of 2 */
#ifdef CONFIG_OBJECT_TRACING
struct ring_buf *__next;
#endif
};
/**
* @defgroup ring_buffer_apis Ring Buffer APIs
* @ingroup kernel_apis
* @{
*/
/**
* @brief Statically define and initialize a high performance ring buffer.
*
* This macro establishes a ring buffer whose size must be a power of 2;
* that is, the ring buffer contains 2^pow 32-bit words, where @a pow is
* the specified ring buffer size exponent. A high performance ring buffer
* doesn't require the use of modulo arithmetic operations to maintain itself.
*
* The ring buffer can be accessed outside the module where it is defined
* using:
*
* @code extern struct ring_buf <name>; @endcode
*
* @param name Name of the ring buffer.
* @param pow Ring buffer size exponent.
*/
#define SYS_RING_BUF_DECLARE_POW2(name, pow) \
static u32_t _ring_buffer_data_##name[1 << (pow)]; \
struct ring_buf name = { \
.size = (1 << (pow)), \
.mask = (1 << (pow)) - 1, \
.buf = _ring_buffer_data_##name \
};
/**
* @brief Statically define and initialize a standard ring buffer.
*
* This macro establishes a ring buffer of an arbitrary size. A standard
* ring buffer uses modulo arithmetic operations to maintain itself.
*
* The ring buffer can be accessed outside the module where it is defined
* using:
*
* @code extern struct ring_buf <name>; @endcode
*
* @param name Name of the ring buffer.
* @param size32 Size of ring buffer (in 32-bit words).
*/
#define SYS_RING_BUF_DECLARE_SIZE(name, size32) \
static u32_t _ring_buffer_data_##name[size32]; \
struct ring_buf name = { \
.size = size32, \
.buf = _ring_buffer_data_##name \
};
/**
* @brief Initialize a ring buffer.
*
* This routine initializes a ring buffer, prior to its first use. It is only
* used for ring buffers not defined using SYS_RING_BUF_DECLARE_POW2 or
* SYS_RING_BUF_DECLARE_SIZE.
*
* Setting @a size to a power of 2 establishes a high performance ring buffer
* that doesn't require the use of modulo arithmetic operations to maintain
* itself.
*
* @param buf Address of ring buffer.
* @param size Ring buffer size (in 32-bit words).
* @param data Ring buffer data area (typically u32_t data[size]).
*/
static inline void sys_ring_buf_init(struct ring_buf *buf, u32_t size,
u32_t *data)
{
buf->head = 0;
buf->tail = 0;
buf->dropped_put_count = 0;
buf->size = size;
buf->buf = data;
if (is_power_of_two(size)) {
buf->mask = size - 1;
} else {
buf->mask = 0;
}
SYS_TRACING_OBJ_INIT(sys_ring_buf, buf);
}
/**
* @brief Determine if a ring buffer is empty.
*
* @param buf Address of ring buffer.
*
* @return 1 if the ring buffer is empty, or 0 if not.
*/
static inline int sys_ring_buf_is_empty(struct ring_buf *buf)
{
return (buf->head == buf->tail);
}
/**
* @brief Determine free space in a ring buffer.
*
* @param buf Address of ring buffer.
*
* @return Ring buffer free space (in 32-bit words).
*/
static inline int sys_ring_buf_space_get(struct ring_buf *buf)
{
if (sys_ring_buf_is_empty(buf)) {
return buf->size - 1;
}
if (buf->tail < buf->head) {
return buf->head - buf->tail - 1;
}
/* buf->tail > buf->head */
return (buf->size - buf->tail) + buf->head - 1;
}
/**
* @brief Write a data item to a ring buffer.
*
* This routine writes a data item to ring buffer @a buf. The data item
* is an array of 32-bit words (from zero to 1020 bytes in length),
* coupled with a 16-bit type identifier and an 8-bit integer value.
*
* @warning
* Use cases involving multiple writers to the ring buffer must prevent
* concurrent write operations, either by preventing all writers from
* being preempted or by using a mutex to govern writes to the ring buffer.
*
* @param buf Address of ring buffer.
* @param type Data item's type identifier (application specific).
* @param value Data item's integer value (application specific).
* @param data Address of data item.
* @param size32 Data item size (number of 32-bit words).
*
* @retval 0 Data item was written.
* @retval -EMSGSIZE Ring buffer has insufficient free space.
*/
int sys_ring_buf_put(struct ring_buf *buf, u16_t type, u8_t value,
u32_t *data, u8_t size32);
/**
* @brief Read a data item from a ring buffer.
*
* This routine reads a data item from ring buffer @a buf. The data item
* is an array of 32-bit words (up to 1020 bytes in length),
* coupled with a 16-bit type identifier and an 8-bit integer value.
*
* @warning
* Use cases involving multiple reads of the ring buffer must prevent
* concurrent read operations, either by preventing all readers from
* being preempted or by using a mutex to govern reads to the ring buffer.
*
* @param buf Address of ring buffer.
* @param type Area to store the data item's type identifier.
* @param value Area to store the data item's integer value.
* @param data Area to store the data item.
* @param size32 Size of the data item storage area (number of 32-bit chunks).
*
* @retval 0 Data item was fetched; @a size32 now contains the number of
* 32-bit words read into data area @a data.
* @retval -EAGAIN Ring buffer is empty.
* @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains
* the number of 32-bit words needed.
*/
int sys_ring_buf_get(struct ring_buf *buf, u16_t *type, u8_t *value,
u32_t *data, u8_t *size32);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __RING_BUFFER_H__ */
#endif /* RING_BUFFER_DEP_H__ */

216
include/ring_buffer.h Normal file
View file

@ -0,0 +1,216 @@
/* ring_buffer.h: Simple ring buffer API */
/*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file */
#ifndef __RING_BUFFER_H__
#define __RING_BUFFER_H__
#include <kernel.h>
#include <debug/object_tracing_common.h>
#include <misc/util.h>
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SIZE32_OF(x) (sizeof((x))/sizeof(u32_t))
/**
* @brief A structure to represent a ring buffer
*/
struct ring_buf {
u32_t head; /**< Index in buf for the head element */
u32_t tail; /**< Index in buf for the tail element */
u32_t dropped_put_count; /**< Running tally of the number of failed
* put attempts
*/
u32_t size; /**< Size of buf in 32-bit chunks */
u32_t *buf; /**< Memory region for stored entries */
u32_t mask; /**< Modulo mask if size is a power of 2 */
#ifdef CONFIG_OBJECT_TRACING
struct ring_buf *__next;
#endif
};
/**
* @defgroup ring_buffer_apis Ring Buffer APIs
* @ingroup kernel_apis
* @{
*/
/**
* @brief Statically define and initialize a high performance ring buffer.
*
* This macro establishes a ring buffer whose size must be a power of 2;
* that is, the ring buffer contains 2^pow 32-bit words, where @a pow is
* the specified ring buffer size exponent. A high performance ring buffer
* doesn't require the use of modulo arithmetic operations to maintain itself.
*
* The ring buffer can be accessed outside the module where it is defined
* using:
*
* @code extern struct ring_buf <name>; @endcode
*
* @param name Name of the ring buffer.
* @param pow Ring buffer size exponent.
*/
#define SYS_RING_BUF_DECLARE_POW2(name, pow) \
static u32_t _ring_buffer_data_##name[1 << (pow)]; \
struct ring_buf name = { \
.size = (1 << (pow)), \
.mask = (1 << (pow)) - 1, \
.buf = _ring_buffer_data_##name \
};
/**
* @brief Statically define and initialize a standard ring buffer.
*
* This macro establishes a ring buffer of an arbitrary size. A standard
* ring buffer uses modulo arithmetic operations to maintain itself.
*
* The ring buffer can be accessed outside the module where it is defined
* using:
*
* @code extern struct ring_buf <name>; @endcode
*
* @param name Name of the ring buffer.
* @param size32 Size of ring buffer (in 32-bit words).
*/
#define SYS_RING_BUF_DECLARE_SIZE(name, size32) \
static u32_t _ring_buffer_data_##name[size32]; \
struct ring_buf name = { \
.size = size32, \
.buf = _ring_buffer_data_##name \
};
/**
* @brief Initialize a ring buffer.
*
* This routine initializes a ring buffer, prior to its first use. It is only
* used for ring buffers not defined using SYS_RING_BUF_DECLARE_POW2 or
* SYS_RING_BUF_DECLARE_SIZE.
*
* Setting @a size to a power of 2 establishes a high performance ring buffer
* that doesn't require the use of modulo arithmetic operations to maintain
* itself.
*
* @param buf Address of ring buffer.
* @param size Ring buffer size (in 32-bit words).
* @param data Ring buffer data area (typically u32_t data[size]).
*/
static inline void sys_ring_buf_init(struct ring_buf *buf, u32_t size,
u32_t *data)
{
buf->head = 0;
buf->tail = 0;
buf->dropped_put_count = 0;
buf->size = size;
buf->buf = data;
if (is_power_of_two(size)) {
buf->mask = size - 1;
} else {
buf->mask = 0;
}
SYS_TRACING_OBJ_INIT(sys_ring_buf, buf);
}
/**
* @brief Determine if a ring buffer is empty.
*
* @param buf Address of ring buffer.
*
* @return 1 if the ring buffer is empty, or 0 if not.
*/
static inline int sys_ring_buf_is_empty(struct ring_buf *buf)
{
return (buf->head == buf->tail);
}
/**
* @brief Determine free space in a ring buffer.
*
* @param buf Address of ring buffer.
*
* @return Ring buffer free space (in 32-bit words).
*/
static inline int sys_ring_buf_space_get(struct ring_buf *buf)
{
if (sys_ring_buf_is_empty(buf)) {
return buf->size - 1;
}
if (buf->tail < buf->head) {
return buf->head - buf->tail - 1;
}
/* buf->tail > buf->head */
return (buf->size - buf->tail) + buf->head - 1;
}
/**
* @brief Write a data item to a ring buffer.
*
* This routine writes a data item to ring buffer @a buf. The data item
* is an array of 32-bit words (from zero to 1020 bytes in length),
* coupled with a 16-bit type identifier and an 8-bit integer value.
*
* @warning
* Use cases involving multiple writers to the ring buffer must prevent
* concurrent write operations, either by preventing all writers from
* being preempted or by using a mutex to govern writes to the ring buffer.
*
* @param buf Address of ring buffer.
* @param type Data item's type identifier (application specific).
* @param value Data item's integer value (application specific).
* @param data Address of data item.
* @param size32 Data item size (number of 32-bit words).
*
* @retval 0 Data item was written.
* @retval -EMSGSIZE Ring buffer has insufficient free space.
*/
int sys_ring_buf_put(struct ring_buf *buf, u16_t type, u8_t value,
u32_t *data, u8_t size32);
/**
* @brief Read a data item from a ring buffer.
*
* This routine reads a data item from ring buffer @a buf. The data item
* is an array of 32-bit words (up to 1020 bytes in length),
* coupled with a 16-bit type identifier and an 8-bit integer value.
*
* @warning
* Use cases involving multiple reads of the ring buffer must prevent
* concurrent read operations, either by preventing all readers from
* being preempted or by using a mutex to govern reads to the ring buffer.
*
* @param buf Address of ring buffer.
* @param type Area to store the data item's type identifier.
* @param value Area to store the data item's integer value.
* @param data Area to store the data item.
* @param size32 Size of the data item storage area (number of 32-bit chunks).
*
* @retval 0 Data item was fetched; @a size32 now contains the number of
* 32-bit words read into data area @a data.
* @retval -EAGAIN Ring buffer is empty.
* @retval -EMSGSIZE Data area @a data is too small; @a size32 now contains
* the number of 32-bit words needed.
*/
int sys_ring_buf_get(struct ring_buf *buf, u16_t *type, u8_t *value,
u32_t *data, u8_t *size32);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __RING_BUFFER_H__ */

View file

@ -1,3 +1,4 @@
add_subdirectory(crc)
add_subdirectory_ifdef(CONFIG_JSON_LIBRARY json)
add_subdirectory(libc)
add_subdirectory_if_kconfig(ring_buffer)

View file

@ -0,0 +1 @@
zephyr_sources(ring_buffer.c)

View file

@ -6,7 +6,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
/**
* Internal data structure for a buffer header.

View file

@ -1,3 +1,2 @@
zephyr_sources_if_kconfig(printk.c)
zephyr_sources_if_kconfig(reboot.c)
zephyr_sources_if_kconfig(ring_buffer.c)

View file

@ -10,7 +10,7 @@
#include <misc/printk.h>
#include <logging/sys_log.h>
#include <stdio.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
#define LOG_BUF_SIZE (512)

View file

@ -10,7 +10,7 @@
*/
#include <logging/event_logger.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
#include <kernel_structs.h>
void sys_event_logger_init(struct event_logger *logger,

View file

@ -7,7 +7,7 @@
*/
#include <ztest.h>
#include <misc/ring_buffer.h>
#include <ring_buffer.h>
#include <logging/sys_log.h>
SYS_RING_BUF_DECLARE_POW2(ring_buf, 8);