ring-buffer(C 语言实现)

        下面是一个 ring-buffer 的 C 语言实现,采用 Mirroring 方式实现空、满判断。通过 void 指针模拟泛型,可以在初始化时灵活定义 ring-buffer 中的元素类型(例如 char 类型,int 类型,数组类型,结构体类型等)。

        首先给出头文件定义:

/**
 * File: ring_buf.h
 *
 * Description:
 *   Header file for ring-buffer.
 *   Implemented according to the 'Mirroring' solution:
 *   http://en.wikipedia.org/wiki/Circular_buffer#Mirroring
 *
 * Change Logs:
 *   Date           Author       Notes
 *   2013-12-16     Glen         first implementation
 */

#ifndef _RING_BUF_H_
#define _RING_BUF_H_

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#define IDX_MASK (SIZE_MAX>>1)
#define MSB_MASK (~IDX_MASK)   /* also the maximum value of the buffer depth */

/* ring buffer structure */
struct ring_buf
{
    size_t depth ;             /* maximum element number */
    size_t width ;             /* sizeof each element */
    size_t rd_idx;             /* MSB is used for the 'mirror' flag */
    size_t wr_idx;             /* MSB is used for the 'mirror' flag */
    void   *buf  ;
};

typedef struct ring_buf  ring_buf_t;
typedef struct ring_buf* ring_buf_p;

bool ring_buf_init (ring_buf_p rbuf, size_t depth, size_t width);
void ring_buf_free (ring_buf_p rbuf);
void ring_buf_clear(ring_buf_p rbuf);

bool ring_buf_full (ring_buf_p rbuf);
bool ring_buf_empty(ring_buf_p rbuf);

bool ring_buf_write(ring_buf_p rbuf, void *wr_data);
bool ring_buf_read (ring_buf_p rbuf, void *rd_data);

#endif

        下面是对应的 C 文件:

/**
 * File: ring_buf.c
 *
 * Description:
 *   Ring-buffer implemented according to the 'Mirroring' solution:
 *   http://en.wikipedia.org/wiki/Circular_buffer#Mirroring
 *
 * Change Logs:
 *   Date           Author       Notes
 *   2013-12-16     Glen         first implementation
 */

#include "ring_buf.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/**
 * @brief  Initialize the ring-buffer. Allocate necessary memory for the buffer.
 *
 * @param  rbuf : Pointer to the ring-buffer object
 * @param  depth: Maximum element number of the buffer
 * @param  width: sizeof each element
 *
 * @return true : Succeeded
 *         false: Failed
 */
bool ring_buf_init(ring_buf_p rbuf, size_t depth, size_t width)
{
    assert(depth > 0 && depth < MSB_MASK && width > 0);

    rbuf->depth  = depth;
    rbuf->width  = width;
    rbuf->rd_idx = 0;
    rbuf->wr_idx = 0;
    rbuf->buf    = calloc(depth, width);
    return rbuf->buf != NULL;
}

/**
 * @brief  Release the ring-buffer object. Deallocate the buffer memory.
 *
 * @param  rbuf: Pointer to the ring-buffer object
 */
void ring_buf_free(ring_buf_p rbuf)
{
    free(rbuf->buf);
    rbuf->buf = NULL;
}

/**
 * @brief  Clear the ring-buffer object.
 *
 * @param  rbuf: Pointer to the ring-buffer object
 */
void ring_buf_clear(ring_buf_p rbuf)
{
    rbuf->rd_idx = 0;
    rbuf->wr_idx = 0;
}

/**
 * @brief  Whether the ring-buffer is empty or not.
 *
 * @param  rbuf: Pointer to the ring-buffer
 *
 * @return true : Empty. There's no elements stored in the buffer.
 *         false: Not empty
 */
bool ring_buf_is_empty(ring_buf_p rbuf)
{
    return rbuf->rd_idx == rbuf->wr_idx;
}

/**
 * @brief  Whether the ring-buffer is full or not.
 *
 * @param  rbuf: Pointer to the ring-buffer
 *
 * @return true : Full
 *         false: Not full
 */
bool ring_buf_is_full(ring_buf_p rbuf)
{
    return (rbuf->rd_idx & IDX_MASK) == (rbuf->wr_idx & IDX_MASK) &&
           (rbuf->rd_idx & MSB_MASK) != (rbuf->wr_idx & MSB_MASK);
}

