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 Markus Armbruster
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
38 /* # of data items in block */
39 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
42 # define VRFY(dq) ((void)0)
45 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
53 pdeq *l_end, *r_end; /* left and right ends of the deque */
54 pdeq *l, *r; /* left and right neighbour */
60 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
61 /* +1 for compilers that can't grok empty arrays */
62 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
63 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
67 free_pdeq_block (pdeq *p)
70 p->cookie = 0xbadf00d1;
72 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
73 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
74 pdeq_block_cache[pdeqs_cached++] = p;
76 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
83 alloc_pdeq_block (void)
86 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
87 p = pdeq_block_cache[--pdeqs_cached];
88 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
90 p = xmalloc (PREF_MALLOC_SIZE);
91 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
103 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
106 && (dq->cookie == PDEQ_COOKIE1)
107 && (dq->l_end && dq->r_end));
110 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
111 && ((q == dq->l_end) ^ (q->l!=0))
112 && ((q == dq->r_end) ^ (q->r!=0))
113 && (!q->l || (q == q->l->r))
114 && ((q->n>=0) && (q->n<=NDATA))
115 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
116 && ((q->p>=0) && (q->p<NDATA)));
128 dq = alloc_pdeq_block();
131 dq->cookie = PDEQ_COOKIE1;
133 dq->l_end = dq->r_end = dq;
134 dq->l = dq->r = NULL;
138 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
144 new_pdeq1 (const void *x)
146 return pdeq_putr (new_pdeq(), x);
157 q = dq->l_end; /* left end of chain */
158 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
159 if (dq->n == 0 && dq->l_end != dq ) {
160 free_pdeq_block (dq);
163 /* Free all blocks in the pdeq chain */
169 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
174 pdeq_empty (pdeq *dq)
177 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
178 return dq->l_end->n == 0;
197 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
203 pdeq_putr (pdeq *dq, const void *x)
212 if (rdq->n >= NDATA) { /* tailblock full */
215 ndq = dq; /* try to reuse trunk, but ... */
216 if (dq->n) { /* ... if trunk used */
217 /* allocate and init new block */
218 ndq = alloc_pdeq_block ();
219 pr = d_ (df_pdeq, 2);
221 ndq->cookie = PDEQ_COOKIE2;
223 ndq->l_end = ndq->r_end = NULL;
227 ndq->l = rdq; rdq->r = ndq;
228 ndq->n = 0; ndq->p = 0;
233 n = rdq->n++ + rdq->p;
234 if (n >= NDATA) n -= NDATA;
239 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
245 pdeq_putl (pdeq *dq, const void *x)
254 if (ldq->n >= NDATA) { /* headblock full */
257 ndq = dq; /* try to reuse trunk, but ... */
258 if (dq->n) { /* ... if trunk used */
259 /* allocate and init new block */
260 ndq = alloc_pdeq_block ();
261 pr = d_ (df_pdeq, 2);
263 ndq->cookie = PDEQ_COOKIE2;
265 ndq->l_end = ndq->r_end = NULL;
269 ndq->r = ldq; ldq->l = ndq;
270 ndq->n = 0; ndq->p = 0;
277 if (p < 0) p += NDATA;
283 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
297 assert (dq->l_end->n);
300 n = rdq->p + --rdq->n;
301 if (n>=NDATA) n -= NDATA;
310 dq->r_end = dq->l_end = dq;
313 free_pdeq_block (rdq);
314 pr = d_ (df_pdeq, 2);
319 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
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);
351 pr = d_ (df_pdeq, 2);
356 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
362 pdeq_contains (pdeq *dq, const void *x)
372 p = q->p; ep = p + q->n;
375 do if (q->data[p] == x) goto found; while (++p < NDATA);
380 while (p < ep) if (q->data[p++] == x) goto found;
385 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
388 found: /* The two gotos can be optimized away, if !DEBUG */
389 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
395 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
406 p = q->p; ep = p + q->n;
409 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
414 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
419 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
422 found: /* The two gotos can be optimized away, if !DEBUG */
423 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
424 dq, cmp, key, q->data[p]);
425 return (void *)q->data[p-1];
430 pdeq_copyl (pdeq *dq, const void **dst)
433 const void **d = dst;
445 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
449 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
454 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
460 pdeq_copyr (pdeq *dq, const void **dst)
463 const void **d = dst;
471 p = q->p; i = q->n + p - 1;
474 do *d++ = q->data[i]; while (--i >= 0);
478 do *d++ = q->data[i]; while (--i >= p);
483 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);