From 7672a29febe9151b4435fae9d6b21a82205d911f Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 30 Jan 2023 16:10:07 +0100 Subject: [PATCH] ldb: sync DLIST_DEMOTE_SHORT() changes to include/dlinklist.h Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme --- lib/ldb/include/dlinklist.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/ldb/include/dlinklist.h b/lib/ldb/include/dlinklist.h index a775e8dcdc18..49a135a23bd9 100644 --- a/lib/ldb/include/dlinklist.h +++ b/lib/ldb/include/dlinklist.h @@ -156,6 +156,27 @@ do { \ DLIST_ADD_END(list, p); \ } while (0) +/* + * like DLIST_DEMOTE(), but optimized + * for short lists with 0, 1 or 2 elements + */ +#define DLIST_DEMOTE_SHORT(list, p) \ +do { \ + if ((list) == NULL) { \ + /* no reason to demote, just add */ \ + DLIST_ADD(list, p); \ + } else if ((list)->prev == (p)) { \ + /* optimize if p is last */ \ + } else if ((list) == (p)) { \ + /* optimize if p is first */ \ + (list)->prev->next = (p); \ + (list) = (p)->next; \ + (p)->next = NULL; \ + } else { \ + DLIST_DEMOTE(list, p); \ + } \ +} while (0) + /* concatenate two lists - putting all elements of the 2nd list at the end of the first list.