From daacb87180fa9c98c53c001bb37929b9aaef46d9 Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Mon, 15 Jul 2024 11:27:19 +0800
Subject: [PATCH] cleancode: refactor lwipgz_list.h

  Changed:
    list_is_empty       -> list_head_empty
    list_is_null        -> list_node_null

    init_list_node      -> list_init_head
    init_list_node_null -> list_init_node

    list_add_node       -> list_add_node
    list_del_node_null  -> list_del_node
    list_del_node       -> __list_del_node
    list_for_each_safe  -> list_for_each_node
  Deprecated:
    list_del_node_init
  Added:
    list_get_node_count
    list_entry

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/common/gazelle_base_func.h          |  5 +-
 src/lstack/api/lstack_epoll.c           | 66 ++++++++++++-------------
 src/lstack/api/lstack_rtc_api.c         |  2 +-
 src/lstack/core/lstack_lwip.c           | 28 +++++------
 src/lstack/core/lstack_protocol_stack.c | 14 +++---
 src/lstack/core/lstack_stack_stat.c     |  4 +-
 6 files changed, 60 insertions(+), 59 deletions(-)

diff --git a/src/common/gazelle_base_func.h b/src/common/gazelle_base_func.h
index 2d629c1..be87ccd 100644
--- a/src/common/gazelle_base_func.h
+++ b/src/common/gazelle_base_func.h
@@ -36,7 +36,8 @@ int32_t filename_check(const char* args);
 
 void gazelle_exit(void);
 
-#undef container_of
-#define container_of(ptr, type, field) ((type *)(void*)(((char *)(ptr)) - offsetof(type, field)))
+/* Do not check if the type of ptr and type->member are the same */
+#define container_of_uncheck_ptr(ptr, type, member) \
+    ((type *)(void*)(((char *)(ptr)) - offsetof(type, member)))
 
 #endif /* ifndef __GAZELLE_BASE_FUNC_H__ */
diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 06c708b..2f48606 100644
--- a/src/lstack/api/lstack_epoll.c
+++ b/src/lstack/api/lstack_epoll.c
@@ -49,8 +49,8 @@ static void change_epollfd_kernel_thread(struct wakeup_poll *wakeup, struct prot
 
 static inline void add_wakeup_to_stack_wakeuplist(struct wakeup_poll *wakeup, struct protocol_stack *stack)
 {
-    if (list_is_null(&wakeup->wakeup_list[stack->stack_idx])) {
-        list_add_node(&stack->wakeup_list, &wakeup->wakeup_list[stack->stack_idx]);
+    if (list_node_null(&wakeup->wakeup_list[stack->stack_idx])) {
+        list_add_node(&wakeup->wakeup_list[stack->stack_idx], &stack->wakeup_list);
     }
 }
 
@@ -73,8 +73,8 @@ void add_sock_event_nolock(struct lwip_sock *sock, uint32_t event)
     }
 
     sock->events |= (event == EPOLLERR) ? (EPOLLIN | EPOLLERR) : (event & sock->epoll_events);
-    if (list_is_null(&sock->event_list)) {
-        list_add_node(&wakeup->event_list, &sock->event_list);
+    if (list_node_null(&sock->event_list)) {
+        list_add_node(&sock->event_list, &wakeup->event_list);
     }
     return;
 }
@@ -111,7 +111,7 @@ void del_sock_event_nolock(struct lwip_sock *sock, uint32_t event)
     }
 
     if (sock->events == 0) {
-        list_del_node_null(&sock->event_list);
+        list_del_node(&sock->event_list);
     }
     return;
 }
