From 019130b43cd7fca44594e9ef6543182ad191d99c Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 17 Aug 2020 17:53:25 -0400 Subject: [PATCH] chore: Cleanup module_database_dd --- runtime/include/module_database.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/runtime/include/module_database.h b/runtime/include/module_database.h index c966ed8..8336a4e 100644 --- a/runtime/include/module_database.h +++ b/runtime/include/module_database.h @@ -16,11 +16,31 @@ extern int module_database_free_offset; static inline int module_database_add(struct module *module) { + /* + * Assumption: Module is added to database before being listened to + * TODO: Why does this matter? + */ assert(module->socket_descriptor == -1); + int rc; + + if (module_database_free_offset >= MODULE_MAX_MODULE_COUNT) goto err_no_space; + int f = __sync_fetch_and_add(&module_database_free_offset, 1); + if (module_database_free_offset > MODULE_MAX_MODULE_COUNT) { + __sync_fetch_and_subtract(&module_database_free_offset, 1); + goto err_no_space; + } + + if (module_database_free_offset == MODULE_MAX_MODULE_COUNT) goto err_no_space; assert(f < MODULE_MAX_MODULE_COUNT); module_database[f] = module; - return 0; + rc = 0; +done: + return rc; +err_no_space: + debuglog("Cannot add module. Database is full.\n"); + rc = -1; + goto done; }