[nasm:debug-macros] nasmlib.h: clear up namespace confusion in list_reverse()

nasm-bot for H. Peter Anvin (Intel) hpa at zytor.com
Thu Jul 9 17:51:07 PDT 2020


Commit-ID:  35ed91f6f8ca944b29fd4e0d57534f504979c00e
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=35ed91f6f8ca944b29fd4e0d57534f504979c00e
Author:     H. Peter Anvin (Intel) <hpa at zytor.com>
AuthorDate: Wed, 8 Jul 2020 10:11:38 -0700
Committer:  H. Peter Anvin (Intel) <hpa at zytor.com>
CommitDate: Wed, 8 Jul 2020 10:11:38 -0700

nasmlib.h: clear up namespace confusion in list_reverse()

list_reverse() used "next" as an argument, while also needing to
refer to the structure field "next". Furthermore, the two temp
variables can be made generic by making them void *, and as this is
not a loop construct this is doable by declaring them inside the macro
loop.

Signed-off-by: H. Peter Anvin (Intel) <hpa at zytor.com>


---
 include/nasmlib.h | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/include/nasmlib.h b/include/nasmlib.h
index e9bfbccf..89bf165e 100644
--- a/include/nasmlib.h
+++ b/include/nasmlib.h
@@ -269,27 +269,32 @@ const char *filename_set_extension(const char *inname, const char *extension);
  *  list_for_each - regular iterator over list
  *  list_for_each_safe - the same but safe against list items removal
  *  list_last - find the last element in a list
+ *  list_reverse - reverse the order of a list
+ *
+ *  Arguments named with _ + single letter should be temp variables
+ *  of the appropriate pointer type.
  */
 #define list_for_each(pos, head)                        \
     for (pos = head; pos; pos = pos->next)
-#define list_for_each_safe(pos, n, head)                \
-    for (pos = head, n = (pos ? pos->next : NULL); pos; \
-        pos = n, n = (n ? n->next : NULL))
+#define list_for_each_safe(pos, _n, head)                \
+    for (pos = head, _n = (pos ? pos->next : NULL); pos; \
+        pos = _n, _n = (_n ? _n->next : NULL))
 #define list_last(pos, head)                            \
     for (pos = head; pos && pos->next; pos = pos->next) \
         ;
-#define list_reverse(head, prev, next)                  \
+#define list_reverse(head)                              \
     do {                                                \
+        void *_p, *_n;                                  \
         if (!head || !head->next)                       \
             break;                                      \
-        prev = NULL;                                    \
+        _p = NULL;                                      \
         while (head) {                                  \
-            next = head->next;                          \
-            head->next = prev;                          \
-            prev = head;                                \
-            head = next;                                \
+            _n = head->next;                            \
+            head->next = _p;                            \
+            _p = head;                                  \
+            head = _n;                                  \
         }                                               \
-        head = prev;                                    \
+        head = _p;                                      \
     } while (0)
 
 /*


More information about the Nasm-commits mailing list