From b8987aac39d644c5cecadfd3ea14f3ebb80f13ee Mon Sep 17 00:00:00 2001 From: Jonathan McCrohan Date: Thu, 17 May 2012 01:12:28 +0100 Subject: Import upstream version 0.11.0~svn1188 --- timer.c | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) (limited to 'timer.c') diff --git a/timer.c b/timer.c index b5446c2..63b223e 100644 --- a/timer.c +++ b/timer.c @@ -1,4 +1,4 @@ -/* $Id: timer.c 1143 2011-02-12 22:46:19Z mzuther $ +/* $Id: timer.c 1185 2012-03-26 13:24:37Z mjona $ * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/timer.c $ * * Generic timer handling. @@ -126,11 +126,8 @@ static void timer_inc(const int timer, struct timeval *now) struct timeval diff; timersub(now, &Timers[timer].when, &diff); - /* convert this time difference to fractional seconds */ - float time_difference = diff.tv_sec + diff.tv_usec / 1000000.0f; - - /* convert time difference to fractional milliseconds */ - time_difference = time_difference * 1000.0f; + /* convert this time difference to fractional milliseconds */ + float time_difference = (diff.tv_sec * 1000.0f) + (diff.tv_usec / 1000.0f); /* calculate the number of timer intervals that have passed since the last timer the given timer has been processed -- value is @@ -184,14 +181,14 @@ int timer_remove(void (*callback) (void *data), void *data) timer slot by looking for its settings */ for (timer = 0; timer < nTimers; timer++) { /* skip inactive (i.e. deleted) timers */ - if (Timers[timer].active == 0) + if (Timers[timer].active == TIMER_INACTIVE) continue; if (Timers[timer].callback == callback && Timers[timer].data == data) { /* we have found the timer slot, so mark it as being inactive; we will not actually delete the slot, so its allocated memory may be re-used */ - Timers[timer].active = 0; + Timers[timer].active = TIMER_INACTIVE; /* signal successful timer removal */ return 0; @@ -232,7 +229,7 @@ int timer_add(void (*callback) (void *data), void *data, const int interval, con /* try to minimize memory usage by looping through the timer slots and looking for an inactive timer */ for (timer = 0; timer < nTimers; timer++) { - if (Timers[timer].active == 0) { + if (Timers[timer].active == TIMER_INACTIVE) { /* we've just found one, so let's reuse it ("timer" holds its ID) by breaking the loop */ break; @@ -241,23 +238,15 @@ int timer_add(void (*callback) (void *data), void *data, const int interval, con /* no inactive timers (or none at all) found, so we have to add a new timer slot */ - if (timer >= nTimers) { - /* increment number of timers and (re-)allocate memory used for - storing the timer slots */ - nTimers++; - Timers = realloc(Timers, nTimers * sizeof(*Timers)); - - /* make sure "timer" points to valid memory */ - timer = nTimers - 1; - - /* realloc() has failed */ - if (Timers == NULL) { - /* restore old number of timers */ - nTimers--; + if (timer == nTimers) { + TIMER *tmp; + if ((tmp = realloc(Timers, (nTimers + 1) * sizeof(*Timers))) == NULL) { /* signal unsuccessful timer creation */ return -1; } + Timers = tmp; + nTimers++; } /* get current time so the timer triggers immediately */ @@ -272,7 +261,7 @@ int timer_add(void (*callback) (void *data), void *data, const int interval, con /* set timer to active so that it is processed and not overwritten by the memory optimization routine above */ - Timers[timer].active = 1; + Timers[timer].active = TIMER_ACTIVE; /* one-shot timers should NOT fire immediately, so delay them by a single timer interval */ @@ -323,7 +312,7 @@ int timer_add_late(void (*callback) (void *data), void *data, const int interval by looking for its settings */ for (timer = 0; timer < nTimers; timer++) { /* skip inactive (i.e. deleted) timers */ - if (Timers[timer].active == 0) + if (Timers[timer].active == TIMER_INACTIVE) continue; if (Timers[timer].callback == callback && Timers[timer].data == data && Timers[timer].interval == interval) { @@ -372,7 +361,7 @@ int timer_process(struct timespec *delay) /* process all expired timers */ for (timer = 0; timer < nTimers; timer++) { /* skip inactive (i.e. deleted) timers */ - if (Timers[timer].active == 0) + if (Timers[timer].active == TIMER_INACTIVE) continue; /* check whether current timer needs to be processed, i.e. the @@ -391,7 +380,7 @@ int timer_process(struct timespec *delay) if (Timers[timer].one_shot) { /* mark one-shot timer as inactive (which means the timer has been deleted and its allocated memory may be re-used) */ - Timers[timer].active = 0; + Timers[timer].active = TIMER_INACTIVE; } else { /* otherwise, re-spawn timer by adding one triggering interval to its triggering time */ @@ -406,7 +395,7 @@ int timer_process(struct timespec *delay) timer */ for (timer = 0; timer < nTimers; timer++) { /* skip inactive (i.e. deleted) timers */ - if (Timers[timer].active == 0) + if (Timers[timer].active == TIMER_INACTIVE) continue; /* if this is the first timer that we check, mark it as the next @@ -473,7 +462,7 @@ int timer_process(struct timespec *delay) /* process all timers */ for (timer = 0; timer < nTimers; timer++) { /* skip inactive (i.e. deleted) timers */ - if (Timers[timer].active == 0) + if (Timers[timer].active == TIMER_INACTIVE) continue; /* correct timer's time stamp by clock skew */ -- cgit v1.2.3