foxygit / doom Log in
commit 44c551fa8f68a94f643bf60a8590ae7a7c633cf1
Author:     Turo Lamminen <turol@iki.fi>
AuthorDate: Sat Jul 15 17:32:50 2023 +0300
Commit:     Turo Lamminen <turol@users.noreply.github.com>
CommitDate: Mon Jul 17 21:24:23 2023 +0300

    Add linked list loops debugging macro
---
 src/m_misc.h | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/m_misc.h b/src/m_misc.h
index 5f16cc60..7b9e72cc 100644
--- a/src/m_misc.h
+++ b/src/m_misc.h
@@ -64,5 +64,46 @@ int M_vsnprintf(char *buf, size_t buf_len, const char *s, va_list args);
 int M_snprintf(char *buf, size_t buf_len, const char *s, ...) PRINTF_ATTR(3, 4);
 void M_NormalizeSlashes(char *str);

+
+// debugging code to check there are no loops in a linked list
+// disabled unless explicitly requested
+#ifdef DEBUG_LINKED_LISTS
+
+
+#define LINKED_LIST_CHECK_NO_CYCLE(list_type, list, next_member)  \
+    do                                                            \
+    {                                                             \
+        if (list != NULL) {                                       \
+            list_type *slow, *fast;                               \
+            slow = list;                                          \
+            fast = list->next_member;                             \
+            while (fast) {                                        \
+                if (!fast->next_member) {                         \
+                    break;                                        \
+                }                                                 \
+                fast = fast->next_member->next_member;            \
+                slow = slow->next_member;                         \
+                if (slow == fast) {                               \
+                    fprintf(stderr, "loop in linked list " # list " in %s:%d", __FILE__, __LINE__); \
+                    __builtin_trap();                             \
+                }                                                 \
+            }                                                     \
+        }                                                         \
+    } while (0)                                                   \
+
+
+
+#else  // DEBUG_LINKED_LISTS
+
+
+#define LINKED_LIST_CHECK_NO_CYCLE(list_type, list, next_member)  \
+    do                                                            \
+    {                                                             \
+    } while (0)                                                   \
+
+
+#endif  // DEBUG_LINKED_LISTS
+
+
 #endif