diff --git a/runtime/include/module.h b/runtime/include/module.h index 1cf5fe5..92d657b 100644 --- a/runtime/include/module.h +++ b/runtime/include/module.h @@ -49,13 +49,6 @@ struct module { mod_main_fn_t main; }; -/*************************************** - * Module Database "Methods" - ***************************************/ - -struct module *module_database__find_by_name(char *name); -struct module *module_database__find_by_socket_descriptor(int socket_descriptor); - /*************************************** * Module "Methods" ***************************************/ diff --git a/runtime/include/module_database.h b/runtime/include/module_database.h new file mode 100644 index 0000000..48521cd --- /dev/null +++ b/runtime/include/module_database.h @@ -0,0 +1,31 @@ +#ifndef SFRT_MODULE_DATABASE_H +#define SFRT_MODULE_DATABASE_H + +#include + +struct module *module_database__find_by_name(char *name); +struct module *module_database__find_by_socket_descriptor(int socket_descriptor); + +extern struct module *module_database[]; +extern int module_database_free_offset; + +/** + * Adds a module to the in-memory module DB + * Note: This was static inline, which I've unwound. I am unclear of the perf implications of this + * @param module module to add + * @return 0 on success. Aborts program on failure + **/ +static inline int +module_database__add(struct module *module) +{ + assert(module->socket_descriptor == -1); + + // __sync_fetch_and_add is provided by GCC + int f = __sync_fetch_and_add(&module_database_free_offset, 1); + assert(f < MOD_MAX); + module_database[f] = module; + + return 0; +} + +#endif /* SFRT_MODULE_DATABASE_H */ diff --git a/runtime/src/module.c b/runtime/src/module.c index bfad45e..eeb2e1f 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -1,70 +1,12 @@ #include #include +#include #include #include #include #include #include -/*************************************** - * Module Database - ***************************************/ - -// In-memory representation of all active modules -static struct module *module_database[MOD_MAX] = { NULL }; -// First free in module -static int module_database_free_offset = 0; - -/** - * Given a name, find the associated module - * @param name - * @return module or NULL if no match found - **/ -struct module * -module_database__find_by_name(char *name) -{ - int f = module_database_free_offset; - for (int i = 0; i < f; i++) { - assert(module_database[i]); - if (strcmp(module_database[i]->name, name) == 0) return module_database[i]; - } - return NULL; -} - -/** - * Given a socket_descriptor, find the associated module - * @param socket_descriptor - * @return module or NULL if no match found - **/ -struct module * -module_database__find_by_socket_descriptor(int socket_descriptor) -{ - int f = module_database_free_offset; - for (int i = 0; i < f; i++) { - assert(module_database[i]); - if (module_database[i]->socket_descriptor == socket_descriptor) return module_database[i]; - } - return NULL; -} - -/** - * Adds a module to the in-memory module DB - * @param module module to add - * @return 0 on success. Aborts program on failure - **/ -static inline int -module_database__add(struct module *module) -{ - assert(module->socket_descriptor == -1); - - // __sync_fetch_and_add is provided by GCC - int f = __sync_fetch_and_add(&module_database_free_offset, 1); - assert(f < MOD_MAX); - module_database[f] = module; - - return 0; -} - /*************************************** * Module "Methods" ***************************************/ diff --git a/runtime/src/module_database.c b/runtime/src/module_database.c new file mode 100644 index 0000000..a0cc0ed --- /dev/null +++ b/runtime/src/module_database.c @@ -0,0 +1,43 @@ +#include + + +/*************************************** + * Module Database + ***************************************/ + +// In-memory representation of all active modules +struct module *module_database[MOD_MAX] = { NULL }; +// First free in module +int module_database_free_offset = 0; + +/** + * Given a name, find the associated module + * @param name + * @return module or NULL if no match found + **/ +struct module * +module_database__find_by_name(char *name) +{ + int f = module_database_free_offset; + for (int i = 0; i < f; i++) { + assert(module_database[i]); + if (strcmp(module_database[i]->name, name) == 0) return module_database[i]; + } + return NULL; +} + +/** + * Given a socket_descriptor, find the associated module + * @param socket_descriptor + * @return module or NULL if no match found + **/ +struct module * +module_database__find_by_socket_descriptor(int socket_descriptor) +{ + int f = module_database_free_offset; + for (int i = 0; i < f; i++) { + assert(module_database[i]); + if (module_database[i]->socket_descriptor == socket_descriptor) return module_database[i]; + } + return NULL; +}