From 02492357cce066201a11993255120119c2703af1 Mon Sep 17 00:00:00 2001 From: Manuel Bachmann Date: Tue, 22 Dec 2015 16:31:39 +0100 Subject: [PATCH] Improve plugin logic, pass plugins count to session We now pre-reserve for 20 plugins (arbitrary for now, we downsize if necessary). Plugins count is now passed to the session in the "pluginCount" variable. Signed-off-by: Manuel Bachmann --- include/local-def.h | 1 + src/rest-api.c | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/local-def.h b/include/local-def.h index 68c42867..22a3608b 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -261,6 +261,7 @@ typedef struct { int fakemod; // respond to GET/POST request without interacting with sndboard int forceexit; // when autoconfig from script force exit before starting server AFB_plugin **plugins; // pointer to REST/API plugins + int pluginCount; // loaded plugins count magic_t magic; // Mime type file magic lib sigjmp_buf restartCkpt; // context save for restart set/longjmp } AFB_session; diff --git a/src/rest-api.c b/src/rest-api.c index 3d9ffeb7..a1bf039b 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -571,9 +571,10 @@ void initPlugins(AFB_session *session) { struct dirent *pluginDir; DIR *dir; afbJsonType = json_object_new_string (AFB_MSG_JTYPE); - int i = 0; + int num = 0; - plugins = (AFB_plugin **) malloc (sizeof(AFB_plugin)); + /* pre-allocate for 20 plugins, we will downsize if necessary */ + plugins = (AFB_plugin **) malloc (20*sizeof(AFB_plugin)); if ((dir = opendir(session->config->plugins)) == NULL) { fprintf(stderr, "Could not open plugin directory [%s], exiting...\n", session->config->plugins); @@ -590,7 +591,7 @@ void initPlugins(AFB_session *session) { pluginRegisterFct = dlsym (plugin, "pluginRegister"); free (pluginPath); if (!plugin) { - if (verbose) fprintf(stderr, "[%s] is not a binary plugin, continuing...\n", pluginDir->d_name); + if (verbose) fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir->d_name); continue; } else if (!pluginRegisterFct) { if (verbose) fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir->d_name); @@ -598,12 +599,14 @@ void initPlugins(AFB_session *session) { } if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading it\n", pluginDir->d_name); - plugins = (AFB_plugin **) realloc (plugins, (i+1)*sizeof(AFB_plugin)); - plugins[i] = (AFB_plugin *) malloc (sizeof(AFB_plugin)); - plugins[i] = (**pluginRegisterFct)(); - i++; + plugins[num] = (AFB_plugin *) malloc (sizeof(AFB_plugin)); + plugins[num] = (**pluginRegisterFct)(); + num++; + /* only 20 plugins are supported at that time */ + if (num == 20) break; } - plugins[i] = NULL; + plugins = (AFB_plugin **) realloc (plugins, (num+1)*sizeof(AFB_plugin)); + plugins[num] = NULL; closedir (dir); @@ -614,4 +617,5 @@ void initPlugins(AFB_session *session) { // complete plugins and save them within current sessions session->plugins = RegisterJsonPlugins(plugins); + session->pluginCount = num; } -- 2.16.6