s/full\>/ful/.
[libfirm] / ir / adt / pdeq.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       double ended queue of generic pointers.
23  * @author      Christian von Roques
24  * @date        1999 by getting from fiasco
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "fourcc.h"
35 #include "pdeq.h"
36 #include "xmalloc.h"
37 #include "align.h"
38
39 /* Pointer Double Ended Queue */
40 #define PDEQ_MAGIC1     FOURCC('P','D','E','1')
41 #define PDEQ_MAGIC2     FOURCC('P','D','E','2')
42
43 /** Size of pdeq block cache. */
44 #define TUNE_NSAVED_PDEQS 16
45
46 /**
47  * Maximal number of data items in a pdeq chunk.
48  */
49 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
50
51 #ifdef NDEBUG
52 # define VRFY(dq) ((void)0)
53 #else
54 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
55 #endif
56
57 /**
58  * A pointer double ended queue.
59  * This structure is used as a list chunk either.
60  */
61 struct pdeq {
62 #ifndef NDEBUG
63   unsigned magic;       /**< debug magic */
64 #endif
65   pdeq *l_end, *r_end;  /**< left and right ends of the queue */
66   pdeq *l, *r;          /**< left and right neighbor */
67   int n;                /**< number of elements in the current chunk */
68   int p;                /**< the read/write pointer */
69   const void *data[1];  /**< storage for elements */
70 };
71
72
73 /**
74  * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
75  * +1 for compilers that can't grok empty arrays
76  */
77 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
78
79 /**
80  * Number of pdeqs in pdeq_store.
81  */
82 static unsigned pdeqs_cached;
83
84 /**
85  * Free a pdeq chunk, put in into the cache if possible.
86  *
87  * @param p   The pdeq chunk.
88  */
89 static inline void free_pdeq_block (pdeq *p)
90 {
91 #ifndef NDEBUG
92   p->magic = 0xbadf00d1;
93 #endif
94   if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
95     pdeq_block_cache[pdeqs_cached++] = p;
96   } else {
97     xfree (p);
98   }
99 }
100
101 /**
102  * Allocate a new pdeq chunk, get it from the cache if possible.
103  *
104  * @return A new pdeq chunk.
105  */
106 static inline pdeq *alloc_pdeq_block (void)
107 {
108   pdeq *p;
109   if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
110     p = pdeq_block_cache[--pdeqs_cached];
111   } else {
112     p = xmalloc(PREF_MALLOC_SIZE);
113   }
114   return p;
115 }
116
117
118 #ifndef NDEBUG
119 /**
120  * Verify a double ended list, assert if failure.
121  *
122  * @param dq   The list to verify.
123  */
124 void _pdeq_vrfy(pdeq *dq)
125 {
126   pdeq *q;
127
128   assert (   dq
129           && (dq->magic == PDEQ_MAGIC1)
130           && (dq->l_end && dq->r_end));
131   q = dq->l_end;
132   while (q) {
133     assert (   ((q == dq) || (q->magic == PDEQ_MAGIC2))
134             && ((q == dq->l_end) ^ (q->l != NULL))
135             && ((q == dq->r_end) ^ (q->r != NULL))
136             && (!q->l || (q == q->l->r))
137             && ((q->n >= 0) && (q->n <= NDATA))
138             && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
139             && ((q->p >= 0) && (q->p < NDATA)));
140     q = q->r;
141   }
142 }
143 #endif
144
145 /* Creates a new double ended pointer list. */
146 pdeq *new_pdeq(void)
147 {
148   pdeq *dq;
149
150   dq = alloc_pdeq_block();
151
152 #ifndef NDEBUG
153   dq->magic = PDEQ_MAGIC1;
154 #endif
155   dq->l_end = dq->r_end = dq;
156   dq->l = dq->r = NULL;
157   dq->n = dq->p = 0;
158
159   VRFY(dq);
160   return dq;
161 }
162
163 /* Creates a new double ended pointer list and puts an initial pointer element in. */
164 pdeq *new_pdeq1(const void *x)
165 {
166   return pdeq_putr(new_pdeq(), x);
167 }
168
169 /* Delete a double ended pointer list. */
170 void del_pdeq(pdeq *dq)
171 {
172   pdeq *q, *qq;
173
174   VRFY(dq);
175
176   q = dq->l_end;                /* left end of chain */
177   /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
178   if (dq->n == 0 && dq->l_end != dq ) {
179     free_pdeq_block(dq);
180   }
181
182   /* Free all blocks in the pdeq chain */
183   do {
184     qq = q->r;
185     free_pdeq_block(q);
186   } while ((q = qq));
187
188 }
189
190 /* Checks if a list is empty. */
191 int pdeq_empty(pdeq *dq)
192 {
193   VRFY(dq);
194   return dq->l_end->n == 0;
195 }
196
197 /* Returns the length of a double ended pointer list. */
198 int pdeq_len(pdeq *dq)
199 {
200   int n;
201   pdeq *q;
202
203   VRFY(dq);
204
205   n = 0;
206   q = dq->l_end;
207   do {
208     n += q->n;
209     q = q->r;
210   }  while (q);
211
212   return n;
213 }
214
215 /* Add a pointer to the right site of a double ended pointer list. */
216 pdeq *pdeq_putr(pdeq *dq, const void *x)
217 {
218   pdeq *rdq;
219   int n;
220
221   VRFY(dq);
222
223   rdq = dq->r_end;
224   if (rdq->n >= NDATA) {        /* tailblock full */
225     pdeq *ndq;
226
227     ndq = dq;                   /* try to reuse trunk, but ... */
228     if (dq->n) {                /* ... if trunk used */
229       /* allocate and init new block */
230       ndq = alloc_pdeq_block();
231 #ifndef NDEBUG
232       ndq->magic = PDEQ_MAGIC2;
233 #endif
234       ndq->l_end = ndq->r_end = NULL;
235     }
236
237     ndq->r = NULL;
238     ndq->l = rdq; rdq->r = ndq;
239     ndq->n = 0; ndq->p = 0;
240     dq->r_end = ndq;
241     rdq = ndq;
242   }
243
244   n = rdq->n++ + rdq->p;
245   if (n >= NDATA) n -= NDATA;
246
247   rdq->data[n] = x;
248
249   VRFY(dq);
250   return dq;
251 }
252
253 /* Add a pointer to the left site of a double ended pointer list. */
254 pdeq *pdeq_putl(pdeq *dq, const void *x)
255 {
256   pdeq *ldq;
257   int p;
258
259   VRFY(dq);
260
261   ldq = dq->l_end;
262   if (ldq->n >= NDATA) {        /* headblock full */
263     pdeq *ndq;
264
265     ndq = dq;                   /* try to reuse trunk, but ... */
266     if (dq->n) {                /* ... if trunk used */
267       /* allocate and init new block */
268       ndq = alloc_pdeq_block();
269 #ifndef NDEBUG
270       ndq->magic = PDEQ_MAGIC2;
271 #endif
272       ndq->l_end = ndq->r_end = NULL;
273     }
274
275     ndq->l = NULL;
276     ndq->r = ldq; ldq->l = ndq;
277     ndq->n = 0; ndq->p = 0;
278     dq->l_end = ndq;
279     ldq = ndq;
280   }
281
282   ldq->n++;
283   p = ldq->p - 1;
284   if (p < 0) p += NDATA;
285   ldq->p = p;
286
287   ldq->data[p] = x;
288
289   VRFY(dq);
290   return dq;
291 }
292
293 /* Retrieve a pointer from the right site of a double ended pointer list. */
294 void *pdeq_getr(pdeq *dq)
295 {
296   pdeq *rdq;
297   const void *x;
298   int n;
299
300   VRFY(dq);
301   assert(dq->l_end->n);
302
303   rdq = dq->r_end;
304   n = rdq->p + --rdq->n;
305   if (n >= NDATA) n -= NDATA;
306   x = rdq->data[n];
307
308   if (rdq->n == 0) {
309     if (rdq->l) {
310       dq->r_end = rdq->l;
311       rdq->l->r = NULL;
312       rdq->l = NULL;
313     } else {
314       dq->r_end = dq->l_end = dq;
315     }
316     if (dq != rdq) {
317       free_pdeq_block(rdq);
318     }
319   }
320
321   VRFY(dq);
322   return (void *)x;
323 }
324
325 /* Retrieve a pointer from the left site of a double ended pointer list. */
326 void *pdeq_getl(pdeq *dq)
327 {
328   pdeq *ldq;
329   const void *x;
330   int p;
331
332   VRFY(dq);
333   assert(dq->l_end->n);
334
335   ldq = dq->l_end;
336   p = ldq->p;
337   x = ldq->data[p];
338   if (++p >= NDATA) p = 0;
339   ldq->p = p;
340
341   if (--ldq->n == 0) {
342     if (ldq->r) {
343       dq->l_end = ldq->r;
344       ldq->r->l = NULL;
345       ldq->r = NULL;
346     } else {
347       dq->l_end = dq->r_end = dq;
348     }
349     if (dq != ldq) {
350       free_pdeq_block(ldq);
351     }
352   }
353
354   VRFY(dq);
355   return (void *)x;
356 }
357
358 /*
359  * Returns non-zero if a double ended pointer list
360  * contains a pointer x.
361  */
362 int pdeq_contains(pdeq *dq, const void *x)
363 {
364   pdeq *q;
365
366   VRFY(dq);
367
368   q = dq->l_end;
369   do {
370     int p, ep;
371
372     p = q->p; ep = p + q->n;
373
374     if (ep > NDATA) {
375       do {
376         if (q->data[p] == x) return 1;
377       } while (++p < NDATA);
378       p = 0;
379       ep -= NDATA;
380     }
381
382     while (p < ep) {
383       if (q->data[p++] == x) return 1;
384     }
385
386     q = q->r;
387   } while (q);
388
389   return 0;
390 }
391
392 /*
393  * Search a key in a double ended pointer list, the search
394  * is controlled by a compare function.
395  * An element is found, if the compare function returns 0.
396  * The search is started from the left site of the list.
397  */
398 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
399 {
400   pdeq *q;
401   int p;
402
403   VRFY(dq);
404
405   q = dq->l_end;
406   do {
407     int ep;
408
409     p = q->p; ep = p + q->n;
410
411     if (ep > NDATA) {
412       do {
413         if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
414       } while (++p < NDATA);
415       p = 0;
416       ep -= NDATA;
417     }
418
419     while (p < ep) {
420       if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
421     }
422
423     q = q->r;
424   } while (q);
425
426   return NULL;
427 }
428
429 /*
430  * Convert the double ended pointer list into a linear array beginning from
431  * left, the first element in the linear array will be the left one.
432  */
433 void **pdeq_copyl(pdeq *dq, const void **dst)
434 {
435   pdeq *q;
436   const void **d = dst;
437
438   VRFY(dq);
439
440   q = dq->l_end;
441   while (q) {
442     int p, n;
443
444     p = q->p; n = q->n;
445
446     if (n + p > NDATA) {
447       int nn = NDATA - p;
448       memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
449       p = 0; n -= nn;
450     }
451
452     memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
453
454     q = q->r;
455   }
456
457   return (void **)dst;
458 }
459
460 /*
461  * Convert the double ended pointer list into a linear array beginning from
462  * right, the first element in the linear array will be the right one.
463  */
464 void **pdeq_copyr(pdeq *dq, const void **dst)
465 {
466   pdeq *q;
467   const void **d = dst;
468
469   VRFY(dq);
470
471   q = dq->r_end;
472   while (q) {
473     int p, i;
474
475     p = q->p; i = q->n + p - 1;
476     if (i >= NDATA) {
477       i -= NDATA;
478       do *d++ = q->data[i]; while (--i >= 0);
479       i = NDATA - 1;
480     }
481
482     do *d++ = q->data[i]; while (--i >= p);
483
484     q = q->l;
485   }
486
487   return (void **)dst;
488 }