+static int
+fs_try_alloc_fifo_batch (fifo_segment_header_t * fsh,
+ fifo_segment_slice_t * fss,
+ u32 fl_index, u32 batch_size)
+{
+ if (fsh_try_alloc_fifo_hdr_batch (fsh, fss, batch_size))
+ return 0;
+ return fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch_size);
+}
+
+static svm_fifo_shared_t *
+fsh_try_alloc_fifo_hdr (fifo_segment_header_t *fsh, fifo_segment_slice_t *fss)
+{
+ svm_fifo_shared_t *sf;
+
+ if (!fss->free_fifos)
+ {
+ if (fsh_try_alloc_fifo_hdr_batch (fsh, fss,
+ FIFO_SEGMENT_ALLOC_BATCH_SIZE))
+ return 0;
+ }
+
+ sf = fss_fifo_free_list_pop (fsh, fss);
+ clib_memset (sf, 0, sizeof (*sf));
+
+ return sf;
+}
+
+static svm_fifo_chunk_t *
+fsh_try_alloc_chunk (fifo_segment_header_t * fsh,
+ fifo_segment_slice_t * fss, u32 data_bytes)
+{
+ svm_fifo_chunk_t *c;
+ u32 fl_index;
+
+ fl_index = fs_freelist_for_size (data_bytes);
+
+free_list:
+ c = fss_chunk_free_list_pop (fsh, fss, fl_index);
+ if (c)
+ {
+ c->next = 0;
+ fss_fl_chunk_bytes_sub (fss, fs_freelist_index_to_size (fl_index));
+ fsh_cached_bytes_sub (fsh, fs_freelist_index_to_size (fl_index));
+ }
+ else
+ {
+ u32 chunk_size, batch = FIFO_SEGMENT_ALLOC_BATCH_SIZE;
+ uword n_free;
+
+ chunk_size = fs_freelist_index_to_size (fl_index);
+ n_free = fsh_n_free_bytes (fsh);
+
+ if (chunk_size <= n_free)
+ {
+ batch = chunk_size * batch <= n_free ? batch : 1;
+ if (!fsh_try_alloc_chunk_batch (fsh, fss, fl_index, batch))
+ goto free_list;
+ }
+ /* Failed to allocate larger chunk, try to allocate multi-chunk
+ * that is close to what was actually requested */
+ if (data_bytes <= fss_fl_chunk_bytes (fss))
+ {
+ c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
+ if (c)
+ goto done;
+ batch = n_free / FIFO_SEGMENT_MIN_FIFO_SIZE;
+ if (!batch || fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
+ goto done;
+ }
+ if (data_bytes <= fss_fl_chunk_bytes (fss) + n_free)
+ {
+ u32 min_size = FIFO_SEGMENT_MIN_FIFO_SIZE;
+ if (n_free < min_size)
+ goto done;
+ batch = (data_bytes - fss_fl_chunk_bytes (fss)) / min_size;
+ batch = clib_min (batch + 1, n_free / min_size);
+ if (fsh_try_alloc_chunk_batch (fsh, fss, 0, batch))
+ goto done;
+ c = fs_try_alloc_multi_chunk (fsh, fss, data_bytes);
+ }
+ }
+
+done:
+
+ return c;
+}
+