/**
 * @brief  Increase the buffer index while writing or reading the ring-buffer.
 *         This is implemented according to the 'mirroring' solution:
 *         http://en.wikipedia.org/wiki/Circular_buffer#Mirroring
 *
 * @param  rbuf : Pointer to the ring-buffer
 * @param  p_idx: Pointer to the buffer index to be increased
 */
inline void ring_buf_incr(ring_buf_p rbuf, size_t *p_idx)
{
    size_t idx = *p_idx & IDX_MASK;
    size_t msb = *p_idx & MSB_MASK;

    if (++idx == rbuf->depth) {
        msb ^= MSB_MASK;
        idx = 0;
    }
    *p_idx = msb | idx;
}

/**
 * @brief  Write new element to the ring-buffer.
 *
 * @param  rbuf   : Pointer to the ring-buffer object
 * @param  wr_data: Pointer of the new element to be written to the buffer
 *
 * @return true : Succeeded
 *         false: Failed
 */
bool ring_buf_write(ring_buf_p rbuf, void *wr_data)
{
    if (ring_buf_is_full(rbuf))
        return false;
    else {
        memcpy(rbuf->buf + rbuf->wr_idx * rbuf->width, wr_data, rbuf->width);
        ring_buf_incr(rbuf, &rbuf->wr_idx);

        return true;
    }
}

/**
 * @brief  Read from the ring-buffer
 *
 * @param  rbuf   : Pointer to the ring-buffer object
 * @param  rd_data: Pointer to the read result
 *
 * @return true : Succeeded
 *         false: Failed
 */
bool ring_buf_read(ring_buf_p rbuf, void *rd_data)
{
    if (ring_buf_is_empty(rbuf))
        return false;
    else {
	memcpy(rd_data, rbuf->buf + rbuf->rd_idx * rbuf->width, rbuf->width);
        ring_buf_incr(rbuf, &rbuf->rd_idx);

        return true;
    }
}

        最后给出一个简单的应用示例,将字符数组写入 ring-buffer,然后读出、显示:

int main()
{
    ring_buf_t rbuf;
    char str[10];
    ring_buf_init(&rbuf, 8, sizeof(str));

    ring_buf_write(&rbuf, "str 0");
    ring_buf_write(&rbuf, "str 1");
    ring_buf_write(&rbuf, "str 2");
    ring_buf_write(&rbuf, "str 3");

    int k = 0;
    for (k = 0; k < 8; k++)
        if (ring_buf_read(&rbuf, str))
            printf("%s\n", str);

    ring_buf_free(&rbuf);

    return 0;
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Linux内核中的无锁(lock-free)技术主要用于实现高效的并发数据结构,以提高系统的性能和吞吐量。其中,无锁环形缓冲区(lock-free ring buffer)是一种常用的数据结构,它可以高效地实现在多个线程之间传递数据的功能。 无锁环形缓冲区的实现原理如下: 1. 环形缓冲区的数据结构:无锁环形缓冲区由一个固定大小的环形数组和两个指针构成,一个是读指针,一个是写指针。读指针指向下一个将要读取的元素,写指针指向下一个将要写入的元素。 2. 原子操作:无锁环形缓冲区的实现依赖于原子操作(atomic operations),这些操作是在单个CPU指令中执行的,不会被其他线程中断。在Linux内核中,原子操作是通过宏定义实现的,如“atomic_add()”、“atomic_sub()”等。 3. 写入数据:当一个线程想要写入数据时,它首先需要检查缓冲区是否已满。如果缓冲区已满,则写入操作失败。如果缓冲区未满,则该线程会使用原子操作将数据写入缓冲区,并更新写指针。 4. 读取数据:当一个线程想要读取数据时,它首先需要检查缓冲区是否为空。如果缓冲区为空,则读取操作失败。如果缓冲区不为空,则该线程会使用原子操作将数据从缓冲区中读取,并更新读指针。 5. 线程同步:无锁环形缓冲区的实现不依赖于任何锁机制,因此可以避免锁竞争和死锁等问题。不过,在多个线程并发读写的情况下,需要使用一些同步机制来保证线程安全,如使用原子操作或者memory barrier等技术。 总的来说,无锁环形缓冲区是一种高效的并发数据结构,能够在多个线程之间高效地传递数据,提高系统的性能和吞吐量。在Linux内核中,无锁环形缓冲区的实现依赖于原子操作和线程同步技术,可以避免锁竞争和死锁等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值