简体   繁体   English

非连续数据结构

[英]Non Contiguous Data Structure

I'm working on a bit of code that pulls data from various sensors and stores that data on an SD card. 我正在处理一些代码,这些代码从各种传感器中提取数据并将其存储在SD卡上。 All sensor values belong to at least one group (or "table"). 所有传感器值都属于至少一组(或“表”)。 For example, there is a "voltages" table with all of the voltages. 例如,有一个包含所有电压的“电压”表。 Only entire tables can be received from the sensors and only entire tables can be saved to the SD card. 只能从传感器接收整个表,并且只能将整个表保存到SD卡。

All sensor values are stored in one giant struct. 所有传感器值都存储在一个巨型结构中。 The struct is contiguous but the members of a specific table are not necessarily contiguous. 该结构是连续的,但特定表的成员不一定是连续的。 For example, 例如,

Value Name  |   Table Number
-----------------------------
Value 1     |   1
Value 2     |   1
Value 3     |     2
Value 4     |     2
Value 5     |   1
Value 6     |     2
Value 7     |   1

I'm using a real time kernal. 我正在使用实时内核。 I want to have one task that maintains the various tables and one task that saves things to the SD card. 我想执行一项维护各种表的任务,以及一项将内容保存到SD卡的任务。 When a given table is requested, I want the "table task" to pass some kind of handle to the "SD card task". 当请求给定的表时,我希望“表任务”将某种类型的句柄传递给“ SD卡任务”。

So my question is: Is there a reasonably simple way to create a handle for a data structure that is non-contiguous? 所以我的问题是:是否存在一种合理的简单方法来为不连续的数据结构创建句柄?

Here is an example of the entire struct of sensor values (in reality there are about 600 values and 200 tables. Tables can overlap): 这是传感器值的整个结构的一个示例(实际上大约有600个值和200个表。表可以重叠):

typedef struct __attribute__ ((__packed__)) {
    uint8_t  vsys[3];    // Member of Voltage table
    uint16_t cursys[3];  // Member of Current table
    uint32_t cur1;   // Member of Current table
    uint16_t v1;         // Member of Voltage table
    uint16_t cur2;   // Member of Current table
} housekeeping_t;

Functions would be (for example) hk_handle_t get_voltages(); 函数将是(例如) hk_handle_t get_voltages(); or hk_handle_t get_currents(); hk_handle_t get_currents();

I can not change how the data is collected from the sensors. 我无法更改如何从传感器收集数据。 Only how it gets to the SD card. 只有它如何到达SD卡。

Reliability is valued above all else 可靠性高于一切

I take it you want to pass an (array-of-)structures to a function such that it serializes a parametrized sub-set of the fields? 我认为您想将(array-of-)结构传递给函数,以便它对字段的参数化子集进行序列化?

One way of doing this would be to pass an array of field descriptors, containing the relative member offsets and sizes, to the generic functions. 一种方法是将包含相对成员偏移量和大小的字段描述符数组传递给泛型函数。 In C(++) you may take advantage of the offsetof macro to help you with this. 在C(++)中,您可以利用offsetof宏来帮助您实现这一点。

Eg something along these lines: 例如,遵循以下原则:

struct record {
    int apple1, orange1;
    int apple2, orange2;
};

struct field { size_t offset, size; };
#define FIELD(id) { offsetof(struct record, id), sizeof(struct record, id) }
#define SENTINEL() { 0, 0 }

const struct field apples[] = { FIELD(apple1), FIELD(apple2), SENTINEL() };
const struct field oranges[] = { FIELD(orange1), FIELD(orange2), SENTINEL() };

void write_subset(FILE *file, const struct record *record, const struct field *fields) {
    for(; fields->size; ++fields)
        fwrite((const char *) record + fields->offset, fields->size, 1, file);
}

Naturally there are many alternative options. 自然地,有许多替代选择。 You might include type specifiers to do portable I/O, or create raw byte masks, or define a function per field set with callbacks for the I/O task, etc. As you might imagine it is difficult to good advice without more specifics. 您可能包括类型说明符,以进行可移植的I / O,或创建原始字节掩码,或为每个带有I / O任务的回调的字段集定义函数,等等。您可能会想像,如果没有更多细节,很难给出好的建议。

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

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