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