1 /*
2 **  GNU Pth - The GNU Portable Threads
3 **  Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 **  This file is part of GNU Pth, a non-preemptive thread scheduling
6 **  library which can be found at http://www.gnu.org/software/pth/.
7 **
8 **  This library is free software; you can redistribute it and/or
9 **  modify it under the terms of the GNU Lesser General Public
10 **  License as published by the Free Software Foundation; either
11 **  version 2.1 of the License, or (at your option) any later version.
12 **
13 **  This library is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **  Lesser General Public License for more details.
17 **
18 **  You should have received a copy of the GNU Lesser General Public
19 **  License along with this library; if not, write to the Free Software
20 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 **  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 **  pth_ring.c: Pth ring data structure
24 */
25                              /* ``Unix was not designed to stop people
26                                   from doing stupid things, because that
27                                   would also stop them from doing clever
28                                   things.''         --Doug Gwyn          */
29 
30 /*
31  * This is a "ring" data structure, a special case of a list. It is
32  * implemented through double-chained nodes. The link structure is part
33  * of the nodes, i.e. no extra memory is required for the ring itself
34  * and the ring can contain as many nodes as fit into memory. The main
35  * advantage of using a ring instead of a plain list is to make the ring
36  * operations easier (less special cases!). The ring is usually used
37  * in Pth to represent a "set" of something. All operations are O(1),
38  * except for the check whether a node is part of the ring (which is
39  * O(N)).
40  */
41 
42 #include "pth_p.h"
43 
44 /* initialize ring; O(1) */
pth_ring_init(pth_ring_t * r)45 intern void pth_ring_init(pth_ring_t *r)
46 {
47     if (r == NULL)
48         return;
49     r->r_hook  = NULL;
50     r->r_nodes = 0;
51     return;
52 }
53 
54 /* return number of nodes in ring; O(1) */
55 #if cpp
56 #define pth_ring_elements(r) \
57     ((r) == NULL ? (-1) : (r)->r_nodes)
58 #endif
59 
60 /* return first node in ring; O(1) */
61 #if cpp
62 #define pth_ring_first(r) \
63     ((r) == NULL ? NULL : (r)->r_hook)
64 #endif
65 
66 /* return last node in ring; O(1) */
67 #if cpp
68 #define pth_ring_last(r) \
69     ((r) == NULL ? NULL : ((r)->r_hook == NULL ? NULL : (r)->r_hook->rn_prev))
70 #endif
71 
72 /* walk to next node in ring; O(1) */
73 #if cpp
74 #define pth_ring_next(r, rn) \
75     (((r) == NULL || (rn) == NULL) ? NULL : ((rn)->rn_next == (r)->r_hook ? NULL : (rn)->rn_next))
76 #endif
77 
78 /* walk to previous node in ring; O(1) */
79 #if cpp
80 #define pth_ring_prev(r, rn) \
81     (((r) == NULL || (rn) == NULL) ? NULL : ((rn)->rn_prev == (r)->r_hook->rn_prev ? NULL : (rn)->rn_prev))
82 #endif
83 
84 /* insert node into ring; O(1) */
85 #if cpp
86 #define pth_ring_insert(r, rn) \
87     pth_ring_append((r), (rn))
88 #endif
89 
90 /* insert node after a second node in ring; O(1) */
pth_ring_insert_after(pth_ring_t * r,pth_ringnode_t * rn1,pth_ringnode_t * rn2)91 intern void pth_ring_insert_after(pth_ring_t *r, pth_ringnode_t *rn1, pth_ringnode_t *rn2)
92 {
93     if (r == NULL || rn1 == NULL || rn2 == NULL)
94         return;
95     rn2->rn_prev = rn1;
96     rn2->rn_next = rn1->rn_next;
97     rn2->rn_prev->rn_next = rn2;
98     rn2->rn_next->rn_prev = rn2;
99     r->r_nodes++;
100     return;
101 }
102 
103 /* insert node before a second node in ring; O(1) */
pth_ring_insert_before(pth_ring_t * r,pth_ringnode_t * rn1,pth_ringnode_t * rn2)104 intern void pth_ring_insert_before(pth_ring_t *r, pth_ringnode_t *rn1, pth_ringnode_t *rn2)
105 {
106     if (r == NULL || rn1 == NULL || rn2 == NULL)
107         return;
108     rn2->rn_next = rn1;
109     rn2->rn_prev = rn1->rn_prev;
110     rn2->rn_prev->rn_next = rn2;
111     rn2->rn_next->rn_prev = rn2;
112     r->r_nodes++;
113     return;
114 }
115 
116 /* delete an node from ring; O(1) */
pth_ring_delete(pth_ring_t * r,pth_ringnode_t * rn)117 intern void pth_ring_delete(pth_ring_t *r, pth_ringnode_t *rn)
118 {
119     if (r == NULL || rn == NULL)
120         return;
121     if (r->r_hook == rn && rn->rn_prev == rn && rn->rn_next == rn)
122         r->r_hook = NULL;
123     else {
124         if (r->r_hook == rn)
125             r->r_hook = rn->rn_next;
126         rn->rn_prev->rn_next = rn->rn_next;
127         rn->rn_next->rn_prev = rn->rn_prev;
128     }
129     r->r_nodes--;
130     return;
131 }
132 
133 /* prepend an node to ring; O(1) */
pth_ring_prepend(pth_ring_t * r,pth_ringnode_t * rn)134 intern void pth_ring_prepend(pth_ring_t *r, pth_ringnode_t *rn)
135 {
136     if (r == NULL || rn == NULL)
137         return;
138     if (r->r_hook == NULL) {
139         r->r_hook = rn;
140         rn->rn_next = rn;
141         rn->rn_prev = rn;
142     }
143     else {
144         rn->rn_next = r->r_hook;
145         rn->rn_prev = r->r_hook->rn_prev;
146         rn->rn_next->rn_prev = rn;
147         rn->rn_prev->rn_next = rn;
148         r->r_hook = rn;
149     }
150     r->r_nodes++;
151     return;
152 }
153 
154 /* append an node to ring; O(1) */
pth_ring_append(pth_ring_t * r,pth_ringnode_t * rn)155 intern void pth_ring_append(pth_ring_t *r, pth_ringnode_t *rn)
156 {
157     if (r == NULL || rn == NULL)
158         return;
159     if (r->r_hook == NULL) {
160         r->r_hook = rn;
161         rn->rn_next = rn;
162         rn->rn_prev = rn;
163     }
164     else {
165         rn->rn_next = r->r_hook;
166         rn->rn_prev = r->r_hook->rn_prev;
167         rn->rn_next->rn_prev = rn;
168         rn->rn_prev->rn_next = rn;
169     }
170     r->r_nodes++;
171     return;
172 }
173 
174 /* treat ring as stack: push node onto stack; O(1) */
175 #if cpp
176 #define pth_ring_push(r, rn) \
177     pth_ring_prepend((r), (rn))
178 #endif
179 
180 /* treat ring as stack: pop node from stack; O(1) */
pth_ring_pop(pth_ring_t * r)181 intern pth_ringnode_t *pth_ring_pop(pth_ring_t *r)
182 {
183     pth_ringnode_t *rn;
184 
185     rn = pth_ring_first(r);
186     if (rn != NULL)
187         pth_ring_delete(r, rn);
188     return rn;
189 }
190 
191 /* treat ring as queue: favorite a node in the ring; O(1) */
pth_ring_favorite(pth_ring_t * r,pth_ringnode_t * rn)192 intern int pth_ring_favorite(pth_ring_t *r, pth_ringnode_t *rn)
193 {
194     if (r == NULL)
195         return FALSE;
196     if (r->r_hook == NULL)
197         return FALSE;
198     /* element is perhaps already at ring hook */
199     if (r->r_hook == rn)
200         return TRUE;
201     /* move to hook of ring */
202     pth_ring_delete(r, rn);
203     pth_ring_prepend(r, rn);
204     return TRUE;
205 }
206 
207 /* treat ring as queue: enqueue node; O(1) */
208 #if cpp
209 #define pth_ring_enqueue(r, rn) \
210     pth_ring_prepend((r), (rn))
211 #endif
212 
213 /* treat ring as queue: dequeue node; O(1) */
pth_ring_dequeue(pth_ring_t * r)214 intern pth_ringnode_t *pth_ring_dequeue(pth_ring_t *r)
215 {
216     pth_ringnode_t *rn;
217 
218     rn = pth_ring_last(r);
219     if (rn != NULL)
220         pth_ring_delete(r, rn);
221     return rn;
222 }
223 
224 /* check whether node is contained in ring; O(n) */
pth_ring_contains(pth_ring_t * r,pth_ringnode_t * rns)225 intern int pth_ring_contains(pth_ring_t *r, pth_ringnode_t *rns)
226 {
227     pth_ringnode_t *rn;
228     int rc;
229 
230     if (r == NULL || rns == NULL)
231         return pth_error(FALSE, EINVAL);
232     rc = FALSE;
233     rn = r->r_hook;
234     if (rn != NULL) {
235         do {
236             if (rn == rns) {
237                 rc = TRUE;
238                 break;
239             }
240             rn = rn->rn_next;
241         } while (rn != r->r_hook);
242     }
243     return rc;
244 }
245 
246