refactor: cleanup module DB

main
Sean McBride 4 years ago
parent e0a80711f9
commit 3c7e062548

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <errno.h>
#include "debuglog.h" #include "debuglog.h"
#include "module.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); struct module *module_database_find_by_socket_descriptor(int socket_descriptor);
extern struct module *module_database[]; 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 * Adds a module to the in-memory module DB
* @param module module to add * @param module module to add
* @return 0 on success. Aborts program on failure * @return 0 on success. -ENOSPC when full
*/ */
static inline int static inline int
module_database_add(struct module *module) module_database_add(struct module *module)
{ {
assert(module_database_free_offset >= 0); assert(module_database_count <= MODULE_MAX_MODULE_COUNT);
assert(module_database_free_offset <= MODULE_MAX_MODULE_COUNT);
int rc; int rc;
if (module_database_free_offset == MODULE_MAX_MODULE_COUNT) goto err_no_space; if (module_database_count == MODULE_MAX_MODULE_COUNT) goto err_no_space;
module_database[module_database_free_offset++] = module; module_database[module_database_count++] = module;
rc = 0; rc = 0;
done: done:
return rc; return rc;
err_no_space: err_no_space:
debuglog("Cannot add module. Database is full.\n"); debuglog("Cannot add module. Database is full.\n");
rc = -1; rc = -ENOSPC;
goto done; goto done;
} }

@ -4,8 +4,8 @@
* Module Database * * Module Database *
******************/ ******************/
struct module *module_database[MODULE_MAX_MODULE_COUNT] = { NULL }; /* In-memory representation of all active modules */ struct module *module_database[MODULE_MAX_MODULE_COUNT] = { NULL };
int module_database_free_offset = 0; /* First free in module */ size_t module_database_count = 0;
/** /**
* Given a name, find the associated module * Given a name, find the associated module
@ -15,8 +15,7 @@ int module_database_free_offset = 0; /* First fre
struct module * struct module *
module_database_find_by_name(char *name) module_database_find_by_name(char *name)
{ {
int f = module_database_free_offset; for (size_t i = 0; i < module_database_count; i++) {
for (int i = 0; i < f; i++) {
assert(module_database[i]); assert(module_database[i]);
if (strcmp(module_database[i]->name, name) == 0) return 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 * struct module *
module_database_find_by_socket_descriptor(int socket_descriptor) module_database_find_by_socket_descriptor(int socket_descriptor)
{ {
int f = module_database_free_offset; for (size_t i = 0; i < module_database_count; i++) {
for (int i = 0; i < f; i++) {
assert(module_database[i]); assert(module_database[i]);
if (module_database[i]->socket_descriptor == socket_descriptor) return module_database[i]; if (module_database[i]->socket_descriptor == socket_descriptor) return module_database[i];
} }

Loading…
Cancel
Save