+ u32 head, tail;
+
+ f_load_head_tail_all_acq (f, &head, &tail);
+
+ if (!f->shr->head_chunk || !f_chunk_includes_pos (f_head_cptr (f), head))
+ f->shr->head_chunk = f_csptr (f, svm_fifo_find_chunk (f, head));
+
+ f->shr->head_chunk =
+ f_csptr (f, f_lookup_clear_deq_chunks (f, f_head_cptr (f), tail));
+
+ if (f_pos_geq (tail, f_chunk_end (f_start_cptr (f))))
+ fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
+ f_unlink_chunks (f, tail, 0));
+
+ /* store-rel: consumer owned index (paired with load-acq in producer) */
+ clib_atomic_store_rel_n (&f->shr->head, tail);
+}
+
+int
+svm_fifo_fill_chunk_list (svm_fifo_t * f)
+{
+ u32 head, tail;
+
+ f_load_head_tail_prod (f, &head, &tail);
+
+ if (f_chunk_end (f_end_cptr (f)) - head >= f->shr->size)
+ return 0;
+
+ if (f_try_chunk_alloc (f, head, tail, f->shr->size - (tail - head)))
+ return SVM_FIFO_EGROW;
+
+ return 0;
+}
+
+int
+svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs,
+ u32 len)
+{
+ u32 head, tail, n_avail, head_pos, n_bytes, fs_index = 1, clen;
+ svm_fifo_chunk_t *c;
+
+ f_load_head_tail_prod (f, &head, &tail);
+
+ if (f_free_count (f, head, tail) < len)
+ return SVM_FIFO_EFULL;
+
+ n_avail = f_chunk_end (f_end_cptr (f)) - tail;
+
+ if (n_avail < len && f_try_chunk_alloc (f, head, tail, len))
+ return SVM_FIFO_EGROW;
+
+ if (!fs || !n_segs)
+ return 0;
+
+ c = f_tail_cptr (f);
+ head_pos = (tail - c->start_byte);
+ fs[0].data = c->data + head_pos;
+ fs[0].len = clib_min (c->length - head_pos, len);
+ n_bytes = fs[0].len;
+
+ while (n_bytes < len && fs_index < n_segs)
+ {
+ c = f_cptr (f, c->next);
+ clen = clib_min (c->length, len - n_bytes);
+ fs[fs_index].data = c->data;
+ fs[fs_index].len = clen;
+ n_bytes += clen;
+ fs_index += 1;
+ }
+
+ return fs_index;
+}
+
+int
+svm_fifo_segments (svm_fifo_t *f, u32 offset, svm_fifo_seg_t *fs, u32 *n_segs,
+ u32 max_bytes)
+{
+ u32 cursize, to_read, head, tail, fs_index = 1;
+ u32 n_bytes, head_pos, len, start;
+ svm_fifo_chunk_t *c;
+
+ f_load_head_tail_cons (f, &head, &tail);
+
+ /* consumer function, cursize can only increase while we're working */
+ cursize = f_cursize (f, head, tail);
+
+ if (PREDICT_FALSE (cursize == 0))
+ return SVM_FIFO_EEMPTY;
+
+ if (offset >= cursize)
+ return SVM_FIFO_EEMPTY;
+
+ to_read = clib_min (cursize - offset, max_bytes);
+ start = head + offset;
+
+ if (!f->shr->head_chunk)
+ f->shr->head_chunk = f_csptr (f, svm_fifo_find_chunk (f, head));
+
+ c = f_head_cptr (f);
+
+ while (!f_chunk_includes_pos (c, start))
+ c = f_cptr (f, c->next);
+
+ head_pos = start - c->start_byte;
+ fs[0].data = c->data + head_pos;
+ fs[0].len = clib_min (c->length - head_pos, to_read);
+ n_bytes = fs[0].len;
+
+ while (n_bytes < to_read && fs_index < *n_segs)
+ {
+ c = f_cptr (f, c->next);
+ len = clib_min (c->length, to_read - n_bytes);
+ fs[fs_index].data = c->data;
+ fs[fs_index].len = len;
+ n_bytes += len;
+ fs_index += 1;
+ }
+ *n_segs = fs_index;
+
+ return n_bytes;
+}
+
+/**
+ * Clones fifo
+ *
+ * Assumptions:
+ * - no prod and cons are accessing either dest or src fifo
+ * - fifo is not multi chunk
+ */
+void
+svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
+{
+ u32 head, tail;
+
+ /* Support only single chunk clones for now */
+ ASSERT (svm_fifo_n_chunks (sf) == 1);
+
+ clib_memcpy_fast (f_head_cptr (df)->data, f_head_cptr (sf)->data,
+ f_head_cptr (sf)->length);
+
+ f_load_head_tail_all_acq (sf, &head, &tail);
+ clib_atomic_store_rel_n (&df->shr->head, head);
+ clib_atomic_store_rel_n (&df->shr->tail, tail);