Improve the svm fifo allocator

- Round up requested fifo size to the next power of two
- Maintain per-segment power-of-two freelists
- Allocate fifos in chunks, to amortize alignment overhead
- Detach builtin test client application after each run
  so we can use different fifo sizes each time
- Be more suspicious of session / application indices

Useful prep work for dynamically resizing fifos. As far as the svm
fifo code is concerned, it's OK to set fifo->nitems anywhere in the
interval: [0, 1<<(fifo->freelist_index) + FIFO_SEGMENT_MIN_FIFO_SIZE]

It's unlikely that setting nitems below the path MTU will work out
very well...

Change-Id: Idad73a027dfb7412056cb02988b77e300fa7e8a7
Signed-off-by: Dave Barach <dave@barachs.net>
diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c
index 991bcd5..533a6c2 100644
--- a/src/vnet/session/session.c
+++ b/src/vnet/session/session.c
@@ -266,7 +266,13 @@
     return 0;
 
   /* Get session's server */
-  app = application_get (s->app_index);
+  app = application_get_if_valid (s->app_index);
+
+  if (PREDICT_FALSE (app == 0))
+    {
+      clib_warning ("invalid s->app_index = %d", s->app_index);
+      return 0;
+    }
 
   /* Built-in server? Hand event to the callback... */
   if (app->cb_fns.builtin_server_rx_callback)
@@ -327,8 +333,9 @@
       stream_session_t *s0;
 
       /* Get session */
-      s0 = stream_session_get (session_indices_to_enqueue[i], thread_index);
-      if (stream_session_enqueue_notify (s0, 0 /* don't block */ ))
+      s0 = stream_session_get_if_valid (session_indices_to_enqueue[i],
+					thread_index);
+      if (s0 == 0 || stream_session_enqueue_notify (s0, 0 /* don't block */ ))
 	{
 	  errors++;
 	}