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