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
39 /* Pointer Double Ended Queue */
40 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
41 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
43 /** Size of pdeq block cache. */
44 #define TUNE_NSAVED_PDEQS 16
47 * Maximal number of data items in a pdeq chunk.
49 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
52 # define VRFY(dq) ((void)0)
54 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
58 * A pointer double ended queue.
59 * This structure is used as a list chunk either.
63 unsigned magic; /**< debug magic */
65 pdeq *l_end, *r_end; /**< left and right ends of the queue */
66 pdeq *l, *r; /**< left and right neighbor */
67 int n; /**< number of elements in the current chunk */
68 int p; /**< the read/write pointer */
69 const void *data[1]; /**< storage for elements */
74 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
75 * +1 for compilers that can't grok empty arrays
77 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
80 * Number of pdeqs in pdeq_store.
82 static unsigned pdeqs_cached;
85 * Free a pdeq chunk, put in into the cache if possible.
87 * @param p The pdeq chunk.
89 static inline void free_pdeq_block (pdeq *p)
92 p->magic = 0xbadf00d1;
94 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
95 pdeq_block_cache[pdeqs_cached++] = p;
102 * Allocate a new pdeq chunk, get it from the cache if possible.
104 * @return A new pdeq chunk.
106 static inline pdeq *alloc_pdeq_block (void)
109 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
110 p = pdeq_block_cache[--pdeqs_cached];
112 p = xmalloc(PREF_MALLOC_SIZE);
120 * Verify a double ended list, assert if failure.
122 * @param dq The list to verify.
124 void _pdeq_vrfy(pdeq *dq)
129 && (dq->magic == PDEQ_MAGIC1)
130 && (dq->l_end && dq->r_end));
133 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
134 && ((q == dq->l_end) ^ (q->l != NULL))
135 && ((q == dq->r_end) ^ (q->r != NULL))
136 && (!q->l || (q == q->l->r))
137 && ((q->n >= 0) && (q->n <= NDATA))
138 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
139 && ((q->p >= 0) && (q->p < NDATA)));
145 /* Creates a new double ended pointer list. */
150 dq = alloc_pdeq_block();
153 dq->magic = PDEQ_MAGIC1;
155 dq->l_end = dq->r_end = dq;
156 dq->l = dq->r = NULL;
163 /* Creates a new double ended pointer list and puts an initial pointer element in. */
164 pdeq *new_pdeq1(const void *x)
166 return pdeq_putr(new_pdeq(), x);
169 /* Delete a double ended pointer list. */
170 void del_pdeq(pdeq *dq)
176 q = dq->l_end; /* left end of chain */
177 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
178 if (dq->n == 0 && dq->l_end != dq ) {
182 /* Free all blocks in the pdeq chain */
190 /* Checks if a list is empty. */
191 int pdeq_empty(pdeq *dq)
194 return dq->l_end->n == 0;
197 /* Returns the length of a double ended pointer list. */
198 int pdeq_len(pdeq *dq)
215 /* Add a pointer to the right site of a double ended pointer list. */
216 pdeq *pdeq_putr(pdeq *dq, const void *x)
224 if (rdq->n >= NDATA) { /* tailblock full */
227 ndq = dq; /* try to reuse trunk, but ... */
228 if (dq->n) { /* ... if trunk used */
229 /* allocate and init new block */
230 ndq = alloc_pdeq_block();
232 ndq->magic = PDEQ_MAGIC2;
234 ndq->l_end = ndq->r_end = NULL;
238 ndq->l = rdq; rdq->r = ndq;
239 ndq->n = 0; ndq->p = 0;
244 n = rdq->n++ + rdq->p;
245 if (n >= NDATA) n -= NDATA;
253 /* Add a pointer to the left site of a double ended pointer list. */
254 pdeq *pdeq_putl(pdeq *dq, const void *x)
262 if (ldq->n >= NDATA) { /* headblock full */
265 ndq = dq; /* try to reuse trunk, but ... */
266 if (dq->n) { /* ... if trunk used */
267 /* allocate and init new block */
268 ndq = alloc_pdeq_block();
270 ndq->magic = PDEQ_MAGIC2;
272 ndq->l_end = ndq->r_end = NULL;
276 ndq->r = ldq; ldq->l = ndq;
277 ndq->n = 0; ndq->p = 0;
284 if (p < 0) p += NDATA;
293 /* Retrieve a pointer from the right site of a double ended pointer list. */
294 void *pdeq_getr(pdeq *dq)
301 assert(dq->l_end->n);
304 n = rdq->p + --rdq->n;
305 if (n >= NDATA) n -= NDATA;
314 dq->r_end = dq->l_end = dq;
317 free_pdeq_block(rdq);
325 /* Retrieve a pointer from the left site of a double ended pointer list. */
326 void *pdeq_getl(pdeq *dq)
333 assert(dq->l_end->n);
338 if (++p >= NDATA) p = 0;
347 dq->l_end = dq->r_end = dq;
350 free_pdeq_block(ldq);
359 * Returns non-zero if a double ended pointer list
360 * contains a pointer x.
362 int pdeq_contains(pdeq *dq, const void *x)
372 p = q->p; ep = p + q->n;
376 if (q->data[p] == x) return 1;
377 } while (++p < NDATA);
383 if (q->data[p++] == x) return 1;
393 * Search a key in a double ended pointer list, the search
394 * is controlled by a compare function.
395 * An element is found, if the compare function returns 0.
396 * The search is started from the left site of the list.
398 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
409 p = q->p; ep = p + q->n;
413 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
414 } while (++p < NDATA);
420 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
430 * Convert the double ended pointer list into a linear array beginning from
431 * left, the first element in the linear array will be the left one.
433 void **pdeq_copyl(pdeq *dq, const void **dst)
436 const void **d = dst;
448 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
452 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
461 * Convert the double ended pointer list into a linear array beginning from
462 * right, the first element in the linear array will be the right one.
464 void **pdeq_copyr(pdeq *dq, const void **dst)
467 const void **d = dst;
475 p = q->p; i = q->n + p - 1;
478 do *d++ = q->data[i]; while (--i >= 0);
482 do *d++ = q->data[i]; while (--i >= p);