Skip to content

Commit c68a438

Browse files
author
Shane J Gianelli
committed
fixed queues for real this time
1 parent 33102e0 commit c68a438

File tree

2 files changed

+37
-107
lines changed

2 files changed

+37
-107
lines changed

queue.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ QueueModuleInit ()
2020
for (i = 0; i < QUEUE_MAX_LINKS; i++) {
2121
dbprintf ('i', "Initializing queue link %d.\n", i);
2222
linkpool[i].next = NULL;
23-
linkpool[i].prev = NULL;
24-
linkpool[i].object = NULL;
2523
QueueFreeLink (&(linkpool[i]));
2624
}
2725
}
2826

2927
void
3028
QueueInit (Queue *q)
3129
{
32-
q->first = NULL;
33-
q->last = NULL;
30+
q->first = (Link *)q;
31+
q->last = (Link *)q;
3432
q->nitems = 0;
3533
}
3634

queue.h

+35-103
Original file line numberDiff line numberDiff line change
@@ -20,173 +20,105 @@
2020
#define QUEUE_MAX_LINKS 400
2121

2222
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;
2727
} Link;
2828

2929
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;
3333
} Queue;
3434

3535
inline
36-
void
36+
void
3737
QueueLinkInit (Link *l, void *obj)
3838
{
39-
l->next = NULL;
40-
l->object = obj;
39+
l->next = NULL;
40+
l->object = obj;
4141
}
4242

4343
inline
44-
Link *
44+
Link *
4545
QueueNext (Link *l)
4646
{
47-
return (l->next);
47+
return (l->next);
4848
}
4949

5050
inline
51-
Link *
51+
Link *
5252
QueuePrev (Link *l)
5353
{
54-
return (l->prev);
54+
return (l->prev);
5555
}
5656

5757
inline
58-
Link *
58+
Link *
5959
QueueFirst (Queue *q)
6060
{
61-
return (q->first);
61+
return (q->first);
6262
}
6363

6464
inline
65-
Link *
65+
Link *
6666
QueueLast (Queue *q)
6767
{
68-
return (q->last);
68+
return (q->last);
6969
}
7070

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-
}
8771

8872
inline
89-
void
73+
void
9074
QueueInsertAfter (Queue *q, Link *after, Link *l)
9175
{
92-
dbprintf('q', "Inserting link (%p) after (%p)\n",l->object,after);
93-
l->queue = q;
94-
95-
if (after) {
76+
l->queue = q;
9677
l->prev = after;
97-
dbprintf('q', "After->%p\n",after->next);
9878
l->next = after->next;
9979
after->next = l;
10080
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;
12182
}
12283

12384
inline
124-
void
85+
void
12586
QueueInsertFirst (Queue *q, Link *l)
12687
{
127-
QueueInsertAfter (q, NULL, l);
88+
QueueInsertAfter (q, (Link *)q, l);
12889
}
12990

13091
inline
131-
void
92+
void
13293
QueueInsertLast (Queue *q, Link *l)
13394
{
134-
QueueInsertAfter (q, QueueLast(q), l);
95+
QueueInsertAfter (q, QueueLast(q), l);
13596
}
13697

13798
inline
138-
void
99+
void
139100
QueueRemove (Link *l)
140101
{
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;
154106
}
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;
167108
}
168109

169110
inline
170-
int
111+
int
171112
QueueLength (Queue *q)
172113
{
173-
return (q->nitems);
114+
return (q->nitems);
174115
}
175116

176117
inline
177-
int
118+
int
178119
QueueEmpty (Queue *q)
179120
{
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);
190122
}
191123

192124
extern void QueueModuleInit ();

0 commit comments

Comments
 (0)