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
38 /* Pointer Double Ended Queue */
39 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
40 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
42 /** Size of pdeq block cache. */
43 #define TUNE_NSAVED_PDEQS 16
45 /** A size handled efficiently by malloc(), at least 1K. */
46 #define PREF_MALLOC_SIZE 2048
49 * Maximal number of data items in a pdeq chunk.
51 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
54 # define VRFY(dq) ((void)0)
56 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
60 * A pointer double ended queue.
61 * This structure is used as a list chunk either.
65 unsigned magic; /**< debug magic */
67 pdeq *l_end, *r_end; /**< left and right ends of the queue */
68 pdeq *l, *r; /**< left and right neighbor */
69 int n; /**< number of elements in the current chunk */
70 int p; /**< the read/write pointer */
71 const void *data[1]; /**< storage for elements */
76 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
77 * +1 for compilers that can't grok empty arrays
79 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
82 * Number of pdeqs in pdeq_store.
84 static unsigned pdeqs_cached;
87 * Free a pdeq chunk, put in into the cache if possible.
89 * @param p The pdeq chunk.
91 static inline void free_pdeq_block (pdeq *p)
94 p->magic = 0xbadf00d1;
96 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
97 pdeq_block_cache[pdeqs_cached++] = p;
104 * Allocate a new pdeq chunk, get it from the cache if possible.
106 * @return A new pdeq chunk.
108 static inline pdeq *alloc_pdeq_block (void)
111 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
112 p = pdeq_block_cache[--pdeqs_cached];
114 p = xmalloc(PREF_MALLOC_SIZE);
122 * Verify a double ended list, assert if failure.
124 * @param dq The list to verify.
126 void _pdeq_vrfy(pdeq *dq)
131 && (dq->magic == PDEQ_MAGIC1)
132 && (dq->l_end && dq->r_end));
135 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
136 && ((q == dq->l_end) ^ (q->l != NULL))
137 && ((q == dq->r_end) ^ (q->r != NULL))
138 && (!q->l || (q == q->l->r))
139 && ((q->n >= 0) && (q->n <= NDATA))
140 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
141 && ((q->p >= 0) && (q->p < NDATA)));
147 /* Creates a new double ended pointer list. */
152 dq = alloc_pdeq_block();
155 dq->magic = PDEQ_MAGIC1;
157 dq->l_end = dq->r_end = dq;
158 dq->l = dq->r = NULL;
165 /* Creates a new double ended pointer list and puts an initial pointer element in. */
166 pdeq *new_pdeq1(const void *x)
168 return pdeq_putr(new_pdeq(), x);
171 /* Delete a double ended pointer list. */
172 void del_pdeq(pdeq *dq)
178 q = dq->l_end; /* left end of chain */
179 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
180 if (dq->n == 0 && dq->l_end != dq ) {
184 /* Free all blocks in the pdeq chain */
192 /* Checks if a list is empty. */
193 int pdeq_empty(pdeq *dq)
196 return dq->l_end->n == 0;
199 /* Returns the length of a double ended pointer list. */
200 int pdeq_len(pdeq *dq)
217 /* Add a pointer to the right site of a double ended pointer list. */
218 pdeq *pdeq_putr(pdeq *dq, const void *x)
226 if (rdq->n >= NDATA) { /* tailblock full */
229 ndq = dq; /* try to reuse trunk, but ... */
230 if (dq->n) { /* ... if trunk used */
231 /* allocate and init new block */
232 ndq = alloc_pdeq_block();
234 ndq->magic = PDEQ_MAGIC2;
236 ndq->l_end = ndq->r_end = NULL;
240 ndq->l = rdq; rdq->r = ndq;
241 ndq->n = 0; ndq->p = 0;
246 n = rdq->n++ + rdq->p;
247 if (n >= NDATA) n -= NDATA;
255 /* Add a pointer to the left site of a double ended pointer list. */
256 pdeq *pdeq_putl(pdeq *dq, const void *x)
264 if (ldq->n >= NDATA) { /* headblock full */
267 ndq = dq; /* try to reuse trunk, but ... */
268 if (dq->n) { /* ... if trunk used */
269 /* allocate and init new block */
270 ndq = alloc_pdeq_block();
272 ndq->magic = PDEQ_MAGIC2;
274 ndq->l_end = ndq->r_end = NULL;
278 ndq->r = ldq; ldq->l = ndq;
279 ndq->n = 0; ndq->p = 0;
286 if (p < 0) p += NDATA;
295 /* Retrieve a pointer from the right site of a double ended pointer list. */
296 void *pdeq_getr(pdeq *dq)
303 assert(dq->l_end->n);
306 n = rdq->p + --rdq->n;
307 if (n >= NDATA) n -= NDATA;
316 dq->r_end = dq->l_end = dq;
319 free_pdeq_block(rdq);
327 /* Retrieve a pointer from the left site of a double ended pointer list. */
328 void *pdeq_getl(pdeq *dq)
335 assert(dq->l_end->n);
340 if (++p >= NDATA) p = 0;
349 dq->l_end = dq->r_end = dq;
352 free_pdeq_block(ldq);
361 * Returns non-zero if a double ended pointer list
362 * contains a pointer x.
364 int pdeq_contains(pdeq *dq, const void *x)
374 p = q->p; ep = p + q->n;
378 if (q->data[p] == x) return 1;
379 } while (++p < NDATA);
385 if (q->data[p++] == x) return 1;
395 * Search a key in a double ended pointer list, the search
396 * is controlled by a compare function.
397 * An element is found, if the compare function returns 0.
398 * The search is started from the left site of the list.
400 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
411 p = q->p; ep = p + q->n;
415 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
416 } while (++p < NDATA);
422 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
432 * Convert the double ended pointer list into a linear array beginning from
433 * left, the first element in the linear array will be the left one.
435 void **pdeq_copyl(pdeq *dq, const void **dst)
438 const void **d = dst;
450 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
454 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
463 * Convert the double ended pointer list into a linear array beginning from
464 * right, the first element in the linear array will be the right one.
466 void **pdeq_copyr(pdeq *dq, const void **dst)
469 const void **d = dst;
477 p = q->p; i = q->n + p - 1;
480 do *d++ = q->data[i]; while (--i >= 0);
484 do *d++ = q->data[i]; while (--i >= p);