- cleaned up irp functions a bit
[libfirm] / ir / obstack_win / obstack.c
1 /* obstack.c - subroutines used implicitly by object stack macros
2    Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3
4
5    NOTE: This source is derived from an old version taken from the GNU C
6    Library (glibc).
7
8    This program is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21    USA.  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include "obstack.h"
27
28 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
29    incremented whenever callers compiled using an old obstack.h can no
30    longer properly call the functions in this obstack.c.  */
31 #define OBSTACK_INTERFACE_VERSION 1
32
33 /* Comment out all this code if we are using the GNU C Library, and are not
34    actually compiling the library itself, and the installed library
35    supports the same library interface we do.  This code is part of the GNU
36    C Library, but also included in many other GNU distributions.  Compiling
37    and linking in this code is a waste when using the GNU C library
38    (especially if it is a shared library).  Rather than having every GNU
39    program understand `configure --with-gnu-libc' and omit the object
40    files, it is simpler to just do this in the source for each such file.  */
41
42 #include <stdio.h>              /* Random thing to get __GNU_LIBRARY__.  */
43 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
44 #include <gnu-versions.h>
45 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
46 #define ELIDE_CODE
47 #endif
48 #endif
49
50
51 #ifndef ELIDE_CODE
52
53
54 #if defined (__STDC__) && __STDC__
55 #define POINTER void *
56 #else
57 #define POINTER char *
58 #endif
59
60 /* Determine default alignment.  */
61 struct fooalign {char x; double d;};
62 #define DEFAULT_ALIGNMENT  \
63   ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
64 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
65    But in fact it might be less smart and round addresses to as much as
66    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
67 union fooround {long x; double d;};
68 #define DEFAULT_ROUNDING (sizeof (union fooround))
69
70 /* When we copy a long block of data, this is the unit to do it with.
71    On some machines, copying successive ints does not work;
72    in such a case, redefine COPYING_UNIT to `long' (if that works)
73    or `char' as a last resort.  */
74 #ifndef COPYING_UNIT
75 #define COPYING_UNIT int
76 #endif
77
78
79 /* The functions allocating more room by calling `obstack_chunk_alloc'
80    jump to the handler pointed to by `obstack_alloc_failed_handler'.
81    This variable by default points to the internal function
82    `print_and_abort'.  */
83 #if defined (__STDC__) && __STDC__
84 static void print_and_abort (void);
85 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
86 #else
87 static void print_and_abort ();
88 void (*obstack_alloc_failed_handler) () = print_and_abort;
89 #endif
90
91 /* Exit value used when `print_and_abort' is used.  */
92 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
93 #include <stdlib.h>
94 #endif
95 #ifndef EXIT_FAILURE
96 #define EXIT_FAILURE 1
97 #endif
98 int obstack_exit_failure = EXIT_FAILURE;
99
100 /* The non-GNU-C macros copy the obstack into this global variable
101    to avoid multiple evaluation.  */
102
103 struct obstack *_obstack;
104
105 /* Define a macro that either calls functions with the traditional malloc/free
106    calling interface, or calls functions with the mmalloc/mfree interface
107    (that adds an extra first argument), based on the state of use_extra_arg.
108    For free, do not use ?:, since some compilers, like the MIPS compilers,
109    do not allow (expr) ? void : void.  */
110
111 #if defined (__STDC__) && __STDC__
112 #define CALL_CHUNKFUN(h, size) \
113   (((h) -> use_extra_arg) \
114    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
115    : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
116
117 #define CALL_FREEFUN(h, old_chunk) \
118   do { \
119     if ((h) -> use_extra_arg) \
120       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
121     else \
122       (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
123   } while (0)
124 #else
125 #define CALL_CHUNKFUN(h, size) \
126   (((h) -> use_extra_arg) \
127    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
128    : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
129
130 #define CALL_FREEFUN(h, old_chunk) \
131   do { \
132     if ((h) -> use_extra_arg) \
133       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
134     else \
135       (*(void (*) ()) (h)->freefun) ((old_chunk)); \
136   } while (0)
137 #endif
138
139
140 /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
141    Objects start on multiples of ALIGNMENT (0 means use default).
142    CHUNKFUN is the function to use to allocate chunks,
143    and FREEFUN the function to free them.
144
145    Return nonzero if successful, zero if out of memory.
146    To recover from an out of memory error,
147    free up some memory, then call this again.  */
148
149 int
150 _obstack_begin (h, size, alignment, chunkfun, freefun)
151      struct obstack *h;
152      int size;
153      int alignment;
154 #if defined (__STDC__) && __STDC__
155      POINTER (*chunkfun) (long);
156      void (*freefun) (void *);
157 #else
158      POINTER (*chunkfun) ();
159      void (*freefun) ();
160 #endif
161 {
162   register struct _obstack_chunk *chunk; /* points to new chunk */
163
164   if (alignment == 0)
165     alignment = (int) DEFAULT_ALIGNMENT;
166   if (size == 0)
167     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
168     {
169       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
170          Use the values for range checking, because if range checking is off,
171          the extra bytes won't be missed terribly, but if range checking is on
172          and we used a larger request, a whole extra 4096 bytes would be
173          allocated.
174
175          These number are irrelevant to the new GNU malloc.  I suspect it is
176          less sensitive to the size of the request.  */
177       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
178                     + 4 + DEFAULT_ROUNDING - 1)
179                    & ~(DEFAULT_ROUNDING - 1));
180       size = 4096 - extra;
181     }
182
183 #if defined (__STDC__) && __STDC__
184   h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
185   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
186 #else
187   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
188   h->freefun = freefun;
189 #endif
190   h->chunk_size = size;
191   h->alignment_mask = alignment - 1;
192   h->use_extra_arg = 0;
193
194   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
195   if (!chunk)
196     (*obstack_alloc_failed_handler) ();
197   h->next_free = h->object_base = chunk->contents;
198   h->chunk_limit = chunk->limit
199     = (char *) chunk + h->chunk_size;
200   chunk->prev = 0;
201   /* The initial chunk now contains no empty object.  */
202   h->maybe_empty_object = 0;
203   h->alloc_failed = 0;
204   return 1;
205 }
206
207 int
208 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
209      struct obstack *h;
210      int size;
211      int alignment;
212 #if defined (__STDC__) && __STDC__
213      POINTER (*chunkfun) (POINTER, long);
214      void (*freefun) (POINTER, POINTER);
215 #else
216      POINTER (*chunkfun) ();
217      void (*freefun) ();
218 #endif
219      POINTER arg;
220 {
221   register struct _obstack_chunk *chunk; /* points to new chunk */
222
223   if (alignment == 0)
224     alignment = (int) DEFAULT_ALIGNMENT;
225   if (size == 0)
226     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
227     {
228       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
229          Use the values for range checking, because if range checking is off,
230          the extra bytes won't be missed terribly, but if range checking is on
231          and we used a larger request, a whole extra 4096 bytes would be
232          allocated.
233
234          These number are irrelevant to the new GNU malloc.  I suspect it is
235          less sensitive to the size of the request.  */
236       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
237                     + 4 + DEFAULT_ROUNDING - 1)
238                    & ~(DEFAULT_ROUNDING - 1));
239       size = 4096 - extra;
240     }
241
242 #if defined(__STDC__) && __STDC__
243   h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
244   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
245 #else
246   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
247   h->freefun = freefun;
248 #endif
249   h->chunk_size = size;
250   h->alignment_mask = alignment - 1;
251   h->extra_arg = arg;
252   h->use_extra_arg = 1;
253
254   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
255   if (!chunk)
256     (*obstack_alloc_failed_handler) ();
257   h->next_free = h->object_base = chunk->contents;
258   h->chunk_limit = chunk->limit
259     = (char *) chunk + h->chunk_size;
260   chunk->prev = 0;
261   /* The initial chunk now contains no empty object.  */
262   h->maybe_empty_object = 0;
263   h->alloc_failed = 0;
264   return 1;
265 }
266
267 /* Allocate a new current chunk for the obstack *H
268    on the assumption that LENGTH bytes need to be added
269    to the current object, or a new object of length LENGTH allocated.
270    Copies any partial object from the end of the old chunk
271    to the beginning of the new one.  */
272
273 void
274 _obstack_newchunk (h, length)
275      struct obstack *h;
276      int length;
277 {
278   register struct _obstack_chunk *old_chunk = h->chunk;
279   register struct _obstack_chunk *new_chunk;
280   register long new_size;
281   register long obj_size = h->next_free - h->object_base;
282   register long i;
283   long already;
284
285   /* Compute size for new chunk.  */
286   new_size = (obj_size + length) + (obj_size >> 3) + 100;
287   if (new_size < h->chunk_size)
288     new_size = h->chunk_size;
289
290   /* Allocate and initialize the new chunk.  */
291   new_chunk = CALL_CHUNKFUN (h, new_size);
292   if (!new_chunk)
293     (*obstack_alloc_failed_handler) ();
294   h->chunk = new_chunk;
295   new_chunk->prev = old_chunk;
296   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
297
298   /* Move the existing object to the new chunk.
299      Word at a time is fast and is safe if the object
300      is sufficiently aligned.  */
301   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
302     {
303       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
304            i >= 0; i--)
305         ((COPYING_UNIT *)new_chunk->contents)[i]
306           = ((COPYING_UNIT *)h->object_base)[i];
307       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
308          but that can cross a page boundary on a machine
309          which does not do strict alignment for COPYING_UNITS.  */
310       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
311     }
312   else
313     already = 0;
314   /* Copy remaining bytes one by one.  */
315   for (i = already; i < obj_size; i++)
316     new_chunk->contents[i] = h->object_base[i];
317
318   /* If the object just copied was the only data in OLD_CHUNK,
319      free that chunk and remove it from the chain.
320      But not if that chunk might contain an empty object.  */
321   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
322     {
323       new_chunk->prev = old_chunk->prev;
324       CALL_FREEFUN (h, old_chunk);
325     }
326
327   h->object_base = new_chunk->contents;
328   h->next_free = h->object_base + obj_size;
329   /* The new chunk certainly contains no empty object yet.  */
330   h->maybe_empty_object = 0;
331 }
332
333 /* Return nonzero if object OBJ has been allocated from obstack H.
334    This is here for debugging.
335    If you use it in a program, you are probably losing.  */
336
337 #if defined (__STDC__) && __STDC__
338 /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
339    obstack.h because it is just for debugging.  */
340 int _obstack_allocated_p (struct obstack *h, POINTER obj);
341 #endif
342
343 int
344 _obstack_allocated_p (h, obj)
345      struct obstack *h;
346      POINTER obj;
347 {
348   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
349   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
350
351   lp = (h)->chunk;
352   /* We use >= rather than > since the object cannot be exactly at
353      the beginning of the chunk but might be an empty object exactly
354      at the end of an adjacent chunk.  */
355   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
356     {
357       plp = lp->prev;
358       lp = plp;
359     }
360   return lp != 0;
361 }
362
363 /* Free objects in obstack H, including OBJ and everything allocate
364    more recently than OBJ.  If OBJ is zero, free everything in H.  */
365
366 #undef obstack_free
367
368 /* This function has two names with identical definitions.
369    This is the first one, called from non-ANSI code.  */
370
371 void
372 _obstack_free (h, obj)
373      struct obstack *h;
374      POINTER obj;
375 {
376   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
377   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
378
379   lp = h->chunk;
380   /* We use >= because there cannot be an object at the beginning of a chunk.
381      But there can be an empty object at that address
382      at the end of another chunk.  */
383   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
384     {
385       plp = lp->prev;
386       CALL_FREEFUN (h, lp);
387       lp = plp;
388       /* If we switch chunks, we can't tell whether the new current
389          chunk contains an empty object, so assume that it may.  */
390       h->maybe_empty_object = 1;
391     }
392   if (lp)
393     {
394       h->object_base = h->next_free = (char *) (obj);
395       h->chunk_limit = lp->limit;
396       h->chunk = lp;
397     }
398   else if (obj != 0)
399     /* obj is not in any of the chunks! */
400     abort ();
401 }
402
403 /* This function is used from ANSI code.  */
404
405 void
406 obstack_free (h, obj)
407      struct obstack *h;
408      POINTER obj;
409 {
410   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
411   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
412
413   lp = h->chunk;
414   /* We use >= because there cannot be an object at the beginning of a chunk.
415      But there can be an empty object at that address
416      at the end of another chunk.  */
417   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
418     {
419       plp = lp->prev;
420       CALL_FREEFUN (h, lp);
421       lp = plp;
422       /* If we switch chunks, we can't tell whether the new current
423          chunk contains an empty object, so assume that it may.  */
424       h->maybe_empty_object = 1;
425     }
426   if (lp)
427     {
428       h->object_base = h->next_free = (char *) (obj);
429       h->chunk_limit = lp->limit;
430       h->chunk = lp;
431     }
432   else if (obj != 0)
433     /* obj is not in any of the chunks! */
434     abort ();
435 }
436
437 int
438 _obstack_memory_used (h)
439      struct obstack *h;
440 {
441   register struct _obstack_chunk* lp;
442   register int nbytes = 0;
443
444   for (lp = h->chunk; lp != 0; lp = lp->prev)
445     {
446       nbytes += lp->limit - (char *) lp;
447     }
448   return nbytes;
449 }
450
451 /* Define the error handler.  */
452 #ifndef _
453 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
454 #  include <libintl.h>
455 #  ifndef _
456 #   define _(Str) gettext (Str)
457 #  endif
458 # else
459 #  define _(Str) (Str)
460 # endif
461 #endif
462
463 static void
464 print_and_abort ()
465 {
466   fputs (_("memory exhausted\n"), stderr);
467   exit (obstack_exit_failure);
468 }
469
470 #if 0
471 /* These are now turned off because the applications do not use it
472    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
473
474 /* Now define the functional versions of the obstack macros.
475    Define them to simply use the corresponding macros to do the job.  */
476
477 #if defined (__STDC__) && __STDC__
478 /* These function definitions do not work with non-ANSI preprocessors;
479    they won't pass through the macro names in parentheses.  */
480
481 /* The function names appear in parentheses in order to prevent
482    the macro-definitions of the names from being expanded there.  */
483
484 POINTER (obstack_base) (obstack)
485      struct obstack *obstack;
486 {
487   return obstack_base (obstack);
488 }
489
490 POINTER (obstack_next_free) (obstack)
491      struct obstack *obstack;
492 {
493   return obstack_next_free (obstack);
494 }
495
496 int (obstack_object_size) (obstack)
497      struct obstack *obstack;
498 {
499   return obstack_object_size (obstack);
500 }
501
502 int (obstack_room) (obstack)
503      struct obstack *obstack;
504 {
505   return obstack_room (obstack);
506 }
507
508 int (obstack_make_room) (obstack, length)
509      struct obstack *obstack;
510      int length;
511 {
512   return obstack_make_room (obstack, length);
513 }
514
515 void (obstack_grow) (obstack, pointer, length)
516      struct obstack *obstack;
517      POINTER pointer;
518      int length;
519 {
520   obstack_grow (obstack, pointer, length);
521 }
522
523 void (obstack_grow0) (obstack, pointer, length)
524      struct obstack *obstack;
525      POINTER pointer;
526      int length;
527 {
528   obstack_grow0 (obstack, pointer, length);
529 }
530
531 void (obstack_1grow) (obstack, character)
532      struct obstack *obstack;
533      int character;
534 {
535   obstack_1grow (obstack, character);
536 }
537
538 void (obstack_blank) (obstack, length)
539      struct obstack *obstack;
540      int length;
541 {
542   obstack_blank (obstack, length);
543 }
544
545 void (obstack_1grow_fast) (obstack, character)
546      struct obstack *obstack;
547      int character;
548 {
549   obstack_1grow_fast (obstack, character);
550 }
551
552 void (obstack_blank_fast) (obstack, length)
553      struct obstack *obstack;
554      int length;
555 {
556   obstack_blank_fast (obstack, length);
557 }
558
559 POINTER (obstack_finish) (obstack)
560      struct obstack *obstack;
561 {
562   return obstack_finish (obstack);
563 }
564
565 POINTER (obstack_alloc) (obstack, length)
566      struct obstack *obstack;
567      int length;
568 {
569   return obstack_alloc (obstack, length);
570 }
571
572 POINTER (obstack_copy) (obstack, pointer, length)
573      struct obstack *obstack;
574      POINTER pointer;
575      int length;
576 {
577   return obstack_copy (obstack, pointer, length);
578 }
579
580 POINTER (obstack_copy0) (obstack, pointer, length)
581      struct obstack *obstack;
582      POINTER pointer;
583      int length;
584 {
585   return obstack_copy0 (obstack, pointer, length);
586 }
587
588 #endif /* __STDC__ */
589
590 #endif /* 0 */
591
592 #endif  /* !ELIDE_CODE */