summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-12-08 05:10:01 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2022-12-08 05:19:40 -0500
commitf6f12db062697035a31309794ed1980040e09a53 (patch)
treeb7c1853448b3c8b9f377c99324aa92d6a2c04dd6
parent66a411a1ba6d3668b002760ce2d26bca42db894f (diff)
downloadrockbox-f6f12db062.tar.gz
rockbox-f6f12db062.zip
events.c do_add_event() only traverse event array once
we already go through the whole array might as well save the free slot as well Change-Id: I6e1164b57a5510f5de5555d2cb91379afc58ae08
-rw-r--r--firmware/events.c36
1 files changed, 18 insertions, 18 deletions
diff --git a/firmware/events.c b/firmware/events.c
index 8015442cc2..8124f92b20 100644
--- a/firmware/events.c
+++ b/firmware/events.c
@@ -43,30 +43,30 @@ static struct sysevent events[MAX_SYS_EVENTS];
static bool do_add_event(unsigned short id, bool oneshot, bool user_data_valid,
void *handler, void *user_data)
{
- int i;
-
- /* Check if the event already exists. */
- for (i = 0; i < MAX_SYS_EVENTS; i++)
+ int free = -1;
+ /* Check if the event already exists. & lowest free slot available */
+ for (int i = MAX_SYS_EVENTS - 1; i >= 0; i--)
{
if (events[i].handler.callback == handler && events[i].id == id
- && (!user_data_valid || (user_data == events[i].handler.user_data)))
+ && (!user_data_valid || (user_data == events[i].handler.user_data)))
+ {
return false;
+ }
+ else if (events[i].handler.callback == NULL)
+ free = i;
}
-
- /* Try to find a free slot. */
- for (i = 0; i < MAX_SYS_EVENTS; i++)
+
+ /* is there a free slot? */
+ if (free >= 0)
{
- if (events[i].handler.callback == NULL)
- {
- events[i].id = id;
- events[i].oneshot = oneshot;
- if ((events[i].has_user_data = user_data_valid))
- events[i].handler.user_data = user_data;
- events[i].handler.callback = handler;
- return true;
- }
+ events[free].id = id;
+ events[free].oneshot = oneshot;
+ if ((events[free].has_user_data = user_data_valid))
+ events[free].handler.user_data = user_data;
+ events[free].handler.callback = handler;
+ return true;
}
-
+
panicf("event line full");
return false;
}