ring_buffer.h

来自「* The functions debug_init() and debug()」· C头文件 代码 · 共 55 行

H
55
字号
/**
 * \file ring_buffer.h
 * \brief Ring (circular) buffer API.
 *
 * A ring (circular) buffer implementation for use in the
 * serial port driver.
 *
 * To avoid the use of malloc to dynamically allocate the
 * ring-buffer structure and its internal buffer, the
 * initialization routine takes a previously allocated
 * ring-buffer structure and data buffer. For example, the
 * serial port driver structure contains a ring buffer
 * for transmit and another for receive.
 */
#ifndef RING_BUFFER_H
#define RING_BUFFER_H

#include <stdint.h>

/* Ring-buffer state */
typedef struct ring_buffer {
	uint32_t  wr;
	uint32_t  rd;
	uint32_t  count;
	uint32_t  size;
	uint8_t  *data;
} ring_buffer_t;

/* Initialize a statically allocated ring buffer with a
 * statically allocated data buffer data[size];
 */
void ring_buffer_init(
	uint32_t      size,
	uint8_t       *data,
	ring_buffer_t *buf);

/* Remove a character from the ring buffer
 * (returns -1 on empty)
 */
int32_t ring_buffer_remove(
	ring_buffer_t *buf);

/* Add a character to the ring buffer
 * (returns -1 on full)
 */
int32_t ring_buffer_add(
	ring_buffer_t *buf,
	int8_t         ch);

/* Number of characters in the buffer */
uint32_t ring_buffer_count(
	ring_buffer_t *buf);

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?