Skip to content

Commit

Permalink
Each module self_t is now a static allocated variable, to avoid user …
Browse files Browse the repository at this point in the history
…freeing it.
  • Loading branch information
FedeDP committed Jan 6, 2019
1 parent bced612 commit 3b64d0f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
28 changes: 8 additions & 20 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ static module_ret_code _register_fd(module *mod, const int fd, const bool autocl
tmp->autoclose = autoclose;
tmp->userptr = userptr;
tmp->prev = mod->fds;
tmp->self = mod->self;
tmp->self = &mod->self;
mod->fds = tmp;
mod->self->ctx->num_fds++;
mod->self.ctx->num_fds++;
/* If a fd is registered at runtime, start polling on it */
int ret = 0;
if (_module_is(mod, RUNNING)) {
ret = poll_set_new_evt(tmp, mod->self->ctx, ADD);
ret = poll_set_new_evt(tmp, mod->self.ctx, ADD);
}
return !ret ? MOD_OK : MOD_ERR;
}
Expand All @@ -127,14 +127,14 @@ static module_ret_code _deregister_fd(module *mod, const int fd) {
*tmp = (*tmp)->prev;
/* If a fd is deregistered for a RUNNING module, stop polling on it */
if (_module_is(mod, RUNNING)) {
ret = poll_set_new_evt(t, mod->self->ctx, RM);
ret = poll_set_new_evt(t, mod->self.ctx, RM);
}
if (t->autoclose) {
close(t->fd);
}
memhook._free(t->ev);
memhook._free(t);
mod->self->ctx->num_fds--;
mod->self.ctx->num_fds--;
return !ret ? MOD_OK : MOD_ERR;
}
tmp = &(*tmp)->prev;
Expand All @@ -159,7 +159,7 @@ static int manage_fds(module *mod, m_context *c, const int flag, const bool stop
}

