From 4a06b9109bcb65c2ffac87f06a3b30f3e12c2dd0 Mon Sep 17 00:00:00 2001 From: Kees Bakker Date: Wed, 5 Jun 2019 23:40:09 +0200 Subject: [PATCH] usbus: simplify adding entry to list --- sys/usb/usbus/usbus.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/sys/usb/usbus/usbus.c b/sys/usb/usbus/usbus.c index 321d526589..a39b9d2ca3 100644 --- a/sys/usb/usbus/usbus.c +++ b/sys/usb/usbus/usbus.c @@ -107,37 +107,24 @@ uint16_t usbus_add_interface(usbus_t *usbus, usbus_interface_t *iface) * usages. Furthermore, the O(1) append is not really necessary as this is * only used at init */ uint16_t idx = 0; - usbus_interface_t *last = usbus->iface; - - if (last) { + usbus_interface_t **last = &usbus->iface; + while (*last) { + last = &(*last)->next; idx++; - while (last->next) { - last = last->next; - idx++; - } - last->next = iface; - } - else { - usbus->iface = iface; } iface->idx = idx; + *last = iface; return idx; } void usbus_register_event_handler(usbus_t *usbus, usbus_handler_t *handler) { /* See note above for reasons against clist.h */ - usbus_handler_t *last = usbus->handlers; - - if (last) { - while (last->next) { - last = last->next; - } - last->next = handler; - } - else { - usbus->handlers = handler; + usbus_handler_t **last = &usbus->handlers; + while (*last) { + last = &(*last)->next; } + *last = handler; } usbus_endpoint_t *usbus_add_endpoint(usbus_t *usbus, usbus_interface_t *iface,