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
31 #include "firm_config.h"
50 /* Pointer Double Ended Queue */
51 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
52 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
54 /** Size of pdeq block cache. */
55 #define TUNE_NSAVED_PDEQS 16
58 * Maximal number of data items in a pdeq chunk.
60 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
63 # define VRFY(dq) ((void)0)
65 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
69 * A pointer double ended queue.
70 * This structure is used as a list chunk either.
74 unsigned magic; /**< debug magic */
76 pdeq *l_end, *r_end; /**< left and right ends of the queue */
77 pdeq *l, *r; /**< left and right neighbor */
78 int n; /**< number of elements in the current chunk */
79 int p; /**< the read/write pointer */
80 const void *data[1]; /**< storage for elements */
85 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
86 * +1 for compilers that can't grok empty arrays
88 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
91 * Number of pdeqs in pdeq_store.
93 static unsigned pdeqs_cached;
96 * Free a pdeq chunk, put in into the cache if possible.
98 * @param p The pdeq chunk.
100 static INLINE void free_pdeq_block (pdeq *p)
103 p->magic = 0xbadf00d1;
105 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
106 pdeq_block_cache[pdeqs_cached++] = p;
113 * Allocate a new pdeq chunk, get it from the cache if possible.
115 * @return A new pdeq chunk.
117 static INLINE pdeq *alloc_pdeq_block (void)
120 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
121 p = pdeq_block_cache[--pdeqs_cached];
123 p = xmalloc(PREF_MALLOC_SIZE);
131 * Verify a double ended list, assert if failure.
133 * @param dq The list to verify.
135 void _pdeq_vrfy(pdeq *dq)
140 && (dq->magic == PDEQ_MAGIC1)
141 && (dq->l_end && dq->r_end));
144 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
145 && ((q == dq->l_end) ^ (q->l != NULL))
146 && ((q == dq->r_end) ^ (q->r != NULL))
147 && (!q->l || (q == q->l->r))
148 && ((q->n >= 0) && (q->n <= NDATA))
149 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
150 && ((q->p >= 0) && (q->p < NDATA)));
156 /* Creates a new double ended pointer list. */
161 dq = alloc_pdeq_block();
164 dq->magic = PDEQ_MAGIC1;
166 dq->l_end = dq->r_end = dq;
167 dq->l = dq->r = NULL;
174 /* Creates a new double ended pointer list and puts an initial pointer element in. */
175 pdeq *new_pdeq1(const void *x)
177 return pdeq_putr(new_pdeq(), x);
180 /* Delete a double ended pointer list. */
181 void del_pdeq(pdeq *dq)
187 q = dq->l_end; /* left end of chain */
188 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
189 if (dq->n == 0 && dq->l_end != dq ) {
193 /* Free all blocks in the pdeq chain */
201 /* Checks if a list is empty. */
202 int pdeq_empty(pdeq *dq)
205 return dq->l_end->n == 0;
208 /* Returns the length of a double ended pointer list. */
209 int pdeq_len(pdeq *dq)
226 /* Add a pointer to the right site of a double ended pointer list. */
227 pdeq *pdeq_putr(pdeq *dq, const void *x)
235 if (rdq->n >= NDATA) { /* tailblock full */
238 ndq = dq; /* try to reuse trunk, but ... */
239 if (dq->n) { /* ... if trunk used */
240 /* allocate and init new block */
241 ndq = alloc_pdeq_block();
243 ndq->magic = PDEQ_MAGIC2;
245 ndq->l_end = ndq->r_end = NULL;
249 ndq->l = rdq; rdq->r = ndq;
250 ndq->n = 0; ndq->p = 0;
255 n = rdq->n++ + rdq->p;
256 if (n >= NDATA) n -= NDATA;
264 /* Add a pointer to the left site of a double ended pointer list. */
265 pdeq *pdeq_putl(pdeq *dq, const void *x)
273 if (ldq->n >= NDATA) { /* headblock full */
276 ndq = dq; /* try to reuse trunk, but ... */
277 if (dq->n) { /* ... if trunk used */
278 /* allocate and init new block */
279 ndq = alloc_pdeq_block();
281 ndq->magic = PDEQ_MAGIC2;
283 ndq->l_end = ndq->r_end = NULL;
287 ndq->r = ldq; ldq->l = ndq;
288 ndq->n = 0; ndq->p = 0;
295 if (p < 0) p += NDATA;
304 /* Retrieve a pointer from the right site of a double ended pointer list. */
305 void *pdeq_getr(pdeq *dq)
312 assert(dq->l_end->n);
315 n = rdq->p + --rdq->n;
316 if (n >= NDATA) n -= NDATA;
325 dq->r_end = dq->l_end = dq;
328 free_pdeq_block(rdq);
336 /* Retrieve a pointer from the left site of a double ended pointer list. */
337 void *pdeq_getl(pdeq *dq)
344 assert(dq->l_end->n);
349 if (++p >= NDATA) p = 0;
358 dq->l_end = dq->r_end = dq;
361 free_pdeq_block(ldq);
370 * Returns non-zero if a double ended pointer list
371 * contains a pointer x.
373 int pdeq_contains(pdeq *dq, const void *x)
383 p = q->p; ep = p + q->n;
387 if (q->data[p] == x) return 1;
388 } while (++p < NDATA);
394 if (q->data[p++] == x) return 1;
404 * Search a key in a double ended pointer list, the search
405 * is controlled by a compare function.
406 * An element is found, if the compare function returns 0.
407 * The search is started from the left site of the list.
409 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
420 p = q->p; ep = p + q->n;
424 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
425 } while (++p < NDATA);
431 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
441 * Convert the double ended pointer list into a linear array beginning from
442 * left, the first element in the linear array will be the left one.
444 void **pdeq_copyl(pdeq *dq, const void **dst)
447 const void **d = dst;
459 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
463 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
472 * Convert the double ended pointer list into a linear array beginning from
473 * right, the first element in the linear array will be the right one.
475 void **pdeq_copyr(pdeq *dq, const void **dst)
478 const void **d = dst;
486 p = q->p; i = q->n + p - 1;
489 do *d++ = q->data[i]; while (--i >= 0);
493 do *d++ = q->data[i]; while (--i >= p);