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