aboutsummaryrefslogtreecommitdiffstats
path: root/timer.c
diff options
context:
space:
mode:
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2012-03-26 04:39:46 +0000
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2012-03-26 04:39:46 +0000
commit3ede2a57df6a1c2324b4b611bd5f2fd5a3c6567e (patch)
tree90b600b83662a57718c3239656b08f49e8a6f5f3 /timer.c
parent7ae0d560e1d1529d958ca75673176c47f0dcf3b9 (diff)
downloadlcd4linux-3ede2a57df6a1c2324b4b611bd5f2fd5a3c6567e.tar.gz
handle failing realloc() correctly (thanks to Mattia)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@1184 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'timer.c')
-rw-r--r--timer.c25
1 files changed, 7 insertions, 18 deletions
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 */