becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / obstack / obstack.c
1 /* obstack.c - subroutines used implicitly by object stack macros
2    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20 #include <config.h>
21
22 #include "obstack.h"
23
24 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
25    incremented whenever callers compiled using an old obstack.h can no
26    longer properly call the functions in this obstack.c.  */
27 #define OBSTACK_INTERFACE_VERSION 1
28
29 #include <stdio.h>
30 #include <stddef.h>
31 #include <stdint.h>
32
33 /* Determine default alignment.  */
34 union fooround
35 {
36   uintmax_t i;
37   long double d;
38   void *p;
39 };
40 struct fooalign
41 {
42   char c;
43   union fooround u;
44 };
45 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
46    But in fact it might be less smart and round addresses to as much as
47    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
48 enum
49   {
50     DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
51     DEFAULT_ROUNDING = sizeof (union fooround)
52   };
53
54 /* When we copy a long block of data, this is the unit to do it with.
55    On some machines, copying successive ints does not work;
56    in such a case, redefine COPYING_UNIT to `long' (if that works)
57    or `char' as a last resort.  */
58 # ifndef COPYING_UNIT
59 #  define COPYING_UNIT int
60 # endif
61
62
63 /* The functions allocating more room by calling `obstack_chunk_alloc'
64    jump to the handler pointed to by `obstack_alloc_failed_handler'.
65    This can be set to a user defined function which should either
66    abort gracefully or use longjump - but shouldn't return.  This
67    variable by default points to the internal function
68    `print_and_abort'.  */
69 static void print_and_abort (void);
70 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
71
72 /* Exit value used when `print_and_abort' is used.  */
73 # include <stdlib.h>
74 int obstack_exit_failure = EXIT_FAILURE;
75
76 /* Define a macro that either calls functions with the traditional malloc/free
77    calling interface, or calls functions with the mmalloc/mfree interface
78    (that adds an extra first argument), based on the state of use_extra_arg.
79    For free, do not use ?:, since some compilers, like the MIPS compilers,
80    do not allow (expr) ? void : void.  */
81
82 # define CALL_CHUNKFUN(h, size) \
83   (((h) -> use_extra_arg) \
84    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
85    : (*(struct _obstack_chunk *(*) (PTR_INT_TYPE)) (h)->chunkfun) ((size)))
86
87 # define CALL_FREEFUN(h, old_chunk) \
88   do { \
89     if ((h) -> use_extra_arg) \
90       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
91     else \
92       (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
93   } while (0)
94
95
96 /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
97    Objects start on multiples of ALIGNMENT (0 means use default).
98    CHUNKFUN is the function to use to allocate chunks,
99    and FREEFUN the function to free them.
100
101    Return nonzero if successful, calls obstack_alloc_failed_handler if
102    allocation fails.  */
103
104 int _obstack_begin(struct obstack *h, int size, int alignment,
105                    void *(*chunkfun)(PTR_INT_TYPE), void (*freefun)(void *))
106 {
107   register struct _obstack_chunk *chunk; /* points to new chunk */
108
109   if (alignment == 0)
110     alignment = DEFAULT_ALIGNMENT;
111   if (size == 0)
112     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
113     {
114       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
115          Use the values for range checking, because if range checking is off,
116          the extra bytes won't be missed terribly, but if range checking is on
117          and we used a larger request, a whole extra 4096 bytes would be
118          allocated.
119
120          These number are irrelevant to the new GNU malloc.  I suspect it is
121          less sensitive to the size of the request.  */
122       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
123                     + 4 + DEFAULT_ROUNDING - 1)
124                    & ~(DEFAULT_ROUNDING - 1));
125       size = 4096 - extra;
126     }
127
128   h->chunkfun = (struct _obstack_chunk * (*)(void *, PTR_INT_TYPE)) chunkfun;
129   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
130   h->chunk_size = size;
131   h->alignment_mask = alignment - 1;
132   h->use_extra_arg = 0;
133
134   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
135   if (!chunk)
136     (*obstack_alloc_failed_handler) ();
137   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
138                                                alignment - 1);
139   h->chunk_limit = chunk->limit
140     = (char *) chunk + h->chunk_size;
141   chunk->prev = 0;
142   /* The initial chunk now contains no empty object.  */
143   h->maybe_empty_object = 0;
144   h->alloc_failed = 0;
145   return 1;
146 }
147
148 int _obstack_begin_1(struct obstack *h, int size, int alignment,
149                      void *(*chunkfun) (void *, PTR_INT_TYPE),
150                      void (*freefun) (void *, void *), void *arg)
151 {
152   register struct _obstack_chunk *chunk; /* points to new chunk */
153
154   if (alignment == 0)
155     alignment = DEFAULT_ALIGNMENT;
156   if (size == 0)
157     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
158     {
159       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
160          Use the values for range checking, because if range checking is off,
161          the extra bytes won't be missed terribly, but if range checking is on
162          and we used a larger request, a whole extra 4096 bytes would be
163          allocated.
164
165          These number are irrelevant to the new GNU malloc.  I suspect it is
166          less sensitive to the size of the request.  */
167       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
168                     + 4 + DEFAULT_ROUNDING - 1)
169                    & ~(DEFAULT_ROUNDING - 1));
170       size = 4096 - extra;
171     }
172
173   h->chunkfun = (struct _obstack_chunk * (*)(void *,PTR_INT_TYPE)) chunkfun;
174   h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
175   h->chunk_size = size;
176   h->alignment_mask = alignment - 1;
177   h->extra_arg = arg;
178   h->use_extra_arg = 1;
179
180   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
181   if (!chunk)
182     (*obstack_alloc_failed_handler) ();
183   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
184                                                alignment - 1);
185   h->chunk_limit = chunk->limit
186     = (char *) chunk + h->chunk_size;
187   chunk->prev = 0;
188   /* The initial chunk now contains no empty object.  */
189   h->maybe_empty_object = 0;
190   h->alloc_failed = 0;
191   return 1;
192 }
193
194 /* Allocate a new current chunk for the obstack *H
195    on the assumption that LENGTH bytes need to be added
196    to the current object, or a new object of length LENGTH allocated.
197    Copies any partial object from the end of the old chunk
198    to the beginning of the new one.  */
199
200 void _obstack_newchunk(struct obstack *h, PTR_INT_TYPE length)
201 {
202   register struct _obstack_chunk *old_chunk = h->chunk;
203   register struct _obstack_chunk *new_chunk;
204   register PTR_INT_TYPE new_size;
205   register PTR_INT_TYPE obj_size = h->next_free - h->object_base;
206   register PTR_INT_TYPE i;
207   PTR_INT_TYPE already;
208   char *object_base;
209
210   /* Compute size for new chunk.  */
211   new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
212   if (new_size < h->chunk_size)
213     new_size = h->chunk_size;
214
215   /* Allocate and initialize the new chunk.  */
216   new_chunk = CALL_CHUNKFUN (h, new_size);
217   if (!new_chunk)
218     (*obstack_alloc_failed_handler) ();
219   h->chunk = new_chunk;
220   new_chunk->prev = old_chunk;
221   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
222
223   /* Compute an aligned object_base in the new chunk */
224   object_base =
225     __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
226
227   /* Move the existing object to the new chunk.
228      Word at a time is fast and is safe if the object
229      is sufficiently aligned.  */
230   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
231     {
232       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
233            i >= 0; i--)
234         ((COPYING_UNIT *)object_base)[i]
235           = ((COPYING_UNIT *)h->object_base)[i];
236       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
237          but that can cross a page boundary on a machine
238          which does not do strict alignment for COPYING_UNITS.  */
239       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
240     }
241   else
242     already = 0;
243   /* Copy remaining bytes one by one.  */
244   for (i = already; i < obj_size; i++)
245     object_base[i] = h->object_base[i];
246
247   /* If the object just copied was the only data in OLD_CHUNK,
248      free that chunk and remove it from the chain.
249      But not if that chunk might contain an empty object.  */
250   if (! h->maybe_empty_object
251       && (h->object_base
252           == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
253                           h->alignment_mask)))
254     {
255       new_chunk->prev = old_chunk->prev;
256       CALL_FREEFUN (h, old_chunk);
257     }
258
259   h->object_base = object_base;
260   h->next_free = h->object_base + obj_size;
261   /* The new chunk certainly contains no empty object yet.  */
262   h->maybe_empty_object = 0;
263 }
264
265 /* Return nonzero if object OBJ has been allocated from obstack H.
266    This is here for debugging.
267    If you use it in a program, you are probably losing.  */
268
269 /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
270    obstack.h because it is just for debugging.  */
271 int _obstack_allocated_p (struct obstack *h, void *obj);
272
273 int _obstack_allocated_p(struct obstack *h, void *obj)
274 {
275   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
276   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
277
278   lp = (h)->chunk;
279   /* We use >= rather than > since the object cannot be exactly at
280      the beginning of the chunk but might be an empty object exactly
281      at the end of an adjacent chunk.  */
282   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
283     {
284       plp = lp->prev;
285       lp = plp;
286     }
287   return lp != 0;
288 }
289
290 /* Free objects in obstack H, including OBJ and everything allocate
291    more recently than OBJ.  If OBJ is zero, free everything in H.  */
292
293 # undef obstack_free
294
295 void obstack_free(struct obstack *h, void *obj)
296 {
297   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
298   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
299
300   lp = h->chunk;
301   /* We use >= because there cannot be an object at the beginning of a chunk.
302      But there can be an empty object at that address
303      at the end of another chunk.  */
304   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
305     {
306       plp = lp->prev;
307       CALL_FREEFUN (h, lp);
308       lp = plp;
309       /* If we switch chunks, we can't tell whether the new current
310          chunk contains an empty object, so assume that it may.  */
311       h->maybe_empty_object = 1;
312     }
313   if (lp)
314     {
315       h->object_base = h->next_free = (char *) (obj);
316       h->chunk_limit = lp->limit;
317       h->chunk = lp;
318     }
319   else if (obj != 0)
320     /* obj is not in any of the chunks! */
321     abort ();
322 }
323
324 PTR_INT_TYPE _obstack_memory_used(struct obstack *h)
325 {
326   register struct _obstack_chunk* lp;
327   register PTR_INT_TYPE nbytes = 0;
328
329   for (lp = h->chunk; lp != 0; lp = lp->prev)
330     {
331       nbytes += lp->limit - (char *) lp;
332     }
333   return nbytes;
334 }
335
336 static void __attribute__((noreturn)) print_and_abort(void)
337 {
338   /* Don't change any of these strings.  Yes, it would be possible to add
339      the newline to the string and use fputs or so.  But this must not
340      happen because the "memory exhausted" message appears in other places
341      like this and the translation should be reused instead of creating
342      a very similar string which requires a separate translation.  */
343   fprintf (stderr, "%s\n", "memory exhausted");
344   exit (obstack_exit_failure);
345 }