]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Implement Linux kernel-inspired generic cyclic doubly linked list interface
authorcloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 7 Sep 2020 23:57:34 +0000 (23:57 +0000)
committercloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 7 Sep 2020 23:57:34 +0000 (23:57 +0000)
It's incomplete, leaving out several helpers. These will be added later
if the need arises.

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12910 d7cf8633-e32d-0410-b094-e92efae38249

com_list.c [new file with mode: 0644]
com_list.h [new file with mode: 0644]
common.h
darkplaces-sdl2-vs2017.vcxproj
darkplaces-sdl2-vs2019.vcxproj
makefile.inc
quakedef.h

diff --git a/com_list.c b/com_list.c
new file mode 100644 (file)
index 0000000..cb00148
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+Copyright (C) 2020 David "Cloudwalk" Knapp
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+// com_list.c - generic doubly linked list interface, inspired by Linux list.h
+
+#include "qtypes.h"
+#include "com_list.h"
+
+/*
+ * Insert a node between two known nodes.
+ * 
+ * Only use when prev and next are known.
+ */
+static void __List_Add(llist_t *node, llist_t *prev, llist_t *next)
+{
+       next->prev = node;
+       node->next = next;
+       node->prev = prev;
+       prev->next = node;
+}
+
+/*
+ * Insert a node immediately after head.
+ */
+void List_Add(llist_t *node, llist_t *head)
+{
+       __List_Add(node, head, head->next);
+}
+
+/*
+ * Insert a node immediately before head.
+ */
+void List_Add_Tail(llist_t *node, llist_t *head)
+{
+       __List_Add(node, head->prev, head);
+}
+
+/*
+ * Bridge prev and next together, when removing the parent of them.
+ */
+static void __List_Delete(llist_t *prev, llist_t *next)
+{
+       next->prev = prev;
+       prev->next = next;
+}
+
+/*
+ * Redundant?
+ */
+static void __List_Delete_Node(llist_t *node)
+{
+       __List_Delete(node->prev, node->next);
+}
+
+/*
+ * Removes a node from its list.
+ */
+void List_Delete(llist_t *node)
+{
+       __List_Delete_Node(node);
+       node->next = node->prev = node;
+}
+
+/*
+ * Replace old with new. Old is overwritten if empty.
+ */
+void List_Replace(llist_t *old, llist_t *_new)
+{
+       _new->next = old->next;
+       _new->next->prev = _new;
+       _new->prev = old->prev;
+       _new->prev->next = _new;
+       old->next = old->prev = old;
+}
+
+/*
+ * Swap node1 with node2 in place.
+ */
+void List_Swap(llist_t *node1, llist_t *node2)
+{
+       llist_t *pos = node2->prev;
+       List_Delete(node2);
+       List_Replace(node1, node2);
+       if(pos == node1)
+               pos = node2;
+       List_Add(node1, pos);
+}
+
+/*
+ * Delete list from its... list, then insert after head.
+ */
+void List_Move(llist_t *list, llist_t *head)
+{
+       __List_Delete_Node(list);
+       List_Add(list, head);
+}
+
+/*
+ * Delete list from its... list, then insert before head.
+ */
+void List_Move_Tail(llist_t *list, llist_t *head)
+{
+       __List_Delete_Node(list);
+       List_Add_Tail(list, head);
+}
+
+/*
+ * Move the first node of a range of nodes immediately after head.
+ * All three parameters must belong to the same list.
+ */
+
+void List_Bulk_Move_Tail(llist_t *head, llist_t *first, llist_t *last)
+{
+       first->prev->next = last->next;
+       last->next->prev = first->prev;
+
+       head->prev->next = first;
+       first->prev = head->prev;
+
+       last->next = head;
+       head->prev = last;
+}
+
+/*
+ * Shift the head to the right (like rotating a wheel counterclockwise).
+ * The node immediately to the right becomes the new head.
+ */
+void List_Rotate_Left(llist_t *head)
+{
+       llist_t *first;
+
+       if (!List_IsEmpty(head))
+       {
+               first = head->next;
+               List_Move_Tail(first, head);
+       }
+}
+
+/*
+ * Make list the new head.
+ */
+void List_Rotate_To_Front(llist_t *list, llist_t *head)
+{
+       List_Move_Tail(head, list);
+}
+
+/*
+ * Concatenate two lists. The head of list will be discarded.
+ */
+static void __List_Splice(const llist_t *list, llist_t *prev, llist_t *next)
+{
+       llist_t *first = list->next;
+       llist_t *last = list->prev;
+
+       first->prev = prev;
+       prev->next = first;
+
+       last->next = next;
+       next->prev = last;
+}
+
+/*
+ * Concatenate two lists. The first node of list will be inserted after head.
+ */
+void List_Splice(const llist_t *list, llist_t *head)
+{
+       if(!List_IsEmpty(list))
+               __List_Splice(list, head, head->next);
+}
+
+/*
+ * Concatenate two lists. The tail of list will be inserted before head.
+ */
+void List_Splice_Tail(const llist_t *list, llist_t *head)
+{
+       if (!List_IsEmpty(list))
+               __List_Splice(list, head->prev, head);
+}
+
+qboolean List_IsFirst(llist_t *list, llist_t *start)
+{
+       return list->prev == start;
+}
+
+qboolean List_IsLast(llist_t *list, llist_t *start)
+{
+       return list->next == start;
+}
+
+qboolean List_IsEmpty(const llist_t *list)
+{
+       return list->next == list;
+}
\ No newline at end of file
diff --git a/com_list.h b/com_list.h
new file mode 100644 (file)
index 0000000..eac2882
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+Copyright (C) 2020 David "Cloudwalk" Knapp
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+// com_list.c - generic doubly linked list interface, inspired by Linux list.h
+
+#include <stddef.h>
+
+typedef struct llist_s
+{
+       struct llist_s *prev;
+       struct llist_s *next;
+} llist_t;
+
+#define List_Head_Reset(name) { &(name), &(name) }
+
+#define List_Container(ptr, type, member) ContainerOf(ptr, type, member)
+
+#define List_ForEach(pos, head) \
+       for (pos = (head)->next; pos != (head); pos = pos->next)
+
+#define List_ForEach_Prev(pos, head) \
+       for (pos = (head)->prev; pos != (head); pos = pos->prev)
+
+void List_Add(llist_t *node, llist_t *start);
+void List_Add_Tail(llist_t *node, llist_t *start);
+void List_Delete(llist_t *node);
+void List_Replace(llist_t *old, llist_t *_new);
+void List_Swap(llist_t *node1, llist_t *node2);
+void List_Move(llist_t *list, llist_t *start);
+void List_Move_Tail(llist_t *list, llist_t *start);
+void List_Bulk_Move_Tail(llist_t *start, llist_t *first, llist_t *last);
+void List_Rotate_Left(llist_t *head);
+void List_Rotate_To_Front(llist_t *list, llist_t *head);
+void List_Splice(const llist_t *list, llist_t *head);
+void List_Splice_Tail(const llist_t *list, llist_t *head);
+qboolean List_IsFirst(llist_t *list, llist_t *start);
+qboolean List_IsLast(llist_t *list, llist_t *start);
+qboolean List_IsEmpty(const llist_t *list);
\ No newline at end of file
index 15ca90d097399660324b0fb043867cb855f2f1c5..b92ca9aa1c36cb19d3aa9ab9b84b3934c68c1cc6 100644 (file)
--- a/common.h
+++ b/common.h
@@ -38,6 +38,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 //============================================================================
 
