From 9a1f16ae3affc4db83f2eb2623418ac5cff2af03 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sat, 09 Mar 2019 20:07:56 +0000
Subject: [PATCH] Code cleanup

---
 src/plugins.c |   65 ++++++++++++++++++++++++++++++--
 1 files changed, 60 insertions(+), 5 deletions(-)

diff --git a/src/plugins.c b/src/plugins.c
index 8947499..3146c2a 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -95,14 +95,18 @@
 
 ts_plugin *init_plugin(ts_char *filename){
 	ts_plugin *plugin = (ts_plugin *)calloc(1, sizeof(ts_plugin));
-	plugin->filename=(ts_char *)calloc(strlen(filename),sizeof(ts_char));
+	plugin->filename=(ts_char *)calloc(strlen(filename)+1,sizeof(ts_char));
 	strcpy(plugin->filename,filename);
 	plugin->libhandle = dlopen(plugin->filename, RTLD_NOW);
 	if(!plugin->libhandle){
-		ts_fprintf(stderr,"Error loading plugin: %s\n", filename);
+		ts_fprintf(stderr,"Error loading plugin: %s\n", plugin->filename);
 		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", plugin->filename);
+		fatal("Exiting", 236);
+	}
 	plugin->details = get_plugin_details();
 	plugin->function = (ts_plugin_function *)calloc(1,sizeof(ts_plugin_function));
 	
@@ -110,8 +114,11 @@
 	plugin->function->at_start = dlsym(plugin->libhandle, "at_start");
 	plugin->function->after_vesicle_init = dlsym(plugin->libhandle, "after_vesicle_init");
 	plugin->function->vm_hard_constraint = dlsym(plugin->libhandle, "vm_hard_constraint");
-	plugin->function->vm_energy_before = dlsym(plugin->libhandle, "vm_energy_before");
-	plugin->function->vm_energy_after = dlsym(plugin->libhandle, "vm_energy_after");
+	plugin->function->vm_energy_before_prepare = dlsym(plugin->libhandle, "vm_energy_before_prepare");
+	plugin->function->vm_energy_before_execute = dlsym(plugin->libhandle, "vm_energy_before_execute");
+	plugin->function->vm_energy_after_prepare = dlsym(plugin->libhandle, "vm_energy_after_prepare");
+	plugin->function->vm_energy_after_execute = dlsym(plugin->libhandle, "vm_energy_after_execute");
+	plugin->function->vm_before_montecarlo_constraint = dlsym(plugin->libhandle, "vm_before_montecarlo_constraint");
 	plugin->function->vm_new_state_rejected = dlsym(plugin->libhandle, "vm_new_state_rejected");
 	plugin->function->vm_new_state_accepted = dlsym(plugin->libhandle, "vm_new_state_accepted");
 
@@ -119,12 +126,59 @@
 }
 
 
+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]);
+		}
+		if(plist->plugin[i]->function->after_vesicle_init!=NULL){
+			plist->chain->after_vesicle_init=plugin_to_chain(plist->chain->after_vesicle_init, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_hard_constraint!=NULL){
+			plist->chain->vm_hard_constraint=plugin_to_chain(plist->chain->vm_hard_constraint, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_energy_before_prepare!=NULL){
+			plist->chain->vm_energy_before_prepare=plugin_to_chain(plist->chain->vm_energy_before_prepare, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_energy_after_execute!=NULL){
+			plist->chain->vm_energy_after_execute=plugin_to_chain(plist->chain->vm_energy_after_execute, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_before_montecarlo_constraint!=NULL){
+			plist->chain->vm_before_montecarlo_constraint=plugin_to_chain(plist->chain->vm_before_montecarlo_constraint, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_new_state_accepted!=NULL){
+			plist->chain->vm_new_state_accepted=plugin_to_chain(plist->chain->vm_new_state_accepted, plist->plugin[i]);
+		}
+		if(plist->plugin[i]->function->vm_new_state_rejected!=NULL){
+			plist->chain->vm_new_state_rejected=plugin_to_chain(plist->chain->vm_new_state_rejected, plist->plugin[i]);
+		}
 	}
+	plist->n=number_of_plugins;
 	return plist;
 }	
 
@@ -137,9 +191,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