Refactor: move private watcher members to a union

This will save a few bytes of stack/heap for users.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2016-10-30 10:58:29 +01:00
1 parent 1f6fa93314
commit e5bc49a8e6
4 files changed
+21 -14

No files matched your search

+12 -5
View File
@@ -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
+2 -2
View File
@@ -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);
}
/**
+3 -3
View File
@@ -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);
}
/**
+4 -4
View File
@@ -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);
}
}