Skip to content

Commit

Permalink
Small improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDP committed Jul 20, 2019
1 parent f75aa57 commit de43f79
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
26 changes: 10 additions & 16 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ static void default_logger(const self_t *self, const char *fmt, va_list args, co
static module_ret_code _register_fd(module *mod, const int fd, const bool autoclose, const void *userptr);
static module_ret_code _deregister_fd(module *mod, const int fd);
static int manage_fds(module *mod, m_context *c, const int flag, const bool stop);
static map_ret_code subscribtions_dump(void *data, const char *key, void *value);

static module_ret_code init_ctx(const char *ctx_name, m_context **context) {
MODULE_DEBUG("Creating context '%s'.\n", ctx_name);
Expand Down Expand Up @@ -158,13 +157,6 @@ static int manage_fds(module *mod, m_context *c, const int flag, const bool stop
return ret;
}

static map_ret_code subscribtions_dump(void *data, const char *key, void *value) {
const self_t *self = (self_t *) data;

module_log(self, "-> %s: %p\n", key, value);
return MAP_OK;
}

/** Private API **/

bool _module_is(const module *mod, const enum module_states st) {
Expand Down Expand Up @@ -314,7 +306,8 @@ module_ret_code module_register(const char *name, const char *ctx_name, self_t *
ret = MOD_ERR;
break;
}
memhook._free((self_t *)*self);
memhook._free(*self);
*self = NULL;
map_free(mod->subscriptions);
stack_free(mod->recvs);
memhook._free(mod);
Expand All @@ -324,24 +317,23 @@ module_ret_code module_register(const char *name, const char *ctx_name, self_t *
module_ret_code module_deregister(self_t **self) {
MOD_PARAM_ASSERT(self);

self_t *tmp = (self_t *) *self;
GET_MOD(tmp);
GET_MOD(*self);
MODULE_DEBUG("Deregistering module '%s'.\n", mod->name);

stop(mod, true);

/* Remove the module from the context */
map_remove(tmp->ctx->modules, mod->name);
map_remove((*self)->ctx->modules, mod->name);

/* Remove context without modules */
if (map_length(tmp->ctx->modules) == 0) {
destroy_ctx(tmp->ctx);
if (map_length((*self)->ctx->modules) == 0) {
destroy_ctx((*self)->ctx);
}
map_free(mod->subscriptions);
stack_free(mod->recvs);

/* Destroy external handler */
memhook._free(tmp);
memhook._free(*self);
*self = NULL;

/*
Expand Down Expand Up @@ -475,7 +467,9 @@ module_ret_code module_dump(const self_t *self) {
module_log(self, "* State: %u\n", mod->state);
module_log(self, "* Userdata: %p\n", mod->userdata);
module_log(self, "* Subscriptions:\n");
map_iterate(mod->subscriptions, subscribtions_dump, (void *)self);
for (map_itr_t *itr = map_itr_new(mod->subscriptions); itr; itr = map_itr_next(itr)) {
module_log(self, "-> %s: %p\n", map_itr_get_key(itr), map_itr_get_data(itr));
}
module_log(self, "* Fds:\n");
module_poll_t *tmp = mod->fds;
while (tmp) {
Expand Down
12 changes: 6 additions & 6 deletions Lib/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
* Skip reference check for pure functions.
*/
#define _GET_CTX(self, pure) \
MOD_ASSERT(self, "NULL self handler.", MOD_NO_SELF); \
MOD_ASSERT(!self->is_ref || pure, "Self is a reference object. It does not own module.", MOD_REF_ERR); \
GET_CTX_PRIV(self); \
MOD_ASSERT((self), "NULL self handler.", MOD_NO_SELF); \
MOD_ASSERT(!(self)->is_ref || pure, "Self is a reference object. It does not own module.", MOD_REF_ERR); \
GET_CTX_PRIV((self)); \
MOD_ASSERT(c, "Context not found.", MOD_NO_CTX);

/* Convenience macros for exposed API */
Expand All @@ -49,9 +49,9 @@
* Skip reference check for pure functions.
*/
#define _GET_MOD(self, pure) \
MOD_ASSERT(self, "NULL self handler.", MOD_NO_SELF); \
MOD_ASSERT(!self->is_ref || pure, "Self is a reference object. It does not own module.", MOD_REF_ERR); \
GET_MOD_PRIV(self); \
MOD_ASSERT((self), "NULL self handler.", MOD_NO_SELF); \
MOD_ASSERT(!(self)->is_ref || pure, "Self is a reference object. It does not own module.", MOD_REF_ERR); \
GET_MOD_PRIV((self)); \
MOD_ASSERT(mod, "Module not found.", MOD_NO_MOD);

/* Convenience macros for exposed API */
Expand Down

0 comments on commit de43f79

Please sign in to comment.