+ srd_dbg("Freeing instance %s", di->inst_id);
+
+ Py_DecRef(di->py_inst);
+ g_free(di->inst_id);
+ g_free(di->dec_probemap);
+ g_slist_free(di->next_di);
+ for (l = di->pd_output; l; l = l->next) {
+ pdo = l->data;
+ g_free(pdo->proto_id);
+ g_free(pdo);
+ }
+ g_slist_free(di->pd_output);
+ g_free(di);
+}
+
+/** @private */
+SRD_PRIV void srd_inst_free_all(struct srd_session *sess, GSList *stack)
+{
+ GSList *l;
+ struct srd_decoder_inst *di;
+
+ if (session_is_valid(sess) != SRD_OK) {
+ srd_err("Invalid session.");
+ return;
+ }
+
+ di = NULL;
+ for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
+ di = l->data;
+ if (di->next_di)
+ srd_inst_free_all(sess, di->next_di);
+ srd_inst_free(di);
+ }
+ if (!stack) {
+ g_slist_free(sess->di_list);
+ sess->di_list = NULL;
+ }
+}
+
+/** @} */
+
+/**
+ * @defgroup grp_session Session handling
+ *
+ * Starting and handling decoding sessions.
+ *
+ * @{
+ */
+
+static int session_is_valid(struct srd_session *sess)
+{
+
+ if (!sess || sess->session_id < 1)
+ return SRD_ERR;
+
+ return SRD_OK;
+}
+
+/**
+ * Create a decoding session.
+ *
+ * A session holds all decoder instances, their stack relationships and
+ * output callbacks.
+ *
+ * @param sess A pointer which will hold a pointer to a newly
+ * initialized session on return.
+ *
+ * @return SRD_OK upon success, a (negative) error code otherwise.
+ *
+ * @since 0.3.0
+ */
+SRD_API int srd_session_new(struct srd_session **sess)
+{
+
+ if (!sess) {
+ srd_err("Invalid session pointer.");
+ return SRD_ERR_ARG;
+ }
+
+ if (!(*sess = g_try_malloc(sizeof(struct srd_session))))
+ return SRD_ERR_MALLOC;
+ (*sess)->session_id = ++max_session_id;
+ (*sess)->di_list = (*sess)->callbacks = NULL;
+
+ /* Keep a list of all sessions, so we can clean up as needed. */
+ sessions = g_slist_append(sessions, *sess);
+
+ srd_dbg("Created session %d.", (*sess)->session_id);
+
+ return SRD_OK;
+}
+
+/**
+ * Start a decoding session.
+ *
+ * Decoders, instances and stack must have been prepared beforehand,
+ * and all SRD_CONF parameters set.
+ *
+ * @param sess The session to start.
+ *
+ * @return SRD_OK upon success, a (negative) error code otherwise.
+ *
+ * @since 0.3.0
+ */
+SRD_API int srd_session_start(struct srd_session *sess)