+ app_worker_t *app_wrk;
+ pool_get (app_main.workers, app_wrk);
+ clib_memset (app_wrk, 0, sizeof (*app_wrk));
+ app_wrk->wrk_index = app_wrk - app_main.workers;
+ app_wrk->app_index = app->app_index;
+ app_wrk->wrk_map_index = ~0;
+ app_wrk->connects_seg_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
+ app_wrk->first_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
+ app_wrk->local_segment_manager = APP_INVALID_SEGMENT_MANAGER_INDEX;
+ APP_DBG ("New app %v worker %u", app_get_name (app), app_wrk->wrk_index);
+ return app_wrk;
+}
+
+app_worker_t *
+app_worker_get (u32 wrk_index)
+{
+ return pool_elt_at_index (app_main.workers, wrk_index);
+}
+
+app_worker_t *
+app_worker_get_if_valid (u32 wrk_index)
+{
+ if (pool_is_free_index (app_main.workers, wrk_index))
+ return 0;
+ return pool_elt_at_index (app_main.workers, wrk_index);
+}
+
+void
+app_worker_free (app_worker_t * app_wrk)
+{
+ application_t *app = application_get (app_wrk->app_index);
+ vnet_unbind_args_t _a, *a = &_a;
+ u64 handle, *handles = 0;
+ segment_manager_t *sm;
+ u32 sm_index;
+ int i;
+
+ /*
+ * Listener cleanup
+ */
+
+ /* *INDENT-OFF* */
+ hash_foreach (handle, sm_index, app_wrk->listeners_table,
+ ({
+ vec_add1 (handles, handle);
+ sm = segment_manager_get (sm_index);
+ sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ }));
+ /* *INDENT-ON* */
+
+ for (i = 0; i < vec_len (handles); i++)
+ {
+ a->app_index = app->app_index;
+ a->wrk_map_index = app_wrk->wrk_map_index;
+ a->handle = handles[i];
+ /* seg manager is removed when unbind completes */
+ vnet_unbind (a);
+ }
+
+ /*
+ * Connects segment manager cleanup
+ */
+
+ if (app_wrk->connects_seg_manager != APP_INVALID_SEGMENT_MANAGER_INDEX)
+ {
+ sm = segment_manager_get (app_wrk->connects_seg_manager);
+ sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ segment_manager_init_del (sm);
+ }
+
+ /* If first segment manager is used by a listener */
+ if (app_wrk->first_segment_manager != APP_INVALID_SEGMENT_MANAGER_INDEX
+ && app_wrk->first_segment_manager != app_wrk->connects_seg_manager)
+ {
+ sm = segment_manager_get (app_wrk->first_segment_manager);
+ sm->first_is_protected = 0;
+ sm->app_wrk_index = SEGMENT_MANAGER_INVALID_APP_INDEX;
+ /* .. and has no fifos, e.g. it might be used for redirected sessions,
+ * remove it */
+ if (!segment_manager_has_fifos (sm))
+ segment_manager_del (sm);
+ }
+
+ /*
+ * Local sessions
+ */
+ app_worker_local_sessions_free (app_wrk);
+
+ pool_put (app_main.workers, app_wrk);
+ if (CLIB_DEBUG)
+ clib_memset (app_wrk, 0xfe, sizeof (*app_wrk));
+}
+
+int
+app_worker_alloc_and_init (application_t * app, app_worker_t ** wrk)
+{
+ app_worker_map_t *wrk_map;
+ app_worker_t *app_wrk;
+ segment_manager_t *sm;
+ int rv;
+
+ app_wrk = app_worker_alloc (app);
+ wrk_map = app_worker_map_alloc (app);
+ wrk_map->wrk_index = app_wrk->wrk_index;
+ app_wrk->wrk_map_index = app_worker_map_index (app, wrk_map);
+
+ /*
+ * Setup first segment manager
+ */
+ sm = segment_manager_new ();
+ sm->app_wrk_index = app_wrk->wrk_index;
+
+ if ((rv = segment_manager_init (sm, app->sm_properties.segment_size,
+ app->sm_properties.prealloc_fifos)))
+ {
+ app_worker_free (app_wrk);
+ return rv;
+ }
+ sm->first_is_protected = 1;
+
+ /*
+ * Setup app worker
+ */
+ app_wrk->first_segment_manager = segment_manager_index (sm);
+ app_wrk->listeners_table = hash_create (0, sizeof (u64));
+ app_wrk->event_queue = segment_manager_event_queue (sm);
+ app_wrk->app_is_builtin = application_is_builtin (app);
+
+ /*
+ * Segment manager for local sessions
+ */
+ sm = segment_manager_new ();
+ sm->app_wrk_index = app_wrk->wrk_index;
+ app_wrk->local_segment_manager = segment_manager_index (sm);
+ app_wrk->local_connects = hash_create (0, sizeof (u64));
+
+ *wrk = app_wrk;
+
+ return 0;
+}
+
+application_t *
+app_worker_get_app (u32 wrk_index)
+{
+ app_worker_t *app_wrk;
+ app_wrk = app_worker_get_if_valid (wrk_index);
+ if (!app_wrk)
+ return 0;
+ return application_get_if_valid (app_wrk->app_index);