Skip to content

Commit

Permalink
Some better namings.
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeDP committed Aug 3, 2019
1 parent a954672 commit b64f368
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 74 deletions.
60 changes: 30 additions & 30 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

/** Generic module interface **/

static module_ret_code init_ctx(const char *ctx_name, m_context **context);
static void destroy_ctx(m_context *context);
static m_context *check_ctx(const char *ctx_name);
static int _pipe(module *mod);
static module_ret_code init_pubsub_fd(module *mod);
static module_ret_code init_ctx(const char *ctx_name, ctx_t **context);
static void destroy_ctx(ctx_t *context);
static ctx_t *check_ctx(const char *ctx_name);
static int _pipe(mod_t *mod);
static module_ret_code init_pubsub_fd(mod_t *mod);
static void default_logger(const self_t *self, const char *fmt, va_list args, const void *userdata);
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 module_ret_code _register_fd(mod_t *mod, const int fd, const bool autoclose, const void *userptr);
static module_ret_code _deregister_fd(mod_t *mod, const int fd);
static int manage_fds(mod_t *mod, ctx_t *c, const int flag, const bool stop);

static module_ret_code init_ctx(const char *ctx_name, m_context **context) {
static module_ret_code init_ctx(const char *ctx_name, ctx_t **context) {
MODULE_DEBUG("Creating context '%s'.\n", ctx_name);

*context = memhook._calloc(1, sizeof(m_context));
*context = memhook._calloc(1, sizeof(ctx_t));
MOD_ALLOC_ASSERT(*context);

(*context)->fd = poll_create();
Expand All @@ -39,23 +39,23 @@ static module_ret_code init_ctx(const char *ctx_name, m_context **context) {
return MOD_ERR;
}

static void destroy_ctx(m_context *context) {
static void destroy_ctx(ctx_t *context) {
MODULE_DEBUG("Destroying context '%s'.\n", context->name);
map_free(context->modules);
poll_close(context->fd, &context->pevents, &context->max_events);
map_remove(ctx, context->name);
memhook._free(context);
}

static m_context *check_ctx(const char *ctx_name) {
m_context *context = map_get(ctx, ctx_name);
static ctx_t *check_ctx(const char *ctx_name) {
ctx_t *context = map_get(ctx, ctx_name);
if (!context) {
init_ctx(ctx_name, &context);
}
return context;
}

static int _pipe(module *mod) {
static int _pipe(mod_t *mod) {
int ret = pipe(mod->pubsub_fd);
if (ret == 0) {
for (int i = 0; i < 2; i++) {
Expand All @@ -70,7 +70,7 @@ static int _pipe(module *mod) {
return ret;
}

static module_ret_code init_pubsub_fd(module *mod) {
static module_ret_code init_pubsub_fd(mod_t *mod) {
if (_pipe(mod) == 0) {
if (_register_fd(mod, mod->pubsub_fd[0], true, NULL) == MOD_OK) {
return MOD_OK;
Expand All @@ -92,8 +92,8 @@ static void default_logger(const self_t *self, const char *fmt, va_list args, co
* Append this fd to our list of fds and
* if module is in RUNNING state, start listening on its events
*/
static module_ret_code _register_fd(module *mod, const int fd, const bool autoclose, const void *userptr) {
module_poll_t *tmp = memhook._malloc(sizeof(module_poll_t));
static module_ret_code _register_fd(mod_t *mod, const int fd, const bool autoclose, const void *userptr) {
fd_priv_t *tmp = memhook._malloc(sizeof(fd_priv_t));
MOD_ALLOC_ASSERT(tmp);

if (poll_set_data(&tmp->ev) == MOD_OK) {
Expand All @@ -115,13 +115,13 @@ static module_ret_code _register_fd(module *mod, const int fd, const bool autocl
}

/* Linearly search for fd */
static module_ret_code _deregister_fd(module *mod, const int fd) {
module_poll_t **tmp = &mod->fds;
static module_ret_code _deregister_fd(mod_t *mod, const int fd) {
fd_priv_t **tmp = &mod->fds;

int ret = 0;
while (*tmp) {
if ((*tmp)->fd == fd) {
module_poll_t *t = *tmp;
fd_priv_t *t = *tmp;
*tmp = (*tmp)->prev;
/* If a fd is deregistered for a RUNNING module, stop polling on it */
if (_module_is(mod, RUNNING)) {
Expand All @@ -139,12 +139,12 @@ static module_ret_code _deregister_fd(module *mod, const int fd) {
return MOD_ERR;
}

static int manage_fds(module *mod, m_context *c, const int flag, const bool stop) {
module_poll_t *tmp = mod->fds;
static int manage_fds(mod_t *mod, ctx_t *c, const int flag, const bool stop) {
fd_priv_t *tmp = mod->fds;
int ret = 0;

while (tmp && !ret) {
module_poll_t *t = tmp;
fd_priv_t *t = tmp;
tmp = tmp->prev;
if (flag == RM && stop) {
if (t->fd == mod->pubsub_fd[0]) {
Expand All @@ -166,19 +166,19 @@ static int manage_fds(module *mod, m_context *c, const int flag, const bool stop

/** Private API **/

bool _module_is(const module *mod, const enum module_states st) {
bool _module_is(const mod_t *mod, const enum module_states st) {
return mod->state & st;
}

map_ret_code evaluate_module(void *data, const char *key, void *value) {
module *mod = (module *)value;
mod_t *mod = (mod_t *)value;
if (_module_is(mod, IDLE) && mod->hook.evaluate()) {
start(mod, true);
}
return MAP_OK;
}

module_ret_code start(module *mod, const bool start) {
module_ret_code start(mod_t *mod, const bool start) {
static const char *errors[2] = { "Failed to resume module.", "Failed to start module." };

GET_CTX_PRIV((&mod->self));
Expand Down Expand Up @@ -214,7 +214,7 @@ module_ret_code start(module *mod, const bool start) {
return MOD_OK;
}

module_ret_code stop(module *mod, const bool stop) {
module_ret_code stop(mod_t *mod, const bool stop) {
static const char *errors[2] = { "Failed to pause module.", "Failed to stop module." };

GET_CTX_PRIV((&mod->self));
Expand Down Expand Up @@ -249,15 +249,15 @@ module_ret_code module_register(const char *name, const char *ctx_name, self_t *
MOD_PARAM_ASSERT(!*self);
MOD_PARAM_ASSERT(hook);

m_context *context = check_ctx(ctx_name);
ctx_t *context = check_ctx(ctx_name);
MOD_ASSERT(context, "Failed to create context.", MOD_ERR);

const bool present = map_has_key(context->modules, name);
MOD_ASSERT(!present, "Module with same name already registered in context.", MOD_ERR);

MODULE_DEBUG("Registering module '%s'.\n", name);

module *mod = memhook._calloc(1, sizeof(module));
mod_t *mod = memhook._calloc(1, sizeof(mod_t));
MOD_ALLOC_ASSERT(mod);

module_ret_code ret = MOD_NO_MEM;
Expand Down Expand Up @@ -458,7 +458,7 @@ module_ret_code module_dump(const self_t *self) {
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;
fd_priv_t *tmp = mod->fds;
while (tmp) {
if (tmp->fd != mod->pubsub_fd[0]) {
module_log(self, "-> Fd: %d Ac: %d Up: %p\n", tmp->fd, tmp->autoclose, tmp->userptr);
Expand Down
24 changes: 12 additions & 12 deletions Lib/modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ static void *thread_loop(void *param);
static map_ret_code main_loop(void *data, const char *key, void *value);
static _ctor1_ void modules_init(void);
static _dtor0_ void modules_destroy(void);
static void evaluate_new_state(m_context *c);
static module_ret_code loop_start(m_context *c, const int max_events);
static uint8_t loop_stop(m_context *c);
static inline module_ret_code loop_quit(m_context *c, const uint8_t quit_code);
static int recv_events(m_context *c, int timeout);
static void evaluate_new_state(ctx_t *c);
static module_ret_code loop_start(ctx_t *c, const int max_events);
static uint8_t loop_stop(ctx_t *c);
static inline module_ret_code loop_quit(ctx_t *c, const uint8_t quit_code);
static int recv_events(ctx_t *c, int timeout);

map_t *ctx;
memhook_t memhook;
Expand Down Expand Up @@ -93,11 +93,11 @@ static void modules_destroy(void) {
ctx = NULL;
}

static void evaluate_new_state(m_context *c) {
static void evaluate_new_state(ctx_t *c) {
map_iterate(c->modules, evaluate_module, NULL);
}

static module_ret_code loop_start(m_context *c, const int max_events) {
static module_ret_code loop_start(ctx_t *c, const int max_events) {
if (poll_init_pevents(&c->pevents, max_events) == MOD_OK) {
c->looping = true;
c->quit = false;
Expand All @@ -114,7 +114,7 @@ static module_ret_code loop_start(m_context *c, const int max_events) {
return MOD_ERR;
}

static uint8_t loop_stop(m_context *c) {
static uint8_t loop_stop(ctx_t *c) {
/* Tell every module that loop is stopped */
tell_system_pubsub_msg(NULL, c, LOOP_STOPPED, NULL, NULL);

Expand All @@ -126,18 +126,18 @@ static uint8_t loop_stop(m_context *c) {
return c->quit_code;
}

static inline module_ret_code loop_quit(m_context *c, const uint8_t quit_code) {
static inline module_ret_code loop_quit(ctx_t *c, const uint8_t quit_code) {
c->quit = true;
c->quit_code = quit_code;
return MOD_OK;
}

static int recv_events(m_context *c, int timeout) {
static int recv_events(ctx_t *c, int timeout) {
int nfds = poll_wait(c->fd, c->max_events, c->pevents, timeout);
for (int i = 0; i < nfds; i++) {
module_poll_t *p = poll_recv(i, c->pevents);
fd_priv_t *p = poll_recv(i, c->pevents);
if (p && p->self && p->self->mod) {
module *mod = p->self->mod;
mod_t *mod = p->self->mod;

msg_t msg;
fd_msg_t fd_msg;
Expand Down
6 changes: 3 additions & 3 deletions Lib/poll_plugins/epoll_priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int poll_set_data(void **_ev) {
return MOD_OK;
}

int poll_set_new_evt(module_poll_t *tmp, m_context *c, const enum op_type flag) {
int poll_set_new_evt(fd_priv_t *tmp, ctx_t *c, const enum op_type flag) {
int f = flag == ADD ? EPOLL_CTL_ADD : EPOLL_CTL_DEL;
struct epoll_event *ev = (struct epoll_event *)tmp->ev;
ev->data.ptr = tmp;
Expand All @@ -34,9 +34,9 @@ int poll_wait(const int fd, const int max_events, void *pevents, const int timeo
return epoll_wait(fd, (struct epoll_event *) pevents, max_events, timeout);
}

module_poll_t *poll_recv(const int idx, void *pevents) {
fd_priv_t *poll_recv(const int idx, void *pevents) {
struct epoll_event *pev = (struct epoll_event *) pevents;
return (module_poll_t *)pev[idx].data.ptr;
return (fd_priv_t *)pev[idx].data.ptr;
}

int poll_destroy_pevents(void **pevents, int *max_events) {
Expand Down
6 changes: 3 additions & 3 deletions Lib/poll_plugins/kqueue_priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int poll_set_data(void **_ev) {
return MOD_OK;
}

int poll_set_new_evt(module_poll_t *tmp, m_context *c, const enum op_type flag) {
int poll_set_new_evt(fd_priv_t *tmp, ctx_t *c, const enum op_type flag) {
int f = flag == ADD ? EV_ADD : EV_DELETE;
struct kevent *_ev = (struct kevent *)tmp->ev;
EV_SET(_ev, tmp->fd, EVFILT_READ, f, 0, 0, (void *)tmp);
Expand All @@ -39,12 +39,12 @@ int poll_wait(const int fd, const int max_events, void *pevents, const int timeo
return kevent(fd, NULL, 0, (struct kevent *)pevents, max_events, timeout >= 0 ? &t : NULL);
}

module_poll_t *poll_recv(const int idx, void *pevents) {
fd_priv_t *poll_recv(const int idx, void *pevents) {
struct kevent *pev = (struct kevent *)pevents;
if (pev[idx].flags & EV_ERROR) {
return NULL;
}
return (module_poll_t *)pev[idx].udata;
return (fd_priv_t *)pev[idx].udata;
}

int poll_destroy_pevents(void **pevents, int *max_events) {
Expand Down
4 changes: 2 additions & 2 deletions Lib/poll_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ enum op_type { ADD, RM };

int poll_create(void);
int poll_set_data(void **_ev);
int poll_set_new_evt(module_poll_t *tmp, m_context *c, const enum op_type flag);
int poll_set_new_evt(fd_priv_t *tmp, ctx_t *c, const enum op_type flag);
int poll_init_pevents(void **pevents, const int max_events);
int poll_wait(const int fd, const int max_events, void *pevents, const int timeout);
module_poll_t *poll_recv(const int idx, void *pevents);
fd_priv_t *poll_recv(const int idx, void *pevents);
int poll_destroy_pevents(void **pevents, int *max_events);
int poll_close(const int fd, void **pevents, int *max_events);
31 changes: 16 additions & 15 deletions Lib/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
/* Finds a ctx inside our global map, given its name */
#define FIND_CTX(name) \
MOD_ASSERT(name, "NULL ctx.", MOD_ERR); \
m_context *c = map_get(ctx, (char *)name); \
ctx_t *c = map_get(ctx, (char *)name); \
MOD_ASSERT(c, "Context not found.", MOD_NO_CTX);

#define GET_CTX_PRIV(self) m_context *c = self->ctx
#define GET_CTX_PRIV(self) ctx_t *c = self->ctx
/*
* Convenience macro to retrieve self->ctx + doing some checks.
* Skip reference check for pure functions.
Expand All @@ -40,10 +40,11 @@

/* Convenience macro to retrieve a module from a context, given its name */
#define CTX_GET_MOD(name, ctx) \
module *mod = map_get(ctx->modules, (char *)name); \
mod_t *mod = map_get(ctx->modules, (char *)name); \
MOD_ASSERT(mod, "Module not found.", MOD_NO_MOD);

#define GET_MOD_PRIV(self) module *mod = self->mod
#define GET_MOD_PRIV(self) mod_t *mod = self->mod

/*
* Convenience macro to retrieve self->mod + doing some checks.
* Skip reference check for pure functions.
Expand All @@ -66,13 +67,13 @@
GET_MOD(self); \
MOD_ASSERT(_module_is(mod, state), "Wrong module state.", MOD_WRONG_STATE);

typedef struct _module module;
typedef struct _context m_context;
typedef struct _module mod_t;
typedef struct _context ctx_t;

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

Expand All @@ -84,7 +85,7 @@ typedef struct _poll_t {
const void *userptr;
const struct _self *self; // ptr needed to map a fd to a self_t in epoll
struct _poll_t *prev;
} module_poll_t;
} fd_priv_t;

/* Struct that holds pubsub messaging, private. It keeps reference count. */
typedef struct {
Expand All @@ -100,7 +101,7 @@ struct _module {
const void *userdata; // module's user defined data
enum module_states state; // module's state
const char *name; // module's name
module_poll_t *fds; // module's fds to be polled
fd_priv_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
self_t self; // pointer to self (and thus context)
Expand All @@ -121,16 +122,16 @@ struct _context {
};

/* Defined in module.c */
_pure_ bool _module_is(const module *mod, const enum module_states st);
_pure_ bool _module_is(const mod_t *mod, const enum module_states st);
map_ret_code evaluate_module(void *data, const char *key, void *value);
module_ret_code start(module *mod, const bool start);
module_ret_code stop(module *mod, const bool stop);
module_ret_code start(mod_t *mod, const bool start);
module_ret_code stop(mod_t *mod, const bool stop);

/* Defined in pubsub.c */
module_ret_code tell_system_pubsub_msg(module *mod, m_context *c, enum msg_type type,
module_ret_code tell_system_pubsub_msg(mod_t *mod, ctx_t *c, enum msg_type type,
const self_t *sender, const char *topic);
map_ret_code flush_pubsub_msgs(void *data, const char *key, void *value);
void run_pubsub_cb(module *mod, msg_t *msg);
void run_pubsub_cb(mod_t *mod, msg_t *msg);

/* Defined in priv.c */
char *mem_strdup(const char *s);
Expand Down

0 comments on commit b64f368

Please sign in to comment.