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.
30 /** Size of pdeq block cache */
31 #define TUNE_NSAVED_PDEQS 16
41 /* # of data items in block */
42 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
45 # define VRFY(dq) ((void)0)
48 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
56 pdeq *l_end, *r_end; /* left and right ends of the deque */
57 pdeq *l, *r; /* left and right neighbour */
63 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
64 /* +1 for compilers that can't grok empty arrays */
65 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
66 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
70 free_pdeq_block (pdeq *p)
73 p->cookie = 0xbadf00d1;
75 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
76 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
77 pdeq_block_cache[pdeqs_cached++] = p;
79 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
86 alloc_pdeq_block (void)
89 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
90 p = pdeq_block_cache[--pdeqs_cached];
91 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
93 p = xmalloc (PREF_MALLOC_SIZE);
94 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
102 _pdeq_vrfy (pdeq *dq)
106 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
109 && (dq->cookie == PDEQ_COOKIE1)
110 && (dq->l_end && dq->r_end));
113 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
114 && ((q == dq->l_end) ^ (q->l!=0))
115 && ((q == dq->r_end) ^ (q->r!=0))
116 && (!q->l || (q == q->l->r))
117 && ((q->n>=0) && (q->n<=NDATA))
118 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
119 && ((q->p>=0) && (q->p<NDATA)));
131 dq = alloc_pdeq_block();
134 dq->cookie = PDEQ_COOKIE1;
136 dq->l_end = dq->r_end = dq;
137 dq->l = dq->r = NULL;
141 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
147 new_pdeq1 (const void *x)
149 return pdeq_putr (new_pdeq(), x);
160 q = dq->l_end; /* left end of chain */
161 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
162 if (dq->n == 0 && dq->l_end != dq ) {
163 free_pdeq_block (dq);
166 /* Free all blocks in the pdeq chain */
172 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
177 pdeq_empty (pdeq *dq)
180 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
181 return dq->l_end->n == 0;
200 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
206 pdeq_putr (pdeq *dq, const void *x)
215 if (rdq->n >= NDATA) { /* tailblock full */
218 ndq = dq; /* try to reuse trunk, but ... */
219 if (dq->n) { /* ... if trunk used */
220 /* allocate and init new block */
221 ndq = alloc_pdeq_block ();
222 pr = d_ (df_pdeq, 2);
224 ndq->cookie = PDEQ_COOKIE2;
226 ndq->l_end = ndq->r_end = NULL;
230 ndq->l = rdq; rdq->r = ndq;
231 ndq->n = 0; ndq->p = 0;
236 n = rdq->n++ + rdq->p;
237 if (n >= NDATA) n -= NDATA;
242 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
248 pdeq_putl (pdeq *dq, const void *x)
257 if (ldq->n >= NDATA) { /* headblock full */
260 ndq = dq; /* try to reuse trunk, but ... */
261 if (dq->n) { /* ... if trunk used */
262 /* allocate and init new block */
263 ndq = alloc_pdeq_block ();
264 pr = d_ (df_pdeq, 2);
266 ndq->cookie = PDEQ_COOKIE2;
268 ndq->l_end = ndq->r_end = NULL;
272 ndq->r = ldq; ldq->l = ndq;
273 ndq->n = 0; ndq->p = 0;
280 if (p < 0) p += NDATA;
286 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
300 assert (dq->l_end->n);
303 n = rdq->p + --rdq->n;
304 if (n>=NDATA) n -= NDATA;
313 dq->r_end = dq->l_end = dq;
316 free_pdeq_block (rdq);
317 pr = d_ (df_pdeq, 2);
322 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
336 assert (dq->l_end->n);
341 if (++p >= NDATA) p = 0;
350 dq->l_end = dq->r_end = dq;
353 free_pdeq_block (ldq);
354 pr = d_ (df_pdeq, 2);
359 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
365 pdeq_contains (pdeq *dq, const void *x)
375 p = q->p; ep = p + q->n;
378 do if (q->data[p] == x) goto found; while (++p < NDATA);
383 while (p < ep) if (q->data[p++] == x) goto found;
388 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
391 found: /* The two gotos can be optimized away, if !DEBUG */
392 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
398 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
409 p = q->p; ep = p + q->n;
412 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
417 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
422 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
425 found: /* The two gotos can be optimized away, if !DEBUG */
426 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
427 dq, cmp, key, q->data[p]);
428 return (void *)q->data[p-1];
433 pdeq_copyl (pdeq *dq, const void **dst)
436 const void **d = dst;
448 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
452 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
457 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
463 pdeq_copyr (pdeq *dq, const void **dst)
466 const void **d = dst;
474 p = q->p; i = q->n + p - 1;
477 do *d++ = q->data[i]; while (--i >= 0);
481 do *d++ = q->data[i]; while (--i >= p);
486 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);