Added -fno-reorder-functions to stage2 flags to prevent creation of an
[AROS.git] / rom / exec / insert.c
blobfdaf0e187be5c067f40509ac22f57d7f970008ff
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Insert a node into a list.
6 Lang: english
7 */
8 #include <aros/debug.h>
10 /*****************************************************************************
12 NAME */
13 #include <exec/lists.h>
14 #include <proto/exec.h>
16 AROS_LH3I(void, Insert,
18 /* SYNOPSIS */
19 AROS_LHA(struct List *, list, A0),
20 AROS_LHA(struct Node *, node, A1),
21 AROS_LHA(struct Node *, pred, A2),
23 /* LOCATION */
24 struct ExecBase *, SysBase, 39, Exec)
26 /* FUNCTION
27 Insert Node node after pred in list.
29 INPUTS
30 list - The list to insert the node into
31 node - This node is to be inserted
32 pred - Insert after this node. If this is NULL, node is inserted
33 as the first node (same as AddHead()).
35 RESULT
37 NOTES
39 EXAMPLE
40 struct List * list;
41 struct Node * pred, * node;
43 // Insert Node node as second node in list
44 pred = GetHead (list);
45 Insert (list, node, pred);
47 BUGS
49 SEE ALSO
50 AddHead(), AddTail(), Enqueue(), RemHead(), Remove(), RemTail(),
51 "AROS: Exec Lists".
53 INTERNALS
55 ******************************************************************************/
57 AROS_LIBFUNC_INIT
59 // ASSERT(list != NULL); // MUIBase calls Insert(NULL, ...)
60 ASSERT(node != NULL);
62 /* If we have a node to insert behind... */
63 if (pred)
65 /* Is this the last node in the list ? */
66 if (pred->ln_Succ) /* Normal node ? */
69 Our successor is the successor of the node we add ourselves
70 behind and our predecessor is just the node itself.
72 node->ln_Succ = pred->ln_Succ;
73 node->ln_Pred = pred;
76 We are the predecessor of the successor of our predecessor
77 (What ? blblblb... ;) and of our predecessor itself.
78 Note that here the sequence is quite important since
79 we need ln_Succ in the first expression and change it in
80 the second.
82 pred->ln_Succ->ln_Pred = node;
83 pred->ln_Succ = node;
85 else /* last node */
88 Add the node at the end of the list.
89 Make the node point to the head of the list. Our
90 predecessor is the previous last node of the list.
92 node->ln_Succ = (struct Node *)&list->lh_Tail;
93 node->ln_Pred = list->lh_TailPred;
96 Now we are the last now. Make the old last node point to us
97 and the pointer to the last node, too.
99 list->lh_TailPred->ln_Succ = node;
100 list->lh_TailPred = node;
103 else
106 add at the top of the list. I do not use AddHead() here but
107 write the code twice for two reasons: 1. The code is small and
108 quite prone to errors and 2. If I would call AddHead(), it
109 would take almost as long to call the function as the execution
110 would take yielding 100% overhead.
112 node->ln_Succ = list->lh_Head;
113 node->ln_Pred = (struct Node *)&list->lh_Head;
114 list->lh_Head->ln_Pred = node;
115 list->lh_Head = node;
117 AROS_LIBFUNC_EXIT
118 } /* Insert */