From 3ede2a57df6a1c2324b4b611bd5f2fd5a3c6567e Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 26 Mar 2012 04:39:46 +0000 Subject: handle failing realloc() correctly (thanks to Mattia) git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@1184 3ae390bd-cb1e-0410-b409-cd5a39f66f1f --- timer.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'timer.c') diff --git a/timer.c b/timer.c index 52b5dac..9be12b8 100644 --- a/timer.c +++ b/timer.c @@ -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 @@ -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 */ -- cgit v1.2.3