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