3 * File name: ir/adt/pdeq.c
4 * Purpose: Pdeq --- double ended queue of generic pointers.
5 * Author: Christian von Roques
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Christian von Roques
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
31 /** Size of pdeq block cache. */
32 #define TUNE_NSAVED_PDEQS 16
35 * Maximal number of data items in a pdeq chunk.
37 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
40 # define VRFY(dq) ((void)0)
43 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
47 * A pointer double ended queue.
48 * This structure is used as a list chunk either.
52 unsigned cookie; /**< debug cookie */
54 pdeq *l_end, *r_end; /**< left and right ends of the deque */
55 pdeq *l, *r; /**< left and right neighbour */
56 int n; /**< number of elements in the current chunk */
57 int p; /**< the read/write pointer */
58 const void *data[1]; /**< storage for elements */
63 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
64 * +1 for compilers that can't grok empty arrays
66 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
69 * Number of pdeqs in pdeq_store.
71 unsigned pdeqs_cached;
74 * Free a pdeq chunk, put in into the cache if possible.
76 * @param p The pdeq chunk.
78 static INLINE void free_pdeq_block (pdeq *p)
81 p->cookie = 0xbadf00d1;
83 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
84 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
85 pdeq_block_cache[pdeqs_cached++] = p;
87 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
93 * Allocate a new pdeq chunk, get it from the cache if possible.
95 * @return A new pdeq chunk.
97 static INLINE pdeq *alloc_pdeq_block (void)
100 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
101 p = pdeq_block_cache[--pdeqs_cached];
102 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
104 p = xmalloc (PREF_MALLOC_SIZE);
105 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
113 * Verify a double ended list, assert if failure.
115 * @param dq The list to verify.
117 void _pdeq_vrfy(pdeq *dq)
121 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
124 && (dq->cookie == PDEQ_COOKIE1)
125 && (dq->l_end && dq->r_end));
128 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
129 && ((q == dq->l_end) ^ (q->l != NULL))
130 && ((q == dq->r_end) ^ (q->r != NULL))
131 && (!q->l || (q == q->l->r))
132 && ((q->n >= 0) && (q->n <= NDATA))
133 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
134 && ((q->p >= 0) && (q->p < NDATA)));
140 /* Creates a new double ended pointer list. */
145 dq = alloc_pdeq_block();
148 dq->cookie = PDEQ_COOKIE1;
150 dq->l_end = dq->r_end = dq;
151 dq->l = dq->r = NULL;
155 if (d_(df_pdeq, 1)) printf("(new_pdeq ==> %p)\n", dq);
159 /* Creates a new double ended pointer list and puts an initial pointer element in. */
160 pdeq *new_pdeq1(const void *x)
162 return pdeq_putr(new_pdeq(), x);
165 /* Delete a double ended pointer list. */
166 void del_pdeq(pdeq *dq)
172 q = dq->l_end; /* left end of chain */
173 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
174 if (dq->n == 0 && dq->l_end != dq ) {
178 /* Free all blocks in the pdeq chain */
184 if (d_(df_pdeq, 1)) printf("(del_pdeq %p)\n", dq);
187 /* Checks if a list is empty. */
188 int pdeq_empty(pdeq *dq)
191 if (d_(df_pdeq, 4)) printf("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
192 return dq->l_end->n == 0;
195 /* Returns the lenght of a double ended pointer list. */
196 int pdeq_len(pdeq *dq)
210 if (d_(df_pdeq, 4)) printf("(pdeq_len %p ==> %d)\n", dq, n);
214 /* Add a pointer to the right site of a double ended pointer list. */
215 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();
233 ndq->cookie = PDEQ_COOKIE2;
235 ndq->l_end = ndq->r_end = NULL;
239 ndq->l = rdq; rdq->r = ndq;
240 ndq->n = 0; ndq->p = 0;
245 n = rdq->n++ + rdq->p;
246 if (n >= NDATA) n -= NDATA;
251 if (d_(df_pdeq, 3) || pr) printf("(pdeq_putr %p %p)\n", dq, x);
255 /* Add a pointer to the left site of a double ended pointer list. */
256 pdeq *pdeq_putl(pdeq *dq, const void *x)
265 if (ldq->n >= NDATA) { /* headblock full */
268 ndq = dq; /* try to reuse trunk, but ... */
269 if (dq->n) { /* ... if trunk used */
270 /* allocate and init new block */
271 ndq = alloc_pdeq_block();
274 ndq->cookie = PDEQ_COOKIE2;
276 ndq->l_end = ndq->r_end = NULL;
280 ndq->r = ldq; ldq->l = ndq;
281 ndq->n = 0; ndq->p = 0;
288 if (p < 0) p += NDATA;
294 if (d_(df_pdeq, 3) || pr) printf("(pdeq_putl %p %p)\n", dq, x);
298 /* Retrieve a pointer from the right site of a double ended pointer list. */
299 void *pdeq_getr(pdeq *dq)
307 assert(dq->l_end->n);
310 n = rdq->p + --rdq->n;
311 if (n >= NDATA) n -= NDATA;
320 dq->r_end = dq->l_end = dq;
323 free_pdeq_block(rdq);
329 if (d_(df_pdeq, 3) || pr) printf("(pdeq_getr %p ==> %p)\n", dq, x);
333 /* Retrieve a pointer from the left site of a double ended pointer list. */
334 void *pdeq_getl(pdeq *dq)
342 assert(dq->l_end->n);
347 if (++p >= NDATA) p = 0;
356 dq->l_end = dq->r_end = dq;
359 free_pdeq_block(ldq);
365 if (d_(df_pdeq, 3) || pr) printf("(pdeq_getl %p ==> %p)\n", dq, x);
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;
386 do if (q->data[p] == x) goto found; while (++p < NDATA);
391 while (p < ep) if (q->data[p++] == x) goto found;
396 if (d_(df_pdeq, 3)) printf("(pdeq_contains %p %p ==> 0)\n", dq, x);
399 found: /* The two gotos can be optimized away, if !DEBUG */
400 if (d_(df_pdeq, 3)) printf("(pdeq_contains %p %p ==> 1)\n", dq, x);
405 * Search a key in a double ended pointer list, the search
406 * is controlled by a compare function.
407 * An element is found, if the compare function returns 0.
408 * The search is started from the left site of the list.
410 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
421 p = q->p; ep = p + q->n;
424 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
429 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
434 if (d_(df_pdeq, 3)) printf("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
437 found: /* The two gotos can be optimized away, if !DEBUG */
438 if (d_(df_pdeq, 3)) printf("(pdeq_contains %p %p %p ==> %p)\n",
439 dq, cmp, key, q->data[p]);
440 return (void *)q->data[p-1];
444 * Convert the double ended pointer list into a linear array beginning from
445 * left, the first element in the linear array will be the left one.
447 void **pdeq_copyl(pdeq *dq, const void **dst)
450 const void **d = dst;
462 memcpy(d, &q->data[p], nn * sizeof(void *)); d += nn;
466 memcpy(d, &q->data[p], n * sizeof(void *)); d += n;
471 if (d_(df_pdeq, 3)) printf("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
476 * Convert the double ended pointer list into a linear array beginning from
477 * right, the first element in the linear array will be the right one.
479 void **pdeq_copyr(pdeq *dq, const void **dst)
482 const void **d = dst;
490 p = q->p; i = q->n + p - 1;
493 do *d++ = q->data[i]; while (--i >= 0);
497 do *d++ = q->data[i]; while (--i >= p);
502 if (d_(df_pdeq, 3)) printf("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);