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.
32 /** Size of pdeq block cache */
33 #define TUNE_NSAVED_PDEQS 16
35 /* # of data items in block */
36 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
39 # define VRFY(dq) ((void)0)
42 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
50 pdeq *l_end, *r_end; /* left and right ends of the deque */
51 pdeq *l, *r; /* left and right neighbour */
57 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
58 /* +1 for compilers that can't grok empty arrays */
59 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
60 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
64 free_pdeq_block (pdeq *p)
67 p->cookie = 0xbadf00d1;
69 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
70 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
71 pdeq_block_cache[pdeqs_cached++] = p;
73 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
80 alloc_pdeq_block (void)
83 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
84 p = pdeq_block_cache[--pdeqs_cached];
85 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
87 p = xmalloc (PREF_MALLOC_SIZE);
88 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
100 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
103 && (dq->cookie == PDEQ_COOKIE1)
104 && (dq->l_end && dq->r_end));
107 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
108 && ((q == dq->l_end) ^ (q->l!=0))
109 && ((q == dq->r_end) ^ (q->r!=0))
110 && (!q->l || (q == q->l->r))
111 && ((q->n>=0) && (q->n<=NDATA))
112 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
113 && ((q->p>=0) && (q->p<NDATA)));
125 dq = alloc_pdeq_block();
128 dq->cookie = PDEQ_COOKIE1;
130 dq->l_end = dq->r_end = dq;
131 dq->l = dq->r = NULL;
135 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
141 new_pdeq1 (const void *x)
143 return pdeq_putr (new_pdeq(), x);
154 q = dq->l_end; /* left end of chain */
155 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
156 if (dq->n == 0 && dq->l_end != dq ) {
157 free_pdeq_block (dq);
160 /* Free all blocks in the pdeq chain */
166 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
171 pdeq_empty (pdeq *dq)
174 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
175 return dq->l_end->n == 0;
194 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
200 pdeq_putr (pdeq *dq, const void *x)
209 if (rdq->n >= NDATA) { /* tailblock full */
212 ndq = dq; /* try to reuse trunk, but ... */
213 if (dq->n) { /* ... if trunk used */
214 /* allocate and init new block */
215 ndq = alloc_pdeq_block ();
216 pr = d_ (df_pdeq, 2);
218 ndq->cookie = PDEQ_COOKIE2;
220 ndq->l_end = ndq->r_end = NULL;
224 ndq->l = rdq; rdq->r = ndq;
225 ndq->n = 0; ndq->p = 0;
230 n = rdq->n++ + rdq->p;
231 if (n >= NDATA) n -= NDATA;
236 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
242 pdeq_putl (pdeq *dq, const void *x)
251 if (ldq->n >= NDATA) { /* headblock full */
254 ndq = dq; /* try to reuse trunk, but ... */
255 if (dq->n) { /* ... if trunk used */
256 /* allocate and init new block */
257 ndq = alloc_pdeq_block ();
258 pr = d_ (df_pdeq, 2);
260 ndq->cookie = PDEQ_COOKIE2;
262 ndq->l_end = ndq->r_end = NULL;
266 ndq->r = ldq; ldq->l = ndq;
267 ndq->n = 0; ndq->p = 0;
274 if (p < 0) p += NDATA;
280 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
294 assert (dq->l_end->n);
297 n = rdq->p + --rdq->n;
298 if (n>=NDATA) n -= NDATA;
307 dq->r_end = dq->l_end = dq;
310 free_pdeq_block (rdq);
311 pr = d_ (df_pdeq, 2);
316 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
330 assert (dq->l_end->n);
335 if (++p >= NDATA) p = 0;
344 dq->l_end = dq->r_end = dq;
347 free_pdeq_block (ldq);
348 pr = d_ (df_pdeq, 2);
353 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
359 pdeq_contains (pdeq *dq, const void *x)
369 p = q->p; ep = p + q->n;
372 do if (q->data[p] == x) goto found; while (++p < NDATA);
377 while (p < ep) if (q->data[p++] == x) goto found;
382 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
385 found: /* The two gotos can be optimized away, if !DEBUG */
386 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
392 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
403 p = q->p; ep = p + q->n;
406 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
411 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
416 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
419 found: /* The two gotos can be optimized away, if !DEBUG */
420 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
421 dq, cmp, key, q->data[p]);
422 return (void *)q->data[p-1];
427 pdeq_copyl (pdeq *dq, const void **dst)
430 const void **d = dst;
442 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
446 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
451 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
457 pdeq_copyr (pdeq *dq, const void **dst)
460 const void **d = dst;
468 p = q->p; i = q->n + p - 1;
471 do *d++ = q->data[i]; while (--i >= 0);
475 do *d++ = q->data[i]; while (--i >= p);
480 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);