From e5bc49a8e6f14db63f0cb7818185680f11afb31f Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 30 Oct 2016 10:58:29 +0100 Subject: [PATCH] Refactor: move private watcher members to a union This will save a few bytes of stack/heap for users. Signed-off-by: Joachim Nilsson --- private.h | 17 ++++++++++++----- signal.c | 4 ++-- timer.c | 6 +++--- uev.c | 8 ++++---- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/private.h b/private.h index 91e12a2..e4d7b8c 100644 --- a/private.h +++ b/private.h @@ -61,12 +61,19 @@ struct uev; void (*cb)(struct uev *, void *, int); \ void *arg; \ \ - /* Timer watchers, time in milliseconds */ \ - int timeout; \ - int period; \ + /* Arguments for different watchers */ \ + union { \ + /* Timer watchers, time in milliseconds */ \ + struct { \ + int timeout; \ + int period; \ + } t; \ \ - /* Signal watchers */ \ - int signo; \ + /* Signal watchers */ \ + struct { \ + int signo; \ + } s; \ + } u; \ \ /* Watcher type */ \ uev_type_t diff --git a/signal.c b/signal.c index 6e32797..8612d92 100644 --- a/signal.c +++ b/signal.c @@ -87,7 +87,7 @@ int uev_signal_set(uev_t *w, int signo) } /* Remember for callbacks and start/stop */ - w->signo = signo; + w->u.s.signo = signo; /* Handle stopped signal watchers */ if (w->fd < 0) { @@ -129,7 +129,7 @@ int uev_signal_start(uev_t *w) if (-1 != w->fd) uev_signal_stop(w); - return uev_signal_set(w, w->signo); + return uev_signal_set(w, w->u.s.signo); } /** diff --git a/timer.c b/timer.c index 2a06466..012f3d4 100644 --- a/timer.c +++ b/timer.c @@ -123,8 +123,8 @@ int uev_timer_set(uev_t *w, int timeout, int period) return -1; } - w->timeout = timeout; - w->period = period; + w->u.t.timeout = timeout; + w->u.t.period = period; if (w->ctx->running) { struct itimerspec time; @@ -153,7 +153,7 @@ int uev_timer_start(uev_t *w) if (-1 != w->fd) uev_watcher_stop(w); - return uev_timer_set(w, w->timeout, w->period); + return uev_timer_set(w, w->u.t.timeout, w->u.t.period); } /** diff --git a/uev.c b/uev.c index cff59f7..ba91bb1 100644 --- a/uev.c +++ b/uev.c @@ -220,7 +220,7 @@ int uev_run(uev_ctx_t *ctx, int flags) /* Start all dormant timers */ LIST_FOREACH(w, &ctx->watchers, link) { if (UEV_TIMER_TYPE == w->type) - uev_timer_set(w, w->timeout, w->period); + uev_timer_set(w, w->u.t.timeout, w->u.t.period); } while (ctx->running) { @@ -282,8 +282,8 @@ int uev_run(uev_ctx_t *ctx, int flags) return -3; } - if (!w->period) - w->timeout = 0; + if (!w->u.t.period) + w->u.t.timeout = 0; } if (UEV_SIGNAL_TYPE == w->type) { @@ -300,7 +300,7 @@ int uev_run(uev_ctx_t *ctx, int flags) w->cb(w, w->arg, events[i].events & UEV_EVENT_MASK); if (UEV_TIMER_TYPE == w->type) { - if (!w->timeout) + if (!w->u.t.timeout) uev_timer_stop(w); } }