You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/02 01:45:19 UTC

[GitHub] [incubator-nuttx] zyfeier commented on a diff in pull request #6966: nuttx/queue: Change some queue function to macro

zyfeier commented on code in PR #6966:
URL: https://github.com/apache/incubator-nuttx/pull/6966#discussion_r961221199


##########
include/queue.h:
##########
@@ -67,6 +67,223 @@
     } \
   while (0)
 
+#define sq_addfirst(p, q) \
+  do \
+    { \
+      FAR sq_entry_t *tmp_node = p; \
+      tmp_node->flink = (q)->head; \
+      if (!(q)->head) \
+        { \
+          (q)->tail = tmp_node; \
+        } \
+      (q)->head = tmp_node; \
+    } \
+  while (0)
+
+#define dq_addfirst(p, q) \
+  do \
+    { \
+      FAR dq_entry_t *tmp_node = p; \
+      tmp_node->blink = NULL; \
+      tmp_node->flink = (q)->head; \
+      if (!(q)->head) \
+        { \
+          (q)->head = tmp_node; \
+          (q)->tail = tmp_node; \
+        } \
+      else \
+        { \
+          (q)->head->blink = tmp_node; \
+          (q)->head = tmp_node; \
+        } \
+    } \
+  while (0)
+
+#define sq_addlast(p, q) \
+  do \
+    { \
+      FAR sq_entry_t *tmp_node = p; \
+      tmp_node->flink = NULL; \
+      if (!(q)->head) \
+        { \
+          (q)->head = tmp_node; \
+          (q)->tail = tmp_node; \
+        } \
+      else \
+        { \
+          (q)->tail->flink = tmp_node; \
+          (q)->tail        = tmp_node; \
+        } \
+    } \
+  while (0)
+
+#define dq_addlast(p, q) \
+  do \
+    { \
+      FAR dq_entry_t *tmp_node = p; \
+      tmp_node->flink = NULL; \
+      tmp_node->blink = (q)->tail; \
+      if (!(q)->head) \
+        { \
+          (q)->head = tmp_node; \
+          (q)->tail = tmp_node; \
+        } \
+      else \
+        { \
+          (q)->tail->flink = tmp_node; \
+          (q)->tail        = tmp_node; \
+        } \
+    } \
+  while (0)
+
+#define dq_addbefore(n, p, q) \
+  do \
+    { \
+      FAR dq_entry_t *_tmp_node = p; \
+      if (!(q)->head || n == (q)->head) \
+        { \
+          dq_addfirst(_tmp_node, q); \
+        } \
+      else \
+        { \
+          FAR dq_entry_t *tmp_prev = (n)->blink; \
+          _tmp_node->flink = n; \
+          _tmp_node->blink = tmp_prev; \
+          tmp_prev->flink = _tmp_node; \
+          (n)->blink = _tmp_node; \
+        } \
+    } \
+  while (0)
+
+#define sq_for_every(q, p) \
+  for(p = (q)->head; p != NULL; p = (p)->flink)
+
+#define sq_rem(p, q) \
+  do \
+    { \
+      FAR sq_entry_t *tmp_node = p; \
+      if ((q)->head && tmp_node) \
+        { \
+          if (tmp_node == (q)->head) \
+            { \
+              (q)->head = tmp_node->flink; \
+              if (tmp_node == (q)->tail) \
+                { \
+                  (q)->tail = NULL; \
+                } \
+            } \
+          else \
+            { \
+              FAR sq_entry_t *tmp_prev; \
+              sq_for_every(q, tmp_prev) \
+                { \
+                  if (tmp_prev->flink == tmp_node) \
+                    { \
+                      sq_remafter(tmp_prev, q); \
+                    } \
+                } \
+            } \
+        } \
+    } \
+  while (0)
+
+#define dq_rem(p, q) \
+  do \
+    { \
+      FAR dq_entry_t *tmp_node = p; \
+      FAR dq_entry_t *tmp_prev = tmp_node->blink; \
+      FAR dq_entry_t *tmp_next = tmp_node->flink; \
+      if (!tmp_prev) \
+        { \
+          (q)->head = tmp_next; \
+        } \
+      else \
+        { \
+          tmp_prev->flink = tmp_next; \
+        } \
+      if (!tmp_next) \
+        { \
+          (q)->tail = tmp_prev; \
+        } \
+      else \
+        { \
+          tmp_next->blink = tmp_prev; \
+        } \
+      tmp_node->flink = NULL; \
+      tmp_node->blink = NULL; \
+    } \
+  while (0)
+
+#define sq_cat(q1, q2) \
+  do \
+    { \
+      if (sq_empty(q2)) \
+        { \
+          sq_move(q1, q2); \
+        } \
+      else if (!sq_empty(q1)) \
+        { \
+          (q2)->tail->flink = (q1)->head; \
+          (q2)->tail = (q1)->tail; \
+          sq_init(q1); \
+        } \
+    } \
+  while (0)
+
+#define dq_cat(q1, q2) \
+  do \
+    { \
+      if (dq_empty(q2)) \
+        { \
+          dq_move(q1, q2); \
+        } \
+      else if (!dq_empty(q1)) \
+        { \
+          (q2)->tail->flink = (q1)->head; \
+          (q1)->head->blink = (q2)->tail; \
+          (q2)->tail = (q1)->tail; \
+          dq_init(q1); \
+        } \
+    } \
+  while (0)
+
+#define sq_remfirst(q) \
+  ({ \
+    FAR sq_entry_t *tmp_ret = (q)->head; \
+    if (tmp_ret) \
+      { \
+        (q)->head = tmp_ret->flink; \
+        if (!(q)->head) \
+          { \
+            (q)->tail = NULL; \
+          } \
+        tmp_ret->flink = NULL; \
+      } \
+    tmp_ret; \
+  })
+
+#define dq_remfirst(q) \
+  ({ \
+    FAR dq_entry_t *tmp_ret = (q)->head; \
+    if (tmp_ret) \
+      { \
+        FAR dq_entry_t *tmp_next = tmp_ret->flink; \
+        if (!tmp_next) \
+          { \
+            (q)->head = NULL; \
+            (q)->tail = NULL; \
+          } \
+        else \
+          { \
+            (q)->head = tmp_next; \
+            tmp_next->blink = NULL; \
+          } \
+        tmp_ret->flink = NULL; \
+        tmp_ret->blink = NULL; \
+      } \
+    tmp_ret; \
+  })

Review Comment:
   We can add a macro, inline function is used  when the macro is valid, like this:
   
   #ifdef INLINE_FUNCTION
   static inline FAR  dq_entry_t *dq_remfirst(FAR dq_queue_t *queue)
   {
   }
   #else
   FAR  dq_entry_t *dq_remfirst(FAR dq_queue_t *queue);
   #endif
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org