]> git.webhop.me Git - lcd4linux.git/commitdiff
handle failing realloc() correctly (thanks to Mattia)
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 26 Mar 2012 04:39:46 +0000 (04:39 +0000)
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 26 Mar 2012 04:39:46 +0000 (04:39 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@1184 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

timer.c
timer_group.c

diff --git a/timer.c b/timer.c
index 52b5dac4632477aac16918cc6cf8a987f73542f9..9be12b8faae7c021f54264567232883fe51b777b 100644 (file)
--- 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 */
index 3e04e56fca2b722196e0c35945f108d0a8895e52..fb4066f63427fa09b7731b9d550b50414bf927fa 100644 (file)
@@ -191,30 +191,17 @@ int timer_add_group(const int interval)
 
     /* no inactive timer groups (or none at all) found, so we have to
        add a new timer group slot */
-    if (group >= nTimerGroups) {
-       /* increment number of timer groups and (re-)allocate memory used
-          for storing the timer group slots */
-       nTimerGroups++;
-       TimerGroups = realloc(TimerGroups, nTimerGroups * sizeof(*TimerGroups));
-
-       /* make sure "group" points to valid memory */
-       group = nTimerGroups - 1;
-
-       /* realloc() has failed */
-       if (TimerGroups == NULL) {
-           /* restore old number of timer groups */
-           nTimerGroups--;
-
+    if (group == nTimerGroups) {
+        TIMER_GROUP *tmp;
+       
+       if ((tmp = realloc(TimerGroups, (nTimerGroups + 1) * sizeof(*TimerGroups))) == NULL) {
            /* signal unsuccessful timer group creation */
            return -1;
        }
+       TimerGroups = tmp;
+       nTimerGroups++;
 
-       /* allocate memory for the underlying generic timer's callback
-          data (i.e. the group's triggering interval in milliseconds) */
-       TimerGroups[group].interval = malloc(sizeof(int));
-
-       /* malloc() has failed */
-       if (TimerGroups[group].interval == NULL) {
+       if ((TimerGroups[group].interval = malloc(sizeof(int))) == NULL) {
            /* signal unsuccessful timer group creation */
            return -1;
        }
@@ -434,23 +421,15 @@ int timer_add_widget(void (*callback) (void *data), void *data, const int interv
 
     /* no inactive widget slots (or none at all) found, so we have to
        add a new widget slot */
-    if (widget >= nTimerGroupWidgets) {
-       /* increment number of widget slots and (re-)allocate memory used
-          for storing the widget slots */
-       nTimerGroupWidgets++;
-       TimerGroupWidgets = realloc(TimerGroupWidgets, nTimerGroupWidgets * sizeof(*TimerGroupWidgets));
-
-       /* make sure "widget" points to valid memory */
-       widget = nTimerGroupWidgets - 1;
-
-       /* realloc() has failed */
-       if (TimerGroupWidgets == NULL) {
-           /* restore old number of widget slots */
-           nTimerGroupWidgets--;
-
+    if (widget == nTimerGroupWidgets) {
+        TIMER_GROUP_WIDGET *tmp;
+       
+       if ((tmp = realloc(TimerGroupWidgets, (nTimerGroupWidgets + 1) * sizeof(*TimerGroupWidgets))) == NULL) {
            /* signal unsuccessful creation of widget slot */
            return -1;
        }
+       TimerGroupWidgets = tmp;
+       nTimerGroupWidgets++;
     }
 
     /* initialize widget slot */