简体   繁体   English

带时间戳的环形缓冲区

[英]Ring Buffer with time stamp

I have a need for a ring buffer (In C language) which can hold objects of any type at the run time (almost the data will be different signal's values like current (100ms and 10ms) and temperature.etc) ( I am not sure if it have to be a fixed size or not) and it needs to be very high performance. 我需要一个环形缓冲区(使用C语言),该缓冲区可以在运行时保存任何类型的对象(几乎数据将是不同的信号值,例如电流(100ms和10ms)和温度等)(我不确定(如果必须是固定大小),则必须具有很高的性能。 although it's in a multi-tasking embedded environment. 尽管它是在多任务嵌入式环境中。

Actually i need this buffer as a back up, which mean the embedded software will work as normal and save the data into the ring buffer, so far for any reason and when an error occurred, then i could have like a reference for the measured values then i will be able to have a look on them and determine the problem. 实际上,我需要此缓冲区作为备份,这意味着嵌入式软件将可以正常运行并将数据保存到环形缓冲区中,到目前为止,无论出于何种原因以及发生错误时,我都可以参考测量值然后我将可以对它们进行查看并确定问题。 Also i need to make a time stamp on the ring buffer, which mean every data (Signal value) is stored on the ring buffer will stored with the measurement's time. 另外,我需要在环形缓冲区上做一个时间戳记,这意味着存储在环形缓冲区上的每个数据(信号值)都将与测量时间一起存储。

Any code or ideas would be greatly appreciated. 任何代码或想法将不胜感激。 some of the operations required are: 所需的一些操作是:

create a ring buffer with specific size. 创建具有特定大小的环形缓冲区。 Link it with the whole software. 将其与整个软件链接。 put at the tail. 放在尾巴。 get from the head. 从头上得到。 at error, read the data and when its happen (time stamp). 错误时,读取数据及其发生的时间(时间戳)。 return the count. 返回计数。 overwrite when the buffer is being full. 当缓冲区已满时覆盖。

#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct ring_buffer
{
     void * buffer;      // data buffer
     void * buffer_end;  // end of data buffer
     void * data_start;  // pointer to head
     void * data_end;    // pointer to tail
     uint64_t capacity;  // maximum number of items in buffer
     uint64_t count;     // number of items in the buffer
     uint64_t size;      // size of each item in the buffer
 } ring_buffer;

 void rb_init (ring_buffer *rb, uint64_t size, uint64_t capacity )
 {
     rb->buffer = malloc(capacity * size);
         if(rb->buffer == NULL)
             // handle error
         rb->buffer_end = (char *)rb->buffer + capacity * size;
         rb->capacity = capacity;
         rb->count = 0;
         rb->size = size;
         rb->data_start = rb->buffer;
         rb->data_end = rb->buffer;
 }

 void cb_free(ring_buffer *rb)
 {
     free(rb->buffer);
     // clear out other fields too, just to be safe
 }

 void rb_push_back(ring_buffer *rb, const void *item)
 {
     if(rb->count == rb->capacity){
         // handle error
     }
     memcpy(rb->data_start, item, rb->size);
     rb->data_start = (char*)rb->data_start + rb->size;
     if(rb->data_start == rb->buffer_end)
         rb->data_start = rb->buffer;
     rb->count++;
 }

 void rb_pop_front(ring_buffer *rb, void *item)
 {
     if(rb->count == 0){
         // handle error
     }
     memcpy(item, rb->data_end, rb->size);
     rb->data_end = (char*)rb->data_end + rb->size;
     if(rb->data_end == rb->buffer_end)
         rb->data_end = rb->buffer;
     rb->count--;
 }

Creating a ring buffer/FIFO with hardcopies of generic type is highly questionable design for embedded systems. 对于嵌入式系统而言,创建具有通用类型的硬拷贝的环形缓冲区/ FIFO是非常有问题的设计。 You shouldn't need that high level of abstraction for code so close to the hardware. 您不需要太接近硬件的代码,就不需要那么高的抽象水平。

Either you make a ring buffer with a data type tag (like an enum) plus a void* to data allocated elsewhere, or you make a ring buffer where all data is of the same type. 您可以使用带有数据类型标签(例如枚举)和为其他位置分配的数据添加void*的环形缓冲区,也可以使用所有数据都属于同一类型的环形缓冲区。 Everything else is most likely confused program design ("XY problem"). 其他所有内容很可能会使程序设计混乱(“ XY问题”)。

You need some means to lock access to the ring buffer internally, to make it thread-safe/interrupt-safe. 您需要一些方法来在内部锁定对环形缓冲区的访问,以使其成为线程安全/中断安全的。 This, as well as the time stamp, has to be handled internally by the ring buffer ADT. 这以及时间戳,必须由环形缓冲区ADT在内部进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM