diff options
Diffstat (limited to '')
-rw-r--r-- | timer_group.c | 96 |
1 files changed, 33 insertions, 63 deletions
diff --git a/timer_group.c b/timer_group.c index f6d7a5a..c50343f 100644 --- a/timer_group.c +++ b/timer_group.c @@ -1,4 +1,4 @@ -/* $Id: timer_group.c 1136 2010-11-28 16:07:16Z mzuther $ +/* $Id: timer_group.c 1185 2012-03-26 13:24:37Z mjona $ * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/timer_group.c $ * * Generic grouping of widgets that have been set to the same update @@ -75,10 +75,10 @@ /* structure for storing all relevant data of a single timer group */ typedef struct TIMER_GROUP { - /* pointer to the group's triggering interval in milliseconds; + /* group's triggering interval in milliseconds; this will be used to identify a specific timer group and also as callback data for the underlying generic timer */ - int *interval; + int interval; /* marks timer group as being active (so it will get processed) or inactive (which means the timer group has been deleted and its @@ -141,10 +141,10 @@ int timer_group_exists(const int interval) matches the specified interval */ for (group = 0; group < nTimerGroups; group++) { /* skip inactive (i.e. deleted) timer groups */ - if (TimerGroups[group].active == 0) + if (TimerGroups[group].active == TIMER_INACTIVE) continue; - if (*TimerGroups[group].interval == interval) { + if (TimerGroups[group].interval == interval) { /* matching timer group found, so signal success by returning a value of 1 */ return 1; @@ -182,7 +182,7 @@ int timer_add_group(const int interval) /* try to minimize memory usage by looping through timer group slots and looking for an inactive timer group */ for (group = 0; group < nTimerGroups; group++) { - if (TimerGroups[group].active == 0) { + if (TimerGroups[group].active == TIMER_INACTIVE) { /* we've just found one, so let's reuse it ("group" holds its ID) by breaking the loop */ break; @@ -191,45 +191,27 @@ 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; } - - /* 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) { - /* signal unsuccessful timer group creation */ - return -1; - } + TimerGroups = tmp; + nTimerGroups++; } /* initialize timer group's interval */ - *TimerGroups[group].interval = interval; + TimerGroups[group].interval = interval; /* set timer group to active so that it is processed and not overwritten by the memory optimization routine above */ - TimerGroups[group].active = 1; + TimerGroups[group].active = TIMER_ACTIVE; /* finally, request a generic timer that calls this group and signal success or failure */ - return timer_add(timer_process_group, TimerGroups[group].interval, interval, 0); + return timer_add(timer_process_group, &TimerGroups[group].interval, interval, 0); } @@ -255,14 +237,14 @@ int timer_remove_group(const int interval) with the specified update interval */ for (widget = 0; widget < nTimerGroupWidgets; widget++) { /* skip inactive (i.e. deleted) widget slots */ - if (TimerGroupWidgets[widget].active == 0) + if (TimerGroupWidgets[widget].active == TIMER_INACTIVE) continue; if (TimerGroupWidgets[widget].interval == interval) { /* we have found a matching widget slot, so mark it as being inactive; we will not actually delete the slot, so its allocated memory may be re-used */ - TimerGroupWidgets[widget].active = 0; + TimerGroupWidgets[widget].active = TIMER_INACTIVE; } } @@ -270,17 +252,17 @@ int timer_remove_group(const int interval) timer group slot by looking for its settings */ for (group = 0; group < nTimerGroups; group++) { /* skip inactive (i.e. deleted) timer groups */ - if (TimerGroups[group].active == 0) + if (TimerGroups[group].active == TIMER_INACTIVE) continue; - if (*TimerGroups[group].interval == interval) { + if (TimerGroups[group].interval == interval) { /* we have found the timer group slot, so mark it as being inactive; we will not actually delete the slot, so its allocated memory may be re-used */ - TimerGroups[group].active = 0; + TimerGroups[group].active = TIMER_INACTIVE; /* remove the generic timer that calls this group */ - if (timer_remove(timer_process_group, TimerGroups[group].interval)) { + if (timer_remove(timer_process_group, &TimerGroups[group].interval)) { /* signal successful removal of timer group */ return 0; } else { @@ -313,7 +295,7 @@ int timer_remove_empty_group(const int interval) specified update interval */ for (widget = 0; widget < nTimerGroupWidgets; widget++) { /* skip inactive (i.e. deleted) widget slots */ - if (TimerGroupWidgets[widget].active == 0) + if (TimerGroupWidgets[widget].active == TIMER_INACTIVE) continue; /* at least one other widget with specified update interval @@ -366,7 +348,7 @@ void timer_process_group(void *data) group's update interval */ for (widget = 0; widget < nTimerGroupWidgets; widget++) { /* skip inactive (i.e. deleted) widgets */ - if (TimerGroupWidgets[widget].active == 0) + if (TimerGroupWidgets[widget].active == TIMER_INACTIVE) continue; /* the current widget belongs to the specified timer group */ @@ -379,7 +361,7 @@ void timer_process_group(void *data) /* mark one-shot widget as inactive (which means the it has been deleted and its allocated memory may be re-used) */ if (TimerGroupWidgets[widget].one_shot) { - TimerGroupWidgets[widget].active = 0; + TimerGroupWidgets[widget].active = TIMER_INACTIVE; /* also remove the corresponding timer group if it is empty */ timer_remove_empty_group(interval); @@ -425,7 +407,7 @@ int timer_add_widget(void (*callback) (void *data), void *data, const int interv /* try to minimize memory usage by looping through the widget slots and looking for an inactive widget slot */ for (widget = 0; widget < nTimerGroupWidgets; widget++) { - if (TimerGroupWidgets[widget].active == 0) { + if (TimerGroupWidgets[widget].active == TIMER_INACTIVE) { /* we've just found one, so let's reuse it ("widget" holds its ID) by breaking the loop */ break; @@ -434,23 +416,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 */ @@ -461,7 +435,7 @@ int timer_add_widget(void (*callback) (void *data), void *data, const int interv /* set widget slot to active so that it is processed and not overwritten by the memory optimization routine above */ - TimerGroupWidgets[widget].active = 1; + TimerGroupWidgets[widget].active = TIMER_ACTIVE; /* signal successful addition of widget slot */ return 0; @@ -490,14 +464,14 @@ int timer_remove_widget(void (*callback) (void *data), void *data) widget slot by looking for its settings */ for (widget = 0; widget < nTimerGroupWidgets; widget++) { /* skip inactive (i.e. deleted) widget slots */ - if (TimerGroupWidgets[widget].active == 0) + if (TimerGroupWidgets[widget].active == TIMER_INACTIVE) continue; if (TimerGroupWidgets[widget].callback == callback && TimerGroupWidgets[widget].data == data) { /* we have found the widget slot, so mark it as being inactive; we will not actually delete the slot, so its allocated memory may be re-used */ - TimerGroupWidgets[widget].active = 0; + TimerGroupWidgets[widget].active = TIMER_INACTIVE; /* store the widget's triggering interval for later use and break the loop */ @@ -530,11 +504,7 @@ void timer_exit_group(void) /* loop through all timer groups and remove them one by one */ for (group = 0; group < nTimerGroups; group++) { /* remove generic timer */ - timer_remove(timer_process_group, TimerGroups[group].interval); - - /* free memory allocated for callback data (i.e. the group's - triggering interval in milliseconds) */ - free(TimerGroups[group].interval); + timer_remove(timer_process_group, &TimerGroups[group].interval); } /* reset number of allocated timer groups */ |