ring buffer的C语言实现

ring buffer实质上是在一块连续的内存上对数据进行写入和读取,只是在写入和读取的时候有些不同。每次读取的时候都要从开辟空间的地址加上一个读取的偏移量读取,相对应的,写入的时候也要从开辟空间的地址加上一个写入的偏移量来写入。重点也就在读取和写入数据上面。下面从代码上面来说一下。

  1. 首先定义一个ring buffer的结构体
typedef struct
{
    uint8_t *buffer;      //存放实际的数据
    int32_t read_offset;  //读取地址相对buffer的偏移量
    int32_t write_offset; //写入地址相对buffer的偏移量
    int32_t valid_size;   //buffer的有效size
    int32_t total_size;   //buffer的总大小,即init时malloc的size

} ring_buffer_t;
  1. 初始化ring buffer
/**
 1. 初始化ring_buffer
 2. malloc开辟传入的buff_size大小的空间存放buffer
 3. read_offset write_offset valid_size均置为0
*/
void ring_buffer_init(ring_buffer_t *ring_buffer, int32_t buff_size)
{
    ring_buffer->buffer = malloc(buff_size);
    memset(ring_buffer->buffer, 0, buff_size);

    ring_buffer->read_offset = 0;
    ring_buffer->write_offset = 0;
    ring_buffer->valid_size = 0;
    ring_buffer->total_size = buff_size;
}
  1. 释放ring buffer
/**
 *释放ring_buffer 
 */
void ring_buffer_deinit(ring_buffer_t *ring_buffer)
{
    if (ring_buffer->buffer != NULL)
    {
        free(ring_buffer->buffer);
    }
    memset(ring_buffer, 0, sizeof(ring_buffer_t));
}

4.向ring buffer里面写入数据,写入之后需要注意valid_size和write_offset的改变

/**
 * buffer_to_write:需要写入的数据的地址
 * size:需要写入的数据的大小
*/
void ring_buffer_write(void *buffer_to_write, int32_t size, ring_buffer_t *ring_buffer)
{
    int32_t write_offset = ring_buffer->write_offset;
    int32_t total_size = ring_buffer->total_size;
    int32_t first_write_size = 0;

    if (ring_buffer->valid_size + size > total_size) //ring_buffer->buffer未使用的总大小比需要写入的size小
    {
        printf("ring buffer is to short to save buff\n");
        printf("total_size:%d valid_size:%d size:%d\n", ring_buffer->total_size, ring_buffer->valid_size, size);
        return;
    }

    if (size + write_offset <= total_size) //ring_buffer->buffer的后段未写入的空间不小于size
    {
        memcpy(ring_buffer->buffer + write_offset, buffer_to_write, size);
    }
    else //ring_buffer->buffer的后段未写入的空间小于size,这时候需要先在后面写入一部分,然后返回头部,从前面接着写入
    {
        first_write_size = total_size - write_offset;
        memcpy(ring_buffer->buffer + write_offset, buffer_to_write, first_write_size);
        memcpy(ring_buffer->buffer, buffer_to_write + first_write_size, size - first_write_size);
    }
    ring_buffer->write_offset += size;
    ring_buffer->write_offset %= total_size;
    ring_buffer->valid_size += size;
}

附上图帮助理解:
ring buffer的C语言实现_第1张图片
5.从ring buffer里读取数据,写入之后需要注意valid_size和read_offset的改变

void ring_buffer_read(ring_buffer_t *ring_buffer, void *buff, int32_t size)
{
    int32_t read_offset = ring_buffer->read_offset;
    int32_t total_size = ring_buffer->total_size;
    int32_t first_read_size = 0;

    if (size > ring_buffer->valid_size)
    {
        printf("valid size < read size\n");
        printf("valid size:%d read size:%d\n", ring_buffer->valid_size, size);
        return;
    }

    if (total_size - read_offset >= size)
    {
        memcpy(buff, ring_buffer->buffer + read_offset, size);
    }
    else
    {
        first_read_size = total_size - read_offset;
        memcpy(buff, ring_buffer->buffer + read_offset, first_read_size);
        memcpy(buff + first_read_size, ring_buffer->buffer, size - first_read_size);
    }

    ring_buffer->read_offset += size;
    ring_buffer->read_offset %= total_size;
    ring_buffer->valid_size -= size;
}

希望本文能帮助到读者,最后附上一个测试:


int main()
{
    ring_buffer_t ring_buffer;
    uint8_t buf[8] = "1234abcd";
    uint8_t buf2[6] = {0};

    ring_buffer_init(&ring_buffer, RING_BUFFER_SIZE);//RING_BUFFER_SIZE我用宏定义为8

    ring_buffer_write(buf, sizeof(buf), &ring_buffer); //ring_buffer->buffer="1234abcd"

    ring_buffer_read(&ring_buffer, buf2, 6); //ring_buffer->buffer="1234abcd" buf2="1234ab"

    ring_buffer_write(buf + 2, 6, &ring_buffer); //ring_buffer->buffer="34abcdcd"

    ring_buffer_read(&ring_buffer, buf2, 6); //ring_buffer->buffer="34abcd78" buf2="7834ab"

    ring_buffer_write("12345678abcd", 12, &ring_buffer);
    ring_buffer_deinit(&ring_buffer);
    return 0;
}

你可能感兴趣的:(ring buffer的C语言实现)