ldb: sync DLIST_DEMOTE_SHORT() changes to include/dlinklist.h
authorStefan Metzmacher <metze@samba.org>
Mon, 30 Jan 2023 15:10:07 +0000 (16:10 +0100)
committerRalph Boehme <slow@samba.org>
Fri, 13 Oct 2023 09:49:33 +0000 (09:49 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/ldb/include/dlinklist.h

index a775e8dcdc1858565a7a3083abfcc753f9581622..49a135a23bd91cb68e1c98882449c34ed4e98497 100644 (file)
@@ -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.