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