2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief double ended queue of generic pointers.
9 * @author Christian von Roques
10 * @date 1999 by getting from fiasco
23 /* Pointer Double Ended Queue */
24 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
25 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
27 /** Size of pdeq block cache. */
28 #define TUNE_NSAVED_PDEQS 16
30 /** A size handled efficiently by malloc(), at least 1K. */
31 #define PREF_MALLOC_SIZE 2048
34 * Maximal number of data items in a pdeq chunk.
36 #define NDATA ((PREF_MALLOC_SIZE - offsetof(pdeq, data)) / sizeof(void *))
39 # define VRFY(dq) ((void)0)
41 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
45 * A pointer double ended queue.
46 * This structure is used as a list chunk either.
50 unsigned magic; /**< debug magic */
52 pdeq *l_end, *r_end; /**< left and right ends of the queue */
53 pdeq *l, *r; /**< left and right neighbor */
54 size_t n; /**< number of elements in the current chunk */
55 size_t p; /**< the read/write pointer */
56 const void *data[1]; /**< storage for elements */
61 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
63 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS];
66 * Number of pdeqs in pdeq_store.
68 static unsigned pdeqs_cached;
71 * Free a pdeq chunk, put in into the cache if possible.
73 * @param p The pdeq chunk.
75 static inline void free_pdeq_block (pdeq *p)
78 p->magic = 0xbadf00d1;
80 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
81 pdeq_block_cache[pdeqs_cached++] = p;
88 * Allocate a new pdeq chunk, get it from the cache if possible.
90 * @return A new pdeq chunk.
92 static inline pdeq *alloc_pdeq_block (void)
95 if (pdeqs_cached > 0) {
96 p = pdeq_block_cache[--pdeqs_cached];
98 p = (pdeq*) xmalloc(PREF_MALLOC_SIZE);
103 /* Creates a new double ended pointer list. */
108 dq = alloc_pdeq_block();
111 dq->magic = PDEQ_MAGIC1;
113 dq->l_end = dq->r_end = dq;
114 dq->l = dq->r = NULL;
121 /* Creates a new double ended pointer list and puts an initial pointer element in. */
122 pdeq *new_pdeq1(const void *x)
124 return pdeq_putr(new_pdeq(), x);
127 /* Delete a double ended pointer list. */
128 void del_pdeq(pdeq *dq)
134 q = dq->l_end; /* left end of chain */
135 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
136 if (dq->n == 0 && dq->l_end != dq ) {
140 /* Free all blocks in the pdeq chain */
147 /* Checks if a list is empty. */
148 int pdeq_empty(pdeq *dq)
151 return dq->l_end->n == 0;
154 /* Returns the length of a double ended pointer list. */
155 size_t pdeq_len(pdeq *dq)
172 /* Add a pointer to the right site of a double ended pointer list. */
173 pdeq *pdeq_putr(pdeq *dq, const void *x)
181 if (rdq->n >= NDATA) { /* tailblock full */
184 ndq = dq; /* try to reuse trunk, but ... */
185 if (dq->n) { /* ... if trunk used */
186 /* allocate and init new block */
187 ndq = alloc_pdeq_block();
189 ndq->magic = PDEQ_MAGIC2;
191 ndq->l_end = ndq->r_end = NULL;
195 ndq->l = rdq; rdq->r = ndq;
196 ndq->n = 0; ndq->p = 0;
201 n = rdq->n++ + rdq->p;
202 if (n >= NDATA) n -= NDATA;
210 /* Add a pointer to the left site of a double ended pointer list. */
211 pdeq *pdeq_putl(pdeq *dq, const void *x)
219 if (ldq->n >= NDATA) { /* headblock full */
222 ndq = dq; /* try to reuse trunk, but ... */
223 if (dq->n) { /* ... if trunk used */
224 /* allocate and init new block */
225 ndq = alloc_pdeq_block();
227 ndq->magic = PDEQ_MAGIC2;
229 ndq->l_end = ndq->r_end = NULL;
233 ndq->r = ldq; ldq->l = ndq;
234 ndq->n = 0; ndq->p = 0;
252 /* Retrieve a pointer from the right site of a double ended pointer list. */
253 void *pdeq_getr(pdeq *dq)
260 assert(dq->l_end->n);
263 n = rdq->p + --rdq->n;
264 if (n >= NDATA) n -= NDATA;
273 dq->r_end = dq->l_end = dq;
276 free_pdeq_block(rdq);
284 /* Retrieve a pointer from the left site of a double ended pointer list. */
285 void *pdeq_getl(pdeq *dq)
292 assert(dq->l_end->n);
297 if (++p >= NDATA) p = 0;
306 dq->l_end = dq->r_end = dq;
309 free_pdeq_block(ldq);
318 * Returns non-zero if a double ended pointer list
319 * contains a pointer x.
321 int pdeq_contains(pdeq *dq, const void *x)
331 p = q->p; ep = p + q->n;
335 if (q->data[p] == x) return 1;
336 } while (++p < NDATA);
342 if (q->data[p++] == x) return 1;
352 * Search a key in a double ended pointer list, the search
353 * is controlled by a compare function.
354 * An element is found, if the compare function returns 0.
355 * The search is started from the left site of the list.
357 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
368 p = q->p; ep = p + q->n;
372 if (!cmp(q->data[p], key)) return (void *)q->data[p-1];
373 } while (++p < NDATA);
379 if (!cmp(q->data[p++], key)) return (void *)q->data[p-1];
389 * Convert the double ended pointer list into a linear array beginning from
390 * left, the first element in the linear array will be the left one.
392 void **pdeq_copyl(pdeq *dq, const void **dst)
395 const void **d = dst;
406 /* p is always < NDATA */
407 size_t nn = NDATA - p;
408 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
412 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
421 * Convert the double ended pointer list into a linear array beginning from
422 * right, the first element in the linear array will be the right one.
424 void **pdeq_copyr(pdeq *dq, const void **dst)
427 const void **d = dst;
435 p = q->p; i = q->n + p - 1;