+ oldheap = ssvm_push_heap (fsh->ssvm_sh);
+ cmem = clib_mem_alloc_aligned_at_offset (size, CLIB_CACHE_LINE_BYTES,
+ 0 /* align_offset */ ,
+ 0 /* os_out_of_memory */ );
+ ssvm_pop_heap (oldheap);
+
+ /* Out of space.. */
+ if (cmem == 0)
+ return -1;
+
+ fss = fsh_slice_get (fsh, slice_index);
+
+ /* Carve fifo + chunk space */
+ for (i = 0; i < batch_size; i++)
+ {
+ c = (svm_fifo_chunk_t *) cmem;
+ c->start_byte = 0;
+ c->length = rounded_data_size;
+ c->next = fss->free_chunks[fl_index];
+ fss->free_chunks[fl_index] = c;
+ cmem += sizeof (*c) + rounded_data_size;
+ fsh_cached_bytes_add (fsh, rounded_data_size);
+ }
+
+ fss->num_chunks[fl_index] += batch_size;
+ fss->n_fl_chunk_bytes += batch_size * rounded_data_size;
+ fsh_free_bytes_sub (fsh, size);
+
+ return 0;
+}
+
+/**
+ * Pre-allocates fifo pairs in fifo segment
+ */
+void
+fifo_segment_preallocate_fifo_pairs (fifo_segment_t * fs,
+ u32 rx_fifo_size, u32 tx_fifo_size,
+ u32 * n_fifo_pairs)
+{
+ u32 rx_rounded_data_size, tx_rounded_data_size, pair_size, pairs_to_alloc;
+ u32 hdrs, pairs_per_slice, alloc_now;
+ fifo_segment_header_t *fsh = fs->h;
+ int rx_fl_index, tx_fl_index, i;
+ fifo_segment_slice_t *fss;
+ uword space_available;
+
+ /* Parameter check */
+ if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0)
+ return;
+
+ if (!fs_chunk_size_is_valid (fsh, rx_fifo_size))
+ {
+ clib_warning ("rx fifo_size out of range %d", rx_fifo_size);
+ return;
+ }
+
+ if (!fs_chunk_size_is_valid (fsh, tx_fifo_size))
+ {
+ clib_warning ("tx fifo_size out of range %d", tx_fifo_size);
+ return;
+ }
+
+ rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size)));
+ rx_fl_index = fs_freelist_for_size (rx_fifo_size);
+ tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size)));
+ tx_fl_index = fs_freelist_for_size (tx_fifo_size);
+
+ hdrs = sizeof (svm_fifo_t) + sizeof (svm_fifo_chunk_t);
+
+ /* Calculate space requirements */
+ pair_size = 2 * hdrs + rx_rounded_data_size + tx_rounded_data_size;
+ space_available = fsh_free_space (fsh);
+ pairs_to_alloc = space_available / pair_size;
+ pairs_to_alloc = clib_min (pairs_to_alloc, *n_fifo_pairs);
+ pairs_per_slice = pairs_to_alloc / fs->n_slices;
+ pairs_per_slice += pairs_to_alloc % fs->n_slices ? 1 : 0;
+
+ if (!pairs_per_slice)
+ return;
+
+ for (i = 0; i < fs->n_slices; i++)
+ {
+ fss = fsh_slice_get (fsh, i);
+ alloc_now = clib_min (pairs_per_slice, *n_fifo_pairs);
+ if (fs_try_alloc_fifo_batch (fsh, fss, rx_fl_index, alloc_now))
+ clib_warning ("rx prealloc failed: pairs %u", alloc_now);
+ if (fs_try_alloc_fifo_batch (fsh, fss, tx_fl_index, alloc_now))
+ clib_warning ("tx prealloc failed: pairs %u", alloc_now);
+
+ /* Account for the pairs allocated */
+ *n_fifo_pairs -= alloc_now;
+ }
+}
+
+/**
+ * Get number of active fifos
+ */
+u32
+fifo_segment_num_fifos (fifo_segment_t * fs)
+{
+ return fsh_n_active_fifos (fs->h);
+}