+#define ContainerOf(ptr, type, member) (type *)((void *)&(ptr) - offsetof(type, member))
+
 typedef struct sizebuf_s
 {
        qboolean        allowoverflow;  ///< if false, do a Sys_Error
index 5eb9906051e2981d9e114d05e6e0d80c9c3c04ce..2b23d8e8b7786a31b18a44ab947617d6334460eb 100644 (file)
        <ClCompile Include="com_ents.c" />\r
        <ClCompile Include="com_ents4.c" />\r
        <ClCompile Include="com_game.c" />\r
+       <ClCompile Include="com_list.c" />\r
        <ClCompile Include="com_msg.c" />\r
     <ClCompile Include="common.c" />\r
     <ClCompile Include="console.c" />\r
     <ClInclude Include="clvm_cmds.h" />\r
     <ClInclude Include="cmd.h" />\r
     <ClInclude Include="collision.h" />\r
+       <ClInclude Include="com_list.h" />\r
     <ClInclude Include="common.h" />\r
     <ClInclude Include="console.h" />\r
     <ClInclude Include="crypto.h" />\r
index 138b58a9708f03faa129db1c39c6d28be52eba39..0eea6d48e85a127fecc1183d3f11f63a50dffd6f 100644 (file)
        <ClCompile Include="com_ents.c" />\r
        <ClCompile Include="com_ents4.c" />\r
        <ClCompile Include="com_game.c" />\r
+       <ClCompile Include="com_list.c" />\r
        <ClCompile Include="com_msg.c" />\r
     <ClCompile Include="common.c" />\r
     <ClCompile Include="console.c" />\r
     <ClInclude Include="clvm_cmds.h" />\r
     <ClInclude Include="cmd.h" />\r
     <ClInclude Include="collision.h" />\r
+       <ClInclude Include="com_list.h" />\r
     <ClInclude Include="common.h" />\r
     <ClInclude Include="console.h" />\r
     <ClInclude Include="crypto.h" />\r
index b2255ab9852cecc1217c616d288a55070695c9fc..51b64d15eb85857aad1060a2d6a173a7507ce0fd 100644 (file)
@@ -101,6 +101,7 @@ OBJ_COMMON= \
        com_ents.o \
        com_ents4.o \
        com_game.o \
+       com_list.o \
        com_msg.o \
        common.o \
        console.o \
index 41c91c9644a1c5fde554db33c3c8d42fff4a1f7a..ff6a39433fa23f25ddba479b6886ddd811c585de 100644 (file)
@@ -371,6 +371,7 @@ extern char engineversion[128];
 #include "zone.h"
 #include "fs.h"
 #include "common.h"
+#include "com_list.h"
 #include "cvar.h"
 #include "bspfile.h"
 #include "sys.h"