static module_ret_code start(module *mod, const char *err_str) {
GET_CTX_PURE(mod->self);
GET_CTX_PURE((&mod->self));

/*
* Starting module for the first time
Expand All @@ -181,7 +181,7 @@ static module_ret_code start(module *mod, const char *err_str) {
}

static module_ret_code stop(module *mod, const char *err_str, const bool stop) {
GET_CTX_PURE(mod->self);
GET_CTX_PURE((&mod->self));

int ret = manage_fds(mod, c, RM, stop);
MOD_ASSERT(!ret, err_str, MOD_ERR);
Expand Down Expand Up @@ -269,14 +269,7 @@ module_ret_code module_register(const char *name, const char *ctx_name, const se
*((bool *)&s->is_ref) = false;

/* Internal reference */
mod->self = memhook._malloc(sizeof(self_t));
if (!mod->self) {
break;
}

*((module **)&mod->self->mod) = mod;
*((m_context **)&mod->self->ctx) = context;
*((bool *)&mod->self->is_ref) = true;
memcpy((self_t *)&mod->self, &((self_t){ mod, context, true }), sizeof(self_t));

if (map_put(context->modules, mod->name, mod, false, false) == MAP_OK) {
evaluate_module(NULL, mod);
Expand All @@ -287,7 +280,6 @@ module_ret_code module_register(const char *name, const char *ctx_name, const se
}
memhook._free((char *)mod->name);
memhook._free((self_t *)*self);
memhook._free((self_t *)mod->self);
map_free(mod->subscriptions);
stack_free(mod->recvs);
memhook._free(mod);
Expand Down Expand Up @@ -318,10 +310,6 @@ module_ret_code module_deregister(const self_t **self) {
/* Destroy external handler */
memhook._free((self_t *)*self);
*self = NULL;

/* Destroy module reference */
memhook._free((self_t *)mod->self);
mod->self = NULL;

/*
* Call destroy once self is NULL
Expand Down
38 changes: 19 additions & 19 deletions Lib/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
#include "stack.h"

#ifndef NDEBUG
#define MOD_ASSERT(cond, msg, ret) if (!(cond)) { fprintf(stderr, "%s\n", msg); return ret; }
#define MODULE_DEBUG printf("Libmodule: "); printf
#else
#define MOD_ASSERT(cond, msg, ret) if (!(cond)) { return ret; }
#define MODULE_DEBUG (void)
#endif

#define MOD_ASSERT(cond, msg, ret) if (!(cond)) { MODULE_DEBUG("%s\n", msg); return ret; }

#define MOD_ALLOC_ASSERT(cond) MOD_ASSERT(cond, "Failed to malloc.", MOD_NO_MEM);
#define MOD_PARAM_ASSERT(cond) MOD_ASSERT(cond, #cond, MOD_WRONG_PARAM);

#ifndef NDEBUG
#define MODULE_DEBUG printf("Libmodule: "); printf
#else
#define MODULE_DEBUG (void)
#endif

/* Finds a ctx inside our global map, given its name */
#define FIND_CTX(name) \
MOD_ASSERT(name, "NULL ctx.", MOD_ERR); \
Expand Down Expand Up @@ -62,6 +58,16 @@
GET_MOD(self); \
MOD_ASSERT(_module_is(mod, state), "Wrong module state.", MOD_WRONG_STATE);

typedef struct _module module;
typedef struct _context m_context;
/* Struct that holds self module informations, static to each module */
struct _self {
module *const mod; // self's mod
m_context *const ctx; // self's ctx
const bool is_ref; // is this a reference?
};

/* List that matches fds with selfs */
typedef struct _poll_t {
int fd;
bool autoclose;
Expand All @@ -72,7 +78,7 @@ typedef struct _poll_t {
} module_poll_t;

/* Struct that holds data for each module */
typedef struct {
struct _module {
userhook hook; // module's user defined callbacks
stack_t *recvs; // Stack of recv functions for module_become/unbecome
const void *userdata; // module's user defined data
Expand All @@ -81,10 +87,11 @@ typedef struct {
module_poll_t *fds; // module's fds to be polled
map_t *subscriptions; // module's subscriptions
int pubsub_fd[2]; // In and Out pipe for pubsub msg
const struct _self *self; // pointer to self (and thus context)
} module;
const self_t self; // pointer to self (and thus context)
};

typedef struct {
/* Struct that holds data for each context */
struct _context {
const char *name;
bool quit;
uint8_t quit_code;
Expand All @@ -96,13 +103,6 @@ typedef struct {
void *pevents;
int max_events;
map_t *topics;
} m_context;

/* Struct that holds self module informations, static to each module */
struct _self {
module *const mod; // self's mod
m_context *const ctx; // self's ctx
const bool is_ref; // is this a reference?
};

/* Defined in module.c */
Expand Down
8 changes: 4 additions & 4 deletions Lib/pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ static module_ret_code publish_msg(const module *mod, const char *topic,
MOD_PARAM_ASSERT(message);
MOD_PARAM_ASSERT(size > 0);

GET_CTX_PURE(mod->self);
GET_CTX_PURE((&mod->self));

void *tmp = NULL;
/*
* Only module that registered a topic can publish on the topic.
* Moreover, a publish can only be made on existent topic.
*/
if (!topic || ((tmp = map_get(c->topics, topic)) && tmp == mod)) {
pubsub_msg_t m = { .topic = topic, .message = message, .sender = mod->self, .type = USER, .size = size };
pubsub_msg_t m = { .topic = topic, .message = message, .sender = &mod->self, .type = USER, .size = size };
return tell_pubsub_msg(&m, NULL, c);
}
return MOD_ERR;
Expand Down Expand Up @@ -108,7 +108,7 @@ module_ret_code module_ref(const self_t *self, const char *name, const self_t **
GET_CTX(self);
CTX_GET_MOD(name, c);

*modref = mod->self;
*modref = &mod->self;
return MOD_OK;
}

Expand Down Expand Up @@ -181,7 +181,7 @@ module_ret_code module_tell(const self_t *self, const self_t *recipient, const u
/* only same ctx modules can talk */
MOD_PARAM_ASSERT(self->ctx == recipient->ctx);

pubsub_msg_t m = { .topic = NULL, .message = message, .sender = mod->self, .type = USER, .size = size };
pubsub_msg_t m = { .topic = NULL, .message = message, .sender = &mod->self, .type = USER, .size = size };
return tell_pubsub_msg(&m, recipient->mod, recipient->ctx);
}

Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module_ref should then return a new self_t* object, add it to a stack in module.
- [x] Update libmodule API doc
- [x] State in doc when you can use a module ref for normal functions (eg: module_is)

- [ ] Each module's self should be a static (non-pointer) variable (to avoid user freeing it)?
- [x] Each module's self should be a static (non-pointer) variable (to avoid user freeing it)?

### Generic
- [x] Use attribute pure where needed
Expand Down

0 comments on commit 3b64d0f

Please sign in to comment.