Skip to content

Commit 72b51ce

Browse files
committed
modules: storage: Move from FIFO to pipe
Use pipe internally instead of FIFO. This aboids dynamic allocation and requirement of the consumer freeing data. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent a00571a commit 72b51ce

File tree

9 files changed

+1206
-402
lines changed

9 files changed

+1206
-402
lines changed

app/src/modules/storage/Kconfig.storage

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ config APP_STORAGE_RAM_LIMIT_KB
7070
MAX_TYPES * MAX_RECORDS_PER_TYPE * RECORD_SIZE bytes.
7171
Set this based on your system's available RAM.
7272

73-
config APP_STORAGE_FIFO_ITEM_COUNT
74-
int "FIFO item count"
75-
default 20
73+
config APP_STORAGE_BATCH_BUFFER_SIZE
74+
int "Storage batch buffer size in bytes"
75+
default 1024
7676
help
77-
Maximum number of memory slabs available to use in the storage module's FIFO.
78-
The FIFO is used when data is sent from the storage module to other modules.
79-
This option is also used to set the number of each registered data type that can be stored
80-
in the FIFO.
81-
For example, if set to 20, there will be 20 memory slabs available for each registered
82-
data type, and a total of 20 items can be stored in the FIFO at any time.
77+
Size of the internal buffer for batch data access.
78+
This buffer is used to transfer data from the storage backend
79+
to consuming modules via the batch interface.
80+
81+
Larger buffers can improve throughput but use more RAM.
82+
Must be large enough to hold at least one complete data item
83+
plus its header.
8384

8485
if APP_STORAGE_BACKEND_INTERNAL_FLASH
8586

@@ -133,7 +134,6 @@ config APP_STORAGE_INITIAL_MODE_BUFFER
133134

134135
endchoice
135136

136-
137137
config APP_STORAGE_SHELL
138138
bool "Enable storage shell commands"
139139
default y

app/src/modules/storage/backends/ram_ring_buffer_backend.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static int ram_init(void)
142142

143143
LOG_DBG("RAM backend initialized with %d types, using %zu bytes of RAM",
144144
ctx.num_registered_types,
145-
sizeof(t->data_size) * RECORDS_PER_TYPE);
145+
t->data_size * (size_t)RECORDS_PER_TYPE);
146146

147147
LOG_DBG("Ring buffer %s initialized with size %u, item size: %zu", t->name,
148148
ring_buf_capacity_get(get_ring_buf_ptr(idx)), t->data_size);
@@ -217,6 +217,62 @@ static int ram_store(const struct storage_data *type, const void *data, size_t s
217217
return 0;
218218
}
219219

220+
/**
221+
* @brief Peek at data from the RAM backend without removing it
222+
*
223+
* Returns the size of the next item without copying data (if data is NULL)
224+
* or copies the data if data buffer is provided.
225+
*
226+
* @param type Storage data type to peek at
227+
* @param data Pointer where the peeked data will be stored (can be NULL for size-only)
228+
* @param size Size of the data buffer in bytes (ignored if data is NULL)
229+
* @return Number of bytes that would be read on success, negative errno on failure
230+
*/
231+
static int ram_peek(const struct storage_data *type, void *data, size_t size)
232+
{
233+
struct ring_buf *ring_buf;
234+
int idx;
235+
uint32_t bytes_peeked;
236+
237+
if (!type) {
238+
return -EINVAL;
239+
}
240+
241+
idx = get_type_index(type);
242+
if (idx < 0) {
243+
return -EINVAL;
244+
}
245+
246+
ring_buf = get_ring_buf_ptr(idx);
247+
248+
if (ring_buf_is_empty(ring_buf)) {
249+
return -EAGAIN;
250+
}
251+
252+
/* If data is NULL, just return the size without copying */
253+
if (data == NULL) {
254+
return (int)type->data_size;
255+
}
256+
257+
/* Data buffer provided - ensure it's large enough */
258+
if (type->data_size > size) {
259+
LOG_ERR("Buffer too small for data: needed %zu, have %zu", type->data_size, size);
260+
261+
return -ENOMEM;
262+
}
263+
264+
/* Peek at the data without removing it */
265+
bytes_peeked = ring_buf_peek(ring_buf, data, (uint32_t)type->data_size);
266+
if (bytes_peeked != (uint32_t)type->data_size) {
267+
LOG_ERR("Failed to peek data: expected %zu bytes, got %u",
268+
type->data_size, bytes_peeked);
269+
270+
return -EIO;
271+
}
272+
273+
return (int)bytes_peeked;
274+
}
275+
220276
/**
221277
* @brief Retrieve data from the RAM backend
222278
*
@@ -347,6 +403,7 @@ static int ram_clear(void)
347403
static const struct storage_backend ram_backend = {
348404
.init = ram_init,
349405
.store = ram_store,
406+
.peek = ram_peek,
350407
.retrieve = ram_retrieve,
351408
.count = ram_records_count,
352409
.clear = ram_clear,

0 commit comments

Comments
 (0)