From cbff794b55c94c3c38f4017d21d78b9fa1743f2c Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 21 Aug 2024 09:58:30 +0800
Subject: [PATCH] Add list test example code

---
 booster/testcase/list.c |   62 +++++++++++++++++++++++++++++++
 1 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/booster/testcase/list.c b/booster/testcase/list.c
new file mode 100644
index 0000000..a16943a
--- /dev/null
+++ b/booster/testcase/list.c
@@ -0,0 +1,62 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
+ *                  All rights reserved.
+ *
+ *       Filename:  list.c
+ *    Description:  This is the list API from linux kernel example code.
+ *
+ *         Notice:  Memory leak check: valgrind --leak-check=full ./list
+ *
+ *        Version:  1.0.0(08/08/2012~)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
+ *
+ ********************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "list.h"
+
+typedef struct node_s
+{
+    int                 data;
+    struct list_head    list;
+} node_t;
+
+int main (int argc, char **argv)
+{
+    int                 i;
+    struct list_head    head;
+    node_t             *node, *tmp;
+
+    INIT_LIST_HEAD(&head);
+
+    /* Create the link list */
+    for(i=0; i<10; i++)
+    {
+        node = malloc(sizeof(*node));
+        node->data = i;
+
+        printf("List add an node data : %p->%d\n", node, node->data);
+        list_add_tail(&node->list, &head);
+    }
+    printf("\n");
+
+    /* Use list_for_each_entry to travel list, we can not remove the node in it */
+    list_for_each_entry(node, &head, list)
+    {
+        printf("List travel node data : %p->%d\n", node, node->data);
+    }
+    printf("\n");
+
+    /* Use list_for_each_entry_safe to destroy the list */
+    list_for_each_entry_safe(node, tmp, &head, list)
+    {
+        printf("List remove node data : %p->%d\n", node, node->data);
+        list_del(&node->list);
+		free(node);
+    }
+    printf("\n");
+
+    return 0;
+}

--
Gitblit v1.9.1