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
37 /* Pointer Double Ended Queue */
38 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
39 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
41 /** Size of pdeq block cache. */
42 #define TUNE_NSAVED_PDEQS 16
44 /** A size handled efficiently by malloc(), at least 1K. */
45 #define PREF_MALLOC_SIZE 2048
48 * Maximal number of data items in a pdeq chunk.
50 #define NDATA ((PREF_MALLOC_SIZE - offsetof(pdeq, data)) / sizeof(void *))
53 # define VRFY(dq) ((void)0)
55 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
59 * A pointer double ended queue.
60 * This structure is used as a list chunk either.
64 unsigned magic; /**< debug magic */
66 pdeq *l_end, *r_end; /**< left and right ends of the queue */
67 pdeq *l, *r; /**< left and right neighbor */
68 size_t n; /**< number of elements in the current chunk */
69 size_t p; /**< the read/write pointer */
70 const void *data[1]; /**< storage for elements */
75 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
77 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS];
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 (pdeqs_cached > 0) {
110 p = pdeq_block_cache[--pdeqs_cached];
112 p = (pdeq*) xmalloc(PREF_MALLOC_SIZE);
117 /* Creates a new double ended pointer list. */
122 dq = alloc_pdeq_block();
125 dq->magic = PDEQ_MAGIC1;
127 dq->l_end = dq->r_end = dq;
128 dq->l = dq->r = NULL;
135 /* Creates a new double ended pointer list and puts an initial pointer element in. */
136 pdeq *new_pdeq1(const void *x)
138 return pdeq_putr(new_pdeq(), x);
141 /* Delete a double ended pointer list. */
142 void del_pdeq(pdeq *dq)
148 q = dq->l_end; /* left end of chain */
149 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
150 if (dq->n == 0 && dq->l_end != dq ) {
154 /* Free all blocks in the pdeq chain */
161 /* Checks if a list is empty. */
162 int pdeq_empty(pdeq *dq)
165 return dq->l_end->n == 0;
168 /* Returns the length of a double ended pointer list. */
169 size_t pdeq_len(pdeq *dq)
186 /* Add a pointer to the right site of a double ended pointer list. */
187 pdeq *pdeq_putr(pdeq *dq, const void *x)
195 if (rdq->n >= NDATA) { /* tailblock full */
198 ndq = dq; /* try to reuse trunk, but ... */
199 if (dq->n) { /* ... if trunk used */
200 /* allocate and init new block */
201 ndq = alloc_pdeq_block();
203 ndq->magic = PDEQ_MAGIC2;
205 ndq->l_end = ndq->r_end = NULL;
209 ndq->l = rdq; rdq->r = ndq;
210 ndq->n = 0; ndq->p = 0;
215 n = rdq->n++ + rdq->p;
216 if (n >= NDATA) n -= NDATA;
224 /* Add a pointer to the left site of a double ended pointer list. */
225 pdeq *pdeq_putl(pdeq *dq, const void *x)
233 if (ldq->n >= NDATA) { /* headblock 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->r = ldq; ldq->l = ndq;
248 ndq->n = 0; ndq->p = 0;
266 /* Retrieve a pointer from the right site of a double ended pointer list. */
267 void *pdeq_getr(pdeq *dq)
274 assert(dq->l_end->n);
277 n = rdq->p + --rdq->n;
278 if (n >= NDATA) n -= NDATA;
287 dq->r_end = dq->l_end = dq;
290 free_pdeq_block(rdq);
298 /* Retrieve a pointer from the left site of a double ended pointer list. */
299 void *pdeq_getl(pdeq *dq)
306 assert(dq->l_end->n);
311 if (++p >= NDATA) p = 0;
320 dq->l_end = dq->r_end = dq;
323 free_pdeq_block(ldq);
332 * Returns non-zero if a double ended pointer list
333 * contains a pointer x.
335 int pdeq_contains(pdeq *dq, const void *x)
345 p = q->p; ep = p + q->n;
349 if (q->data[p] == x) return 1;
350 } while (++p < NDATA);
356 if (q->data[p++] == x) return 1;
366 * Search a key in a double ended pointer list, the search
367 * is controlled by a compare function.
368 * An element is found, if the compare function returns 0.
369 * The search is started from the left site of the list.
371 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
382 p = q->p; ep = p + q->n;
386 if (!cmp(q->data[p], key)) return (void *)q->data[p-1];
387 } while (++p < NDATA);
393 if (!cmp(q->data[p++], key)) return (void *)q->data[p-1];
403 * Convert the double ended pointer list into a linear array beginning from
404 * left, the first element in the linear array will be the left one.
406 void **pdeq_copyl(pdeq *dq, const void **dst)
409 const void **d = dst;
420 /* p is always < NDATA */
421 size_t nn = NDATA - p;
422 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
426 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
435 * Convert the double ended pointer list into a linear array beginning from
436 * right, the first element in the linear array will be the right one.
438 void **pdeq_copyr(pdeq *dq, const void **dst)
441 const void **d = dst;
449 p = q->p; i = q->n + p - 1;