From ca9f26089df29fec5995ed4f19a06c29f7f4b12e Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Thu, 28 Feb 2019 18:55:40 +0000
Subject: [PATCH] Plugin chains to speed up running of plugins, part 1.

---
 src/plugins.c |   34 +++++++++++++++++++++++++++++++++-
 1 files changed, 33 insertions(+), 1 deletions(-)

diff --git a/src/plugins.c b/src/plugins.c
index 9464a6b..84983aa 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -103,6 +103,10 @@
 		fatal("Exiting",235);
 	}
 	ts_plugin_details* (*get_plugin_details)(void) = dlsym(plugin->libhandle, "init");
+	if(get_plugin_details==NULL){
+		ts_fprintf(stderr,"Plugin %s invalid. No init() function.\n", filename);
+		fatal("Exiting", 235);
+	}
 	plugin->details = get_plugin_details();
 	plugin->function = (ts_plugin_function *)calloc(1,sizeof(ts_plugin_function));
 	
@@ -121,12 +125,39 @@
 }
 
 
+ts_plugin_chain *plugin_to_chain(ts_plugin_chain *chain, ts_plugin *plugin){
+	ts_plugin_chain *retchain;
+	if(chain!=NULL){
+		retchain=chain;
+		while(chain->next!=NULL){
+			chain=chain->next;
+		}
+		chain->next=(ts_plugin_chain *)calloc(1,sizeof(ts_plugin_chain));
+		chain->next->plugin=plugin;
+		return retchain;
+	} else {
+		chain=(ts_plugin_chain *)calloc(1,sizeof(ts_plugin_chain));
+		chain->plugin=plugin;
+		return chain;
+	}
+}
+
+
 ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_plugins){
 	ts_plugin_list *plist=(ts_plugin_list *)calloc(1, sizeof(ts_plugin_list));
 	plist->plugin=(ts_plugin **)calloc(number_of_plugins, sizeof(ts_plugin *));
+	plist->chain=(ts_plugin_chains *)calloc(1, sizeof(ts_plugin_chains));
 	for(int i=0;i<number_of_plugins;i++){
 		plist->plugin[i]=init_plugin(plugin_filenames[i]);
+		if(plist->plugin[i]->function->cleanup!=NULL){
+			plist->chain->cleanup=plugin_to_chain(plist->chain->cleanup, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->at_start!=NULL){
+			plist->chain->at_start=plugin_to_chain(plist->chain->at_start, plist->plugin[i]);
+		}
+
 	}
+	//printf("%s", plist->chain->at_start->next->plugin->filename);
 	plist->n=number_of_plugins;
 	return plist;
 }	
@@ -140,9 +171,10 @@
 	free(plugin);
 }
 
-void free_plugin_list(ts_plugin_list *plist){
+void plugin_list_free(ts_plugin_list *plist){
 	for(int i=0;i<plist->n;i++){
 		free_plugin(plist->plugin[i]);
 	}
+	free(plist->chain);
 	free(plist);
 }

--
Gitblit v1.9.3