corrected copyright
[libfirm] / ir / adt / pdeq.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/pdeq.c
4  * Purpose:     Pdeq --- double ended queue of generic pointers.
5  * Author:      Christian von Roques
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Christian von Roques
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdbool.h>
22 # ifdef HAVE_STRING_H
23 #  include <string.h>
24 # endif
25
26 #include "cookies.h"
27 #include "debug.h"
28 #include "pdeq.h"
29 #include "xmalloc.h"
30
31
32 /** Size of pdeq block cache */
33 #define TUNE_NSAVED_PDEQS 16
34
35 #ifndef INLINE
36 #ifdef USE_GCC_INLINE
37 #define INLINE inline
38 #else
39 #define INLINE
40 #endif
41 #endif
42
43 /* # of data items in block */
44 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
45
46 #ifdef NDEBUG
47 # define VRFY(dq) ((void)0)
48 #else
49 # define VRFY(dq) \
50     (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
51 #endif
52
53
54 struct pdeq {
55 #ifndef NDEBUG
56   unsigned cookie;
57 #endif
58   pdeq *l_end, *r_end;          /* left and right ends of the deque */
59   pdeq *l, *r;                  /*  left and right neighbour */
60   int n, p;
61   const void *data[1];
62 };
63
64
65 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
66 /* +1 for compilers that can't grok empty arrays */
67 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
68 unsigned pdeqs_cached;          /* # pdeqs in pdeq_store */
69
70
71 static INLINE void
72 free_pdeq_block (pdeq *p)
73 {
74 #ifndef NDEBUG
75   p->cookie = 0xbadf00d1;
76 #endif
77   if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
78     if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
79     pdeq_block_cache[pdeqs_cached++] = p;
80   } else {
81     if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
82     xfree (p);
83   }
84 }
85
86
87 static INLINE pdeq *
88 alloc_pdeq_block (void)
89 {
90   pdeq *p;
91   if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
92     p = pdeq_block_cache[--pdeqs_cached];
93     if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
94   } else {
95     p = xmalloc (PREF_MALLOC_SIZE);
96     if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
97   }
98   return p;
99 }
100
101
102 #ifndef NDEBUG
103 void
104 _pdeq_vrfy (pdeq *dq)
105 {
106   pdeq *q;
107
108   if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
109
110   assert (   dq
111           && (dq->cookie == PDEQ_COOKIE1)
112           && (dq->l_end && dq->r_end));
113   q = dq->l_end;
114   while (q) {
115     assert (   ((q == dq) || (q->cookie == PDEQ_COOKIE2))
116             && ((q == dq->l_end) ^ (q->l!=0))
117             && ((q == dq->r_end) ^ (q->r!=0))
118             && (!q->l || (q == q->l->r))
119             && ((q->n>=0) && (q->n<=NDATA))
120             && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
121             && ((q->p>=0) && (q->p<NDATA)));
122     q = q->r;
123   }
124 }
125 #endif
126
127
128 pdeq *
129 new_pdeq (void)
130 {
131   pdeq *dq;
132
133   dq = alloc_pdeq_block();
134
135 #ifndef NDEBUG
136   dq->cookie = PDEQ_COOKIE1;
137 #endif
138   dq->l_end = dq->r_end = dq;
139   dq->l = dq->r = NULL;
140   dq->n = dq->p = 0;
141
142   VRFY (dq);
143   if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
144   return dq;
145 }
146
147
148 pdeq *
149 new_pdeq1 (const void *x)
150 {
151   return pdeq_putr (new_pdeq(), x);
152 }
153
154
155 void
156 del_pdeq (pdeq *dq)
157 {
158   pdeq *q, *qq;
159
160   VRFY (dq);
161
162   q = dq->l_end;                /* left end of chain */
163   /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
164   if (dq->n == 0 && dq->l_end != dq ) {
165     free_pdeq_block (dq);
166   }
167
168   /* Free all blocks in the pdeq chain */
169   do {
170     qq = q->r;
171     free_pdeq_block (q);
172   } while ((q = qq));
173
174   if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
175 }
176
177
178 bool
179 pdeq_empty (pdeq *dq)
180 {
181   VRFY (dq);
182   if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
183   return dq->l_end->n == 0;
184 }
185
186
187 int
188 pdeq_len (pdeq *dq)
189 {
190   int n;
191   pdeq *q;
192
193   VRFY (dq);
194
195   n = 0;
196   q = dq->l_end;
197   do {
198     n += q->n;
199     q = q->r;
200   }  while (q);
201
202   if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
203   return n;
204 }
205
206
207 pdeq *
208 pdeq_putr (pdeq *dq, const void *x)
209 {
210   pdeq *rdq;
211   int n;
212   bool pr = 0;
213
214   VRFY (dq);
215
216   rdq = dq->r_end;
217   if (rdq->n >= NDATA) {        /* tailblock full */
218     pdeq *ndq;
219
220     ndq = dq;                   /* try to reuse trunk, but ... */
221     if (dq->n) {                /* ... if trunk used */
222       /* allocate and init new block */
223       ndq = alloc_pdeq_block ();
224       pr = d_ (df_pdeq, 2);
225 #ifndef NDEBUG
226       ndq->cookie = PDEQ_COOKIE2;
227 #endif
228       ndq->l_end = ndq->r_end = NULL;
229     }
230
231     ndq->r = NULL;
232     ndq->l = rdq; rdq->r = ndq;
233     ndq->n = 0; ndq->p = 0;
234     dq->r_end = ndq;
235     rdq = ndq;
236   }
237
238   n = rdq->n++ + rdq->p;
239   if (n >= NDATA) n -= NDATA;
240
241   rdq->data[n] = x;
242
243   VRFY (dq);
244   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
245   return dq;
246 }
247
248
249 pdeq *
250 pdeq_putl (pdeq *dq, const void *x)
251 {
252   pdeq *ldq;
253   int p;
254   bool pr = 0;
255
256   VRFY (dq);
257
258   ldq = dq->l_end;
259   if (ldq->n >= NDATA) {        /* headblock full */
260     pdeq *ndq;
261
262     ndq = dq;                   /* try to reuse trunk, but ... */
263     if (dq->n) {                /* ... if trunk used */
264       /* allocate and init new block */
265       ndq = alloc_pdeq_block ();
266       pr = d_ (df_pdeq, 2);
267 #ifndef NDEBUG
268       ndq->cookie = PDEQ_COOKIE2;
269 #endif
270       ndq->l_end = ndq->r_end = NULL;
271     }
272
273     ndq->l = NULL;
274     ndq->r = ldq; ldq->l = ndq;
275     ndq->n = 0; ndq->p = 0;
276     dq->l_end = ndq;
277     ldq = ndq;
278   }
279
280   ldq->n++;
281   p = ldq->p - 1;
282   if (p < 0) p += NDATA;
283   ldq->p = p;
284
285   ldq->data[p] = x;
286
287   VRFY (dq);
288   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
289   return dq;
290 }
291
292
293 void *
294 pdeq_getr (pdeq *dq)
295 {
296   pdeq *rdq;
297   const void *x;
298   int n;
299   bool pr = 0;
300
301   VRFY (dq);
302   assert (dq->l_end->n);
303
304   rdq = dq->r_end;
305   n = rdq->p + --rdq->n;
306   if (n>=NDATA) n -= NDATA;
307   x = rdq->data[n];
308
309   if (rdq->n == 0) {
310     if (rdq->l) {
311       dq->r_end = rdq->l;
312       rdq->l->r = NULL;
313       rdq->l = NULL;
314     } else {
315       dq->r_end = dq->l_end = dq;
316     }
317     if (dq != rdq) {
318       free_pdeq_block (rdq);
319       pr = d_ (df_pdeq, 2);
320     }
321   }
322
323   VRFY (dq);
324   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
325   return (void *)x;
326 }
327
328
329 void *
330 pdeq_getl (pdeq *dq)
331 {
332   pdeq *ldq;
333   const void *x;
334   int p;
335   bool pr = 0;
336
337   VRFY (dq);
338   assert (dq->l_end->n);
339
340   ldq = dq->l_end;
341   p = ldq->p;
342   x = ldq->data[p];
343   if (++p >= NDATA) p = 0;
344   ldq->p = p;
345
346   if (--ldq->n == 0) {
347     if (ldq->r) {
348       dq->l_end = ldq->r;
349       ldq->r->l = NULL;
350       ldq->r = NULL;
351     } else {
352       dq->l_end = dq->r_end = dq;
353     }
354     if (dq != ldq) {
355       free_pdeq_block (ldq);
356       pr = d_ (df_pdeq, 2);
357     }
358   }
359
360   VRFY (dq);
361   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
362   return (void *)x;
363 }
364
365
366 bool
367 pdeq_contains (pdeq *dq, const void *x)
368 {
369   pdeq *q;
370
371   VRFY (dq);
372
373   q = dq->l_end;
374   do {
375     int p, ep;
376
377     p = q->p; ep = p + q->n;
378
379     if (ep > NDATA) {
380       do if (q->data[p] == x) goto found; while (++p < NDATA);
381       p = 0;
382       ep -= NDATA;
383     }
384
385     while (p < ep) if (q->data[p++] == x) goto found;
386
387     q = q->r;
388   } while (q);
389
390   if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
391   return 0;
392
393 found:  /* The two gotos can be optimized away, if !DEBUG */
394   if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
395   return 1;
396 }
397
398
399 void *
400 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
401 {
402   pdeq *q;
403   int p;
404
405   VRFY (dq);
406
407   q = dq->l_end;
408   do {
409     int ep;
410
411     p = q->p; ep = p + q->n;
412
413     if (ep > NDATA) {
414       do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
415       p = 0;
416       ep -= NDATA;
417     }
418
419     while (p < ep) if (!cmp (q->data[p++], key)) goto found;
420
421     q = q->r;
422   } while (q);
423
424   if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
425   return NULL;
426
427 found:  /* The two gotos can be optimized away, if !DEBUG */
428   if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
429                                dq, cmp, key, q->data[p]);
430   return (void *)q->data[p-1];
431 }
432
433
434 void **
435 pdeq_copyl (pdeq *dq, const void **dst)
436 {
437   pdeq *q;
438   const void **d = dst;
439
440   VRFY (dq);
441
442   q = dq->l_end;
443   while (q) {
444     int p, n;
445
446     p = q->p; n = q->n;
447
448     if (n + p > NDATA) {
449       int nn = NDATA - p;
450       memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
451       p = 0; n -= nn;
452     }
453
454     memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
455
456     q = q->r;
457   }
458
459   if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
460   return (void **)dst;
461 }
462
463
464 void **
465 pdeq_copyr (pdeq *dq, const void **dst)
466 {
467   pdeq *q;
468   const void **d = dst;
469
470   VRFY (dq);
471
472   q = dq->r_end;
473   while (q) {
474     int p, i;
475
476     p = q->p; i = q->n + p - 1;
477     if (i >= NDATA) {
478       i -= NDATA;
479       do *d++ = q->data[i]; while (--i >= 0);
480       i = NDATA - 1;
481     }
482
483     do *d++ = q->data[i]; while (--i >= p);
484
485     q = q->l;
486   }
487
488   if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);
489   return (void **)dst;
490 }