@@ -127,7 +127,7 @@ void wakeup_stack_epoll(struct protocol_stack *stack)
 {
     struct list_node *node, *temp;
 
-    list_for_each_safe(node, temp, &stack->wakeup_list) {
+    list_for_each_node(node, temp, &stack->wakeup_list) {
         /* When temp is NULL, find the tail node in the wekeup_list and connect it to the back of the node */
         if (unlikely(temp == NULL)) {
             struct list_node *nod = &stack->wakeup_list;
@@ -139,7 +139,7 @@ void wakeup_stack_epoll(struct protocol_stack *stack)
             temp = nod;
         }
 
-        struct wakeup_poll *wakeup = container_of((node - stack->stack_idx), struct wakeup_poll, wakeup_list);
+        struct wakeup_poll *wakeup = container_of_uncheck_ptr((node - stack->stack_idx), struct wakeup_poll, wakeup_list);
 
         if (__atomic_load_n(&wakeup->in_wait, __ATOMIC_ACQUIRE)) {
             __atomic_store_n(&wakeup->in_wait, false, __ATOMIC_RELEASE);
@@ -148,7 +148,7 @@ void wakeup_stack_epoll(struct protocol_stack *stack)
             stack->stats.wakeup_events++;
         }
 
-        list_del_node_null(&wakeup->wakeup_list[stack->stack_idx]);
+        list_del_node(&wakeup->wakeup_list[stack->stack_idx]);
     }
 }
 
@@ -198,8 +198,8 @@ static void rtc_raise_pending_events(struct wakeup_poll *wakeup, struct lwip_soc
     if (event) {
         sock->events = event;
         if (wakeup->type == WAKEUP_EPOLL && (sock->events & sock->epoll_events) &&
-            list_is_null(&sock->event_list)) {
-            list_add_node(&wakeup->event_list, &sock->event_list);
+            list_node_null(&sock->event_list)) {
+            list_add_node(&sock->event_list, &wakeup->event_list);
         }
     }
 }
@@ -227,8 +227,8 @@ static void raise_pending_events(struct wakeup_poll *wakeup, struct lwip_sock *s
     if (event) {
         sock->events = event;
         if (wakeup->type == WAKEUP_EPOLL && (sock->events & sock->epoll_events) &&
-            list_is_null(&sock->event_list)) {
-            list_add_node(&wakeup->event_list, &sock->event_list);
+            list_node_null(&sock->event_list)) {
+            list_add_node(&sock->event_list, &wakeup->event_list);
         }
     }
     pthread_spin_unlock(&wakeup->event_list_lock);
@@ -255,7 +255,7 @@ int32_t lstack_do_epoll_create(int32_t fd)
     }
 
     for (uint32_t i = 0; i < PROTOCOL_STACK_MAX; i++) {
-        init_list_node_null(&wakeup->wakeup_list[i]);
+        list_init_node(&wakeup->wakeup_list[i]);
     }
 
     if (sem_init(&wakeup->wait, 0, 0) != 0) {
@@ -266,12 +266,12 @@ int32_t lstack_do_epoll_create(int32_t fd)
     __atomic_store_n(&wakeup->in_wait, false, __ATOMIC_RELEASE);
 
     struct protocol_stack_group *stack_group = get_protocol_stack_group();
-    init_list_node_null(&wakeup->poll_list);
+    list_init_node(&wakeup->poll_list);
     pthread_spin_lock(&stack_group->poll_list_lock);
-    list_add_node(&stack_group->poll_list, &wakeup->poll_list);
+    list_add_node(&wakeup->poll_list, &stack_group->poll_list);
     pthread_spin_unlock(&stack_group->poll_list_lock);
 
-    init_list_node(&wakeup->event_list);
+    list_init_head(&wakeup->event_list);
     pthread_spin_init(&wakeup->event_list_lock, PTHREAD_PROCESS_PRIVATE);
 
     wakeup->type = WAKEUP_EPOLL;
@@ -327,15 +327,15 @@ int32_t lstack_epoll_close(int32_t fd)
 
     struct list_node *node, *temp;
     pthread_spin_lock(&wakeup->event_list_lock);
-    list_for_each_safe(node, temp, &wakeup->event_list) {
-        struct lwip_sock *sock = container_of(node, struct lwip_sock, event_list);
-        list_del_node_null(&sock->event_list);
+    list_for_each_node(node, temp, &wakeup->event_list) {
+        struct lwip_sock *sock = list_entry(node, struct lwip_sock, event_list);
+        list_del_node(&sock->event_list);
     }
     pthread_spin_unlock(&wakeup->event_list_lock);
     pthread_spin_destroy(&wakeup->event_list_lock);
 
     pthread_spin_lock(&stack_group->poll_list_lock);
-    list_del_node_null(&wakeup->poll_list);
+    list_del_node(&wakeup->poll_list);
     pthread_spin_unlock(&stack_group->poll_list_lock);
 
     sem_destroy(&wakeup->wait);
@@ -413,7 +413,7 @@ int32_t lstack_rtc_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
             break;
         case EPOLL_CTL_DEL:
             sock->epoll_events = 0;
-            list_del_node_null(&sock->event_list);
+            list_del_node(&sock->event_list);
             break;
         default:
             GAZELLE_RETURN(EINVAL);
@@ -464,7 +464,7 @@ int32_t lstack_rtw_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_
                 sock->epoll_events = 0;
                 wakeup->stack_fd_cnt[sock->stack->stack_idx]--;
                 pthread_spin_lock(&wakeup->event_list_lock);
-                list_del_node_null(&sock->event_list);
+                list_del_node(&sock->event_list);
                 pthread_spin_unlock(&wakeup->event_list_lock);
                 break;
             default:
@@ -482,18 +482,18 @@ int32_t epoll_lwip_event_nolock(struct wakeup_poll *wakeup, struct epoll_event *
     int32_t event_num = 0;
     struct list_node *node, *temp;
 
-    list_for_each_safe(node, temp, &wakeup->event_list) {
-        struct lwip_sock *sock = container_of(node, struct lwip_sock, event_list);
+    list_for_each_node(node, temp, &wakeup->event_list) {
+        struct lwip_sock *sock = list_entry(node, struct lwip_sock, event_list);
 
         if ((sock->epoll_events & sock->events) == 0) {
-            list_del_node_null(node);
+            list_del_node(node);
             continue;
         }
         
         if (event_num >= maxevents) {
             /* move list head after the current node, and start traversing from this node next time */
-            list_del_node_null(&wakeup->event_list);
-            list_add_node(node, &wakeup->event_list);
+            list_del_node(&wakeup->event_list);
+            list_add_node(&wakeup->event_list, node);
             break;
         }
         
@@ -502,14 +502,14 @@ int32_t epoll_lwip_event_nolock(struct wakeup_poll *wakeup, struct epoll_event *
         event_num++;
 
         if (sock->epoll_events & EPOLLET) {
-            list_del_node_null(node);
+            list_del_node(node);
             sock->events = 0;
         }
 
         /* EPOLLONESHOT: generate event after epoll_ctl add/mod event again
            epoll_event set 0 avoid generating event util epoll_ctl reset epoll_event */
         if (sock->epoll_events & EPOLLONESHOT) {
-            list_del_node_null(node);
+            list_del_node(node);
             sock->epoll_events = 0;
         }
     }
@@ -654,7 +654,7 @@ int32_t lstack_rtc_epoll_wait(int32_t epfd, struct epoll_event* events, int32_t
         }
 
         loop_flag = false;
-        if (!kernel_num && list_is_empty(&wakeup->event_list) && tmptimeout != 0) {
+        if (!kernel_num && list_head_empty(&wakeup->event_list) && tmptimeout != 0) {
             loop_flag = true;
         }
     } while (loop_flag);
@@ -721,7 +721,7 @@ static int32_t init_poll_wakeup_data(struct wakeup_poll *wakeup)
     __atomic_store_n(&wakeup->in_wait, false, __ATOMIC_RELEASE);
 
     for (uint32_t i = 0; i < PROTOCOL_STACK_MAX; i++) {
-        init_list_node_null(&wakeup->wakeup_list[i]);
+        list_init_node(&wakeup->wakeup_list[i]);
     }
 
     wakeup->epollfd = posix_api->epoll_create_fn(POLL_KERNEL_EVENTS);
@@ -745,9 +745,9 @@ static int32_t init_poll_wakeup_data(struct wakeup_poll *wakeup)
     }
 
     struct protocol_stack_group *stack_group = get_protocol_stack_group();
-    init_list_node_null(&wakeup->poll_list);
+    list_init_node(&wakeup->poll_list);
     pthread_spin_lock(&stack_group->poll_list_lock);
-    list_add_node(&stack_group->poll_list, &wakeup->poll_list);
+    list_add_node(&wakeup->poll_list, &stack_group->poll_list);
     pthread_spin_unlock(&stack_group->poll_list_lock);
 
     int32_t stack_count[PROTOCOL_STACK_MAX] = {0};
diff --git a/src/lstack/api/lstack_rtc_api.c b/src/lstack/api/lstack_rtc_api.c
index 97623b3..ca0e7ec 100644
--- a/src/lstack/api/lstack_rtc_api.c
+++ b/src/lstack/api/lstack_rtc_api.c
@@ -56,7 +56,7 @@ int rtc_socket(int domain, int type, int protocol)
         sock->epoll_events = 0;
         sock->events = 0;
         sock->wakeup = NULL;
-        init_list_node_null(&sock->event_list);
+        list_init_node(&sock->event_list);
     }
     return ret;
 }
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index f52f39f..c05a763 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -173,8 +173,8 @@ int do_lwip_init_sock(int32_t fd)
 
     sock->stack = stack;
 
-    init_list_node_null(&sock->recv_list);
-    init_list_node_null(&sock->event_list);
+    list_init_node(&sock->recv_list);
+    list_init_node(&sock->event_list);
     return 0;
 }
 
@@ -187,7 +187,7 @@ void do_lwip_clean_sock(int fd)
 
     if (sock->wakeup && sock->wakeup->type == WAKEUP_EPOLL) {
         pthread_spin_lock(&sock->wakeup->event_list_lock);
-        list_del_node_null(&sock->event_list);
+        list_del_node(&sock->event_list);
         pthread_spin_unlock(&sock->wakeup->event_list_lock);
     }
 
@@ -195,7 +195,7 @@ void do_lwip_clean_sock(int fd)
 
     reset_sock_data(sock);
 
-    list_del_node_null(&sock->recv_list);
+    list_del_node(&sock->recv_list);
 }
 
 void do_lwip_free_pbuf(struct pbuf *pbuf)
@@ -1098,8 +1098,8 @@ void do_lwip_add_recvlist(int32_t fd)
 {
     struct lwip_sock *sock = lwip_get_socket(fd);
 
-    if (sock && sock->stack && list_is_null(&sock->recv_list)) {
-        list_add_node(&sock->stack->recv_list, &sock->recv_list);
+    if (sock && sock->stack && list_node_null(&sock->recv_list)) {
+        list_add_node(&sock->recv_list, &sock->stack->recv_list);
     }
 }
 
@@ -1109,8 +1109,8 @@ void read_same_node_recv_list(struct protocol_stack *stack)
     struct list_node *node, *temp;
     struct lwip_sock *sock;
 
-    list_for_each_safe(node, temp, list) {
-        sock = container_of(node, struct lwip_sock, recv_list);
+    list_for_each_node(node, temp, list) {
+        sock = list_entry(node, struct lwip_sock, recv_list);
 
         if (sock->same_node_rx_ring != NULL && same_node_ring_count(sock)) {
             add_sock_event(sock, EPOLLIN);
@@ -1125,18 +1125,18 @@ void do_lwip_read_recvlist(struct protocol_stack *stack, uint32_t max_num)
     struct lwip_sock *sock;
     uint32_t read_num = 0;
 
-    list_for_each_safe(node, temp, list) {
-        sock = container_of(node, struct lwip_sock, recv_list);
+    list_for_each_node(node, temp, list) {
+        sock = list_entry(node, struct lwip_sock, recv_list);
 
         if (++read_num > max_num) {
             /* list head move to next send */
             list_del_node(&stack->recv_list);
-            list_add_node(&sock->recv_list, &stack->recv_list);
+            list_add_node(&stack->recv_list, &sock->recv_list);
             break;
         }
 
         if (sock->conn == NULL || sock->conn->recvmbox == NULL || rte_ring_count(sock->conn->recvmbox->ring) == 0) {
-            list_del_node_null(&sock->recv_list);
+            list_del_node(&sock->recv_list);
             continue;
         }
 
@@ -1215,7 +1215,7 @@ static void copy_pcb_to_conn(struct gazelle_stat_lstack_conn_info *conn, const s
             conn->send_ring_cnt = (sock->send_ring == NULL) ? 0 : gazelle_ring_readover_count(sock->send_ring);
             conn->events = sock->events;
             conn->epoll_events = sock->epoll_events;
-            conn->eventlist = !list_is_null(&sock->event_list);
+            conn->eventlist = !list_node_null(&sock->event_list);
         }
     }
 }
@@ -1426,7 +1426,7 @@ err_t find_same_node_memzone(struct tcp_pcb *pcb, struct lwip_sock *nsock)
 
     /* rcvlink init in alloc_socket() */
     /* remove from g_rcv_process_list in free_socket */
-    list_add_node(&nsock->stack->same_node_recv_list, &nsock->recv_list);
+    list_add_node(&nsock->recv_list, &nsock->stack->same_node_recv_list);
     return 0;
 }
 
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index 4f5cfae..9bb1b6a 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -328,9 +328,9 @@ static int32_t init_stack_value(struct protocol_stack *stack, void *arg)
     stack->stack_idx = t_params->idx;
     stack->lwip_stats = &lwip_stats;
 
-    init_list_node(&stack->recv_list);
-    init_list_node(&stack->same_node_recv_list);
-    init_list_node(&stack->wakeup_list);
+    list_init_head(&stack->recv_list);
+    list_init_head(&stack->same_node_recv_list);
+    list_init_head(&stack->wakeup_list);
 
     sys_calibrate_tsc();
     stack_stat_init();
@@ -588,7 +588,7 @@ int32_t stack_group_init(void)
     struct protocol_stack_group *stack_group = get_protocol_stack_group();
     stack_group->stack_num = 0;
 
-    init_list_node(&stack_group->poll_list);
+    list_init_head(&stack_group->poll_list);
     pthread_spin_init(&stack_group->poll_list_lock, PTHREAD_PROCESS_PRIVATE);
     pthread_spin_init(&stack_group->socket_lock, PTHREAD_PROCESS_PRIVATE);
     if (sem_init(&stack_group->sem_stack_setup, 0, 0) < 0) {
@@ -1013,7 +1013,7 @@ void stack_clean_epoll(struct rpc_msg *msg)
     struct protocol_stack *stack = get_protocol_stack();
     struct wakeup_poll *wakeup = (struct wakeup_poll *)msg->args[MSG_ARG_0].p;
 
-    list_del_node_null(&wakeup->wakeup_list[stack->stack_idx]);
+    list_del_node(&wakeup->wakeup_list[stack->stack_idx]);
 }
 
 void stack_mempool_size(struct rpc_msg *msg)
@@ -1103,7 +1103,7 @@ void stack_recvlist_count(struct rpc_msg *msg)
     struct list_node *node;
     struct list_node *temp;
 
-    list_for_each_safe(node, temp, list) {
+    list_for_each_node(node, temp, list) {
         count++;
     }
 
@@ -1258,7 +1258,7 @@ static void inline del_accept_in_event(struct lwip_sock *sock)
     if (!NETCONN_IS_ACCEPTIN(sock)) {
         sock->events &= ~EPOLLIN;
         if (sock->events == 0) {
-            list_del_node_null(&sock->event_list);
+            list_del_node(&sock->event_list);
         }
     }
 
diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c
index e42efde..0411d9e 100644
--- a/src/lstack/core/lstack_stack_stat.c
+++ b/src/lstack/core/lstack_stack_stat.c
@@ -230,8 +230,8 @@ static void get_wakeup_stat(struct protocol_stack_group *stack_group, struct pro
 
     pthread_spin_lock(&stack_group->poll_list_lock);
 
-    list_for_each_safe(node, temp, &stack_group->poll_list) {
-        struct wakeup_poll *wakeup = container_of(node, struct wakeup_poll, poll_list);
+    list_for_each_node(node, temp, &stack_group->poll_list) {
+        struct wakeup_poll *wakeup = list_entry(node, struct wakeup_poll, poll_list);
 
         if (wakeup->bind_stack == stack) {
             stat->kernel_events += wakeup->stat.kernel_events;
-- 
2.33.0

