]> git.itanic.dy.fi Git - sdl-planets/commitdiff
does not crash anymore
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 4 Jun 2010 06:46:11 +0000 (09:46 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 4 Jun 2010 06:46:11 +0000 (09:46 +0300)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
quadtree.c

index e0342f8dfbc6da84dffe13b298905a45b78d21f6..1e4e90984c626627d999759ed9c5ef5d16dca294 100644 (file)
@@ -226,7 +226,7 @@ static void _qt_optimal_del(struct quadtree *parent, struct quadtree *node,
 }
 
 static int add_to_relocation_list(struct quadtree *node,
-                                  struct list_head *head)
+                                  struct list_head **head)
 {
        int nodes = 0;
 
@@ -236,11 +236,17 @@ static int add_to_relocation_list(struct quadtree *node,
                nodes += add_to_relocation_list(node->child[1], head);
 
        if (debug) {
-               printf("Adding node %p to relocation list\n", node);
+               printf("Adding node %p to relocation list %p\n",
+                      node, *head);
                fflush(stdout);
        }
 
-       list_add(&node->list, head);
+       if (!*head) {
+               *head = &node->list;
+               INIT_LIST_HEAD(*head);
+       }
+
+       list_add(&node->list, *head);
        nodes++;
 
        if (node->child[2])
@@ -258,28 +264,20 @@ static void relocate_from_list(struct quadtree *start, struct quadtree *end,
        struct quadtree *tmp = start, *node;
        int i, d = nodes / 2;
 
-       for (i = 0; i < nodes; i++) {
-               printf("Node %p, prev %p, next %p\n",
-                      list_to_tree(&tmp->list), 
-                      list_to_tree(tmp->list.prev),
-                      list_to_tree(tmp->list.next));
-               fflush(stdout);
-               tmp = list_to_tree(tmp->list.next);
-       };
-
        for (; nodes > 0; nodes--) {
                if (debug) {
                        printf("stepping %d, %d nodes left\n", d, nodes);
                        fflush(stdout);
                }
 
-/*
                for (i = 0; i < d; i++)
                        tmp = list_to_tree(tmp->list.next);
-*/
 
                if (debug) {
-                       printf("Now moving node %p\n", tmp);
+                       printf("Now moving node %p, next %p , prev %p\n",
+                              tmp,
+                              list_to_tree(tmp->list.prev),
+                              list_to_tree(tmp->list.next));
                        fflush(stdout);
                }
                node = tmp;
@@ -356,12 +354,8 @@ struct quadtree *quadtree_del(struct quadtree *node,
                                parent->parent = 0;
                                continue;
                        }
-                       if (!head) {
-                               head = &node->child[i]->list;
-                               INIT_LIST_HEAD(head);
-                       }
 
-                       nodes += add_to_relocation_list(node->child[i], head);
+                       nodes += add_to_relocation_list(node->child[i], &head);
                }
 
                start = list_to_tree(head);
@@ -402,12 +396,7 @@ struct quadtree *quadtree_del(struct quadtree *node,
                        if (!node->child[i])
                                continue;
 
-                       if (!head) {
-                               head = &node->child[i]->list;
-                               INIT_LIST_HEAD(head);
-                       }
-
-                       nodes += add_to_relocation_list(node->child[i], head);
+                       nodes += add_to_relocation_list(node->child[i], &head);
                }
 
                start = list_to_tree(head);