|
20 | 20 | #define QUEUE_MAX_LINKS 400
|
21 | 21 |
|
22 | 22 | typedef struct Link {
|
23 |
| - struct Link *next; |
24 |
| - struct Link *prev; |
25 |
| - struct Queue *queue; |
26 |
| - void *object; |
| 23 | + struct Link *next; |
| 24 | + struct Link *prev; |
| 25 | + struct Queue *queue; |
| 26 | + void *object; |
27 | 27 | } Link;
|
28 | 28 |
|
29 | 29 | typedef struct Queue {
|
30 |
| - struct Link *first; |
31 |
| - struct Link *last; |
32 |
| - int nitems; |
| 30 | + struct Link *first; |
| 31 | + struct Link *last; |
| 32 | + int nitems; |
33 | 33 | } Queue;
|
34 | 34 |
|
35 | 35 | inline
|
36 |
| - void |
| 36 | +void |
37 | 37 | QueueLinkInit (Link *l, void *obj)
|
38 | 38 | {
|
39 |
| - l->next = NULL; |
40 |
| - l->object = obj; |
| 39 | + l->next = NULL; |
| 40 | + l->object = obj; |
41 | 41 | }
|
42 | 42 |
|
43 | 43 | inline
|
44 |
| - Link * |
| 44 | +Link * |
45 | 45 | QueueNext (Link *l)
|
46 | 46 | {
|
47 |
| - return (l->next); |
| 47 | + return (l->next); |
48 | 48 | }
|
49 | 49 |
|
50 | 50 | inline
|
51 |
| - Link * |
| 51 | +Link * |
52 | 52 | QueuePrev (Link *l)
|
53 | 53 | {
|
54 |
| - return (l->prev); |
| 54 | + return (l->prev); |
55 | 55 | }
|
56 | 56 |
|
57 | 57 | inline
|
58 |
| - Link * |
| 58 | +Link * |
59 | 59 | QueueFirst (Queue *q)
|
60 | 60 | {
|
61 |
| - return (q->first); |
| 61 | + return (q->first); |
62 | 62 | }
|
63 | 63 |
|
64 | 64 | inline
|
65 |
| - Link * |
| 65 | +Link * |
66 | 66 | QueueLast (Queue *q)
|
67 | 67 | {
|
68 |
| - return (q->last); |
| 68 | + return (q->last); |
69 | 69 | }
|
70 | 70 |
|
71 |
| -inline |
72 |
| - void |
73 |
| -QueuePrint(Queue *q) { |
74 |
| - Link *l = q->first; |
75 |
| - int i = 0; |
76 |
| - |
77 |
| - dbprintf('q', "\tFirst: %p Last: %p\n",q->first,q->last); |
78 |
| - while (l != NULL) { |
79 |
| - dbprintf('q', "\tLink: %p o(%p)\n",l,l->object); |
80 |
| - l = l->next; |
81 |
| - if (i++ == 10) |
82 |
| - break; |
83 |
| - } |
84 |
| - |
85 |
| - dbprintf('q', "Done printing links of (%p)\n",q); |
86 |
| -} |
87 | 71 |
|
88 | 72 | inline
|
89 |
| - void |
| 73 | +void |
90 | 74 | QueueInsertAfter (Queue *q, Link *after, Link *l)
|
91 | 75 | {
|
92 |
| - dbprintf('q', "Inserting link (%p) after (%p)\n",l->object,after); |
93 |
| - l->queue = q; |
94 |
| - |
95 |
| - if (after) { |
| 76 | + l->queue = q; |
96 | 77 | l->prev = after;
|
97 |
| - dbprintf('q', "After->%p\n",after->next); |
98 | 78 | l->next = after->next;
|
99 | 79 | after->next = l;
|
100 | 80 | l->next->prev = l;
|
101 |
| - |
102 |
| - if (q->last == after) { |
103 |
| - q->last = after->next; |
104 |
| - } |
105 |
| - } else if (q->nitems > 0) { |
106 |
| - q->first->prev = l; |
107 |
| - l->next = q->first; |
108 |
| - q->first = l; |
109 |
| - q->first->prev = NULL; |
110 |
| - } else { |
111 |
| - q->first = l; |
112 |
| - |
113 |
| - if (!q->last) |
114 |
| - q->last = q->first; |
115 |
| - } |
116 |
| - |
117 |
| - q->nitems += 1; |
118 |
| - |
119 |
| - if (l->object) |
120 |
| - QueuePrint(q); |
| 81 | + q->nitems += 1; |
121 | 82 | }
|
122 | 83 |
|
123 | 84 | inline
|
124 |
| - void |
| 85 | +void |
125 | 86 | QueueInsertFirst (Queue *q, Link *l)
|
126 | 87 | {
|
127 |
| - QueueInsertAfter (q, NULL, l); |
| 88 | + QueueInsertAfter (q, (Link *)q, l); |
128 | 89 | }
|
129 | 90 |
|
130 | 91 | inline
|
131 |
| - void |
| 92 | +void |
132 | 93 | QueueInsertLast (Queue *q, Link *l)
|
133 | 94 | {
|
134 |
| - QueueInsertAfter (q, QueueLast(q), l); |
| 95 | + QueueInsertAfter (q, QueueLast(q), l); |
135 | 96 | }
|
136 | 97 |
|
137 | 98 | inline
|
138 |
| - void |
| 99 | +void |
139 | 100 | QueueRemove (Link *l)
|
140 | 101 | {
|
141 |
| - Queue *q = l->queue; |
142 |
| - dbprintf('q', "Removing entry (%p->%p)\n",l,l->object); |
143 |
| - QueuePrint(q); |
144 |
| - |
145 |
| - if (q->nitems > 1) { |
146 |
| - if (q->first == l) |
147 |
| - q->first = q->first->next; |
148 |
| - else if (q->last == l) { |
149 |
| - q->last = q->last->prev; |
150 |
| - q->last->next = NULL; |
151 |
| - } else { |
152 |
| - l->prev->next = l->next; |
153 |
| - l->next->prev = l->prev; |
| 102 | + if (l->queue->nitems > 0) { |
| 103 | + l->prev->next = l->next; |
| 104 | + l->next->prev = l->prev; |
| 105 | + l->queue->nitems -= 1; |
154 | 106 | }
|
155 |
| - } else { |
156 |
| - q->first = NULL; |
157 |
| - q->last = NULL; |
158 |
| - } |
159 |
| - |
160 |
| - q->nitems -= 1; |
161 |
| - |
162 |
| - l->prev = NULL; |
163 |
| - l->next = NULL; |
164 |
| - |
165 |
| - dbprintf('q', "Removing entry (%p->%p)\n",l,l->object); |
166 |
| - QueuePrint(q); |
| 107 | + l->next = NULL; |
167 | 108 | }
|
168 | 109 |
|
169 | 110 | inline
|
170 |
| - int |
| 111 | +int |
171 | 112 | QueueLength (Queue *q)
|
172 | 113 | {
|
173 |
| - return (q->nitems); |
| 114 | + return (q->nitems); |
174 | 115 | }
|
175 | 116 |
|
176 | 117 | inline
|
177 |
| - int |
| 118 | +int |
178 | 119 | QueueEmpty (Queue *q)
|
179 | 120 | {
|
180 |
| - return (QueueLength (q) == 0); |
181 |
| -} |
182 |
| - |
183 |
| -inline |
184 |
| -void |
185 |
| -LinkMoveToLast(Link *l) { |
186 |
| - Queue *q = l->queue; |
187 |
| - QueueRemove(l); |
188 |
| - |
189 |
| - QueueInsertLast(q, l); |
| 121 | + return (QueueLength (q) == 0); |
190 | 122 | }
|
191 | 123 |
|
192 | 124 | extern void QueueModuleInit ();
|
|
0 commit comments