From 3c7e0625485f7b13b9f0727751d6db28c84cd383 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 23 Aug 2020 14:36:13 -0400 Subject: [PATCH] refactor: cleanup module DB --- runtime/include/module_database.h | 15 ++++++++------- runtime/src/module_database.c | 10 ++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/runtime/include/module_database.h b/runtime/include/module_database.h index cf12ee4..af5e1cc 100644 --- a/runtime/include/module_database.h +++ b/runtime/include/module_database.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "debuglog.h" #include "module.h" @@ -7,29 +9,28 @@ 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; +extern size_t module_database_count; /** * Adds a module to the in-memory module DB * @param module module to add - * @return 0 on success. Aborts program on failure + * @return 0 on success. -ENOSPC when full */ static inline int module_database_add(struct module *module) { - assert(module_database_free_offset >= 0); - assert(module_database_free_offset <= MODULE_MAX_MODULE_COUNT); + assert(module_database_count <= MODULE_MAX_MODULE_COUNT); int rc; - if (module_database_free_offset == MODULE_MAX_MODULE_COUNT) goto err_no_space; - module_database[module_database_free_offset++] = module; + if (module_database_count == MODULE_MAX_MODULE_COUNT) goto err_no_space; + module_database[module_database_count++] = module; rc = 0; done: return rc; err_no_space: debuglog("Cannot add module. Database is full.\n"); - rc = -1; + rc = -ENOSPC; goto done; } diff --git a/runtime/src/module_database.c b/runtime/src/module_database.c index 9519627..fc78d6f 100644 --- a/runtime/src/module_database.c +++ b/runtime/src/module_database.c @@ -4,8 +4,8 @@ * Module Database * ******************/ -struct module *module_database[MODULE_MAX_MODULE_COUNT] = { NULL }; /* In-memory representation of all active modules */ -int module_database_free_offset = 0; /* First free in module */ +struct module *module_database[MODULE_MAX_MODULE_COUNT] = { NULL }; +size_t module_database_count = 0; /** * Given a name, find the associated module @@ -15,8 +15,7 @@ int module_database_free_offset = 0; /* First fre struct module * module_database_find_by_name(char *name) { - int f = module_database_free_offset; - for (int i = 0; i < f; i++) { + for (size_t i = 0; i < module_database_count; i++) { assert(module_database[i]); if (strcmp(module_database[i]->name, name) == 0) return module_database[i]; } @@ -31,8 +30,7 @@ module_database_find_by_name(char *name) struct module * module_database_find_by_socket_descriptor(int socket_descriptor) { - int f = module_database_free_offset; - for (int i = 0; i < f; i++) { + for (size_t i = 0; i < module_database_count; i++) { assert(module_database[i]); if (module_database[i]->socket_descriptor == socket_descriptor) return module_database[i]; }