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