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