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