2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief double ended queue of generic pointers.
23 * @author Christian von Roques
24 * @date 1999 by getting from fiasco
48 /* Pointer Double Ended Queue */
49 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
50 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
52 /** Size of pdeq block cache. */
53 #define TUNE_NSAVED_PDEQS 16
56 * Maximal number of data items in a pdeq chunk.
58 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
61 # define VRFY(dq) ((void)0)
63 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
67 * A pointer double ended queue.
68 * This structure is used as a list chunk either.
72 unsigned magic; /**< debug magic */
74 pdeq *l_end, *r_end; /**< left and right ends of the queue */
75 pdeq *l, *r; /**< left and right neighbor */
76 int n; /**< number of elements in the current chunk */
77 int p; /**< the read/write pointer */
78 const void *data[1]; /**< storage for elements */
83 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
84 * +1 for compilers that can't grok empty arrays
86 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
89 * Number of pdeqs in pdeq_store.
91 static unsigned pdeqs_cached;
94 * Free a pdeq chunk, put in into the cache if possible.
96 * @param p The pdeq chunk.
98 static INLINE void free_pdeq_block (pdeq *p)
101 p->magic = 0xbadf00d1;
103 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
104 pdeq_block_cache[pdeqs_cached++] = p;
111 * Allocate a new pdeq chunk, get it from the cache if possible.
113 * @return A new pdeq chunk.
115 static INLINE pdeq *alloc_pdeq_block (void)
118 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
119 p = pdeq_block_cache[--pdeqs_cached];
121 p = xmalloc(PREF_MALLOC_SIZE);
129 * Verify a double ended list, assert if failure.
131 * @param dq The list to verify.
133 void _pdeq_vrfy(pdeq *dq)
138 && (dq->magic == PDEQ_MAGIC1)
139 && (dq->l_end && dq->r_end));
142 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
143 && ((q == dq->l_end) ^ (q->l != NULL))
144 && ((q == dq->r_end) ^ (q->r != NULL))
145 && (!q->l || (q == q->l->r))
146 && ((q->n >= 0) && (q->n <= NDATA))
147 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
148 && ((q->p >= 0) && (q->p < NDATA)));
154 /* Creates a new double ended pointer list. */
159 dq = alloc_pdeq_block();
162 dq->magic = PDEQ_MAGIC1;
164 dq->l_end = dq->r_end = dq;
165 dq->l = dq->r = NULL;
172 /* Creates a new double ended pointer list and puts an initial pointer element in. */
173 pdeq *new_pdeq1(const void *x)
175 return pdeq_putr(new_pdeq(), x);
178 /* Delete a double ended pointer list. */
179 void del_pdeq(pdeq *dq)
185 q = dq->l_end; /* left end of chain */
186 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
187 if (dq->n == 0 && dq->l_end != dq ) {
191 /* Free all blocks in the pdeq chain */
199 /* Checks if a list is empty. */
200 int pdeq_empty(pdeq *dq)
203 return dq->l_end->n == 0;
206 /* Returns the length of a double ended pointer list. */
207 int pdeq_len(pdeq *dq)
224 /* Add a pointer to the right site of a double ended pointer list. */
225 pdeq *pdeq_putr(pdeq *dq, const void *x)
233 if (rdq->n >= NDATA) { /* tailblock full */
236 ndq = dq; /* try to reuse trunk, but ... */
237 if (dq->n) { /* ... if trunk used */
238 /* allocate and init new block */
239 ndq = alloc_pdeq_block();
241 ndq->magic = PDEQ_MAGIC2;
243 ndq->l_end = ndq->r_end = NULL;
247 ndq->l = rdq; rdq->r = ndq;
248 ndq->n = 0; ndq->p = 0;
253 n = rdq->n++ + rdq->p;
254 if (n >= NDATA) n -= NDATA;
262 /* Add a pointer to the left site of a double ended pointer list. */
263 pdeq *pdeq_putl(pdeq *dq, const void *x)
271 if (ldq->n >= NDATA) { /* headblock full */
274 ndq = dq; /* try to reuse trunk, but ... */
275 if (dq->n) { /* ... if trunk used */
276 /* allocate and init new block */
277 ndq = alloc_pdeq_block();
279 ndq->magic = PDEQ_MAGIC2;
281 ndq->l_end = ndq->r_end = NULL;
285 ndq->r = ldq; ldq->l = ndq;
286 ndq->n = 0; ndq->p = 0;
293 if (p < 0) p += NDATA;
302 /* Retrieve a pointer from the right site of a double ended pointer list. */
303 void *pdeq_getr(pdeq *dq)
310 assert(dq->l_end->n);
313 n = rdq->p + --rdq->n;
314 if (n >= NDATA) n -= NDATA;
323 dq->r_end = dq->l_end = dq;
326 free_pdeq_block(rdq);
334 /* Retrieve a pointer from the left site of a double ended pointer list. */
335 void *pdeq_getl(pdeq *dq)
342 assert(dq->l_end->n);
347 if (++p >= NDATA) p = 0;
356 dq->l_end = dq->r_end = dq;
359 free_pdeq_block(ldq);
368 * Returns non-zero if a double ended pointer list
369 * contains a pointer x.
371 int pdeq_contains(pdeq *dq, const void *x)
381 p = q->p; ep = p + q->n;
385 if (q->data[p] == x) return 1;
386 } while (++p < NDATA);
392 if (q->data[p++] == x) return 1;
402 * Search a key in a double ended pointer list, the search
403 * is controlled by a compare function.
404 * An element is found, if the compare function returns 0.
405 * The search is started from the left site of the list.
407 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
418 p = q->p; ep = p + q->n;
422 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
423 } while (++p < NDATA);
429 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
439 * Convert the double ended pointer list into a linear array beginning from
440 * left, the first element in the linear array will be the left one.
442 void **pdeq_copyl(pdeq *dq, const void **dst)
445 const void **d = dst;
457 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
461 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
470 * Convert the double ended pointer list into a linear array beginning from
471 * right, the first element in the linear array will be the right one.
473 void **pdeq_copyr(pdeq *dq, const void **dst)
476 const void **d = dst;
484 p = q->p; i = q->n + p - 1;
487 do *d++ = q->data[i]; while (--i >= 0);
491 do *d++ = q->data[i]; while (--i >= p);