xref: /aosp_15_r20/external/obstack/libiberty/obstack.c (revision 6912a2bee9c91e2e65d305b4d8eb5813eb26cd9f)
1*6912a2beSHONG Yifan /* obstack.c - subroutines used implicitly by object stack macros
2*6912a2beSHONG Yifan    Copyright (C) 1988-2022 Free Software Foundation, Inc.
3*6912a2beSHONG Yifan    This file is part of the GNU C Library.
4*6912a2beSHONG Yifan 
5*6912a2beSHONG Yifan    The GNU C Library is free software; you can redistribute it and/or
6*6912a2beSHONG Yifan    modify it under the terms of the GNU Lesser General Public
7*6912a2beSHONG Yifan    License as published by the Free Software Foundation; either
8*6912a2beSHONG Yifan    version 2.1 of the License, or (at your option) any later version.
9*6912a2beSHONG Yifan 
10*6912a2beSHONG Yifan    The GNU C Library is distributed in the hope that it will be useful,
11*6912a2beSHONG Yifan    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*6912a2beSHONG Yifan    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*6912a2beSHONG Yifan    Lesser General Public License for more details.
14*6912a2beSHONG Yifan 
15*6912a2beSHONG Yifan    You should have received a copy of the GNU Lesser General Public
16*6912a2beSHONG Yifan    License along with the GNU C Library; if not, see
17*6912a2beSHONG Yifan    <http://www.gnu.org/licenses/>.  */
18*6912a2beSHONG Yifan 
19*6912a2beSHONG Yifan 
20*6912a2beSHONG Yifan #ifdef _LIBC
21*6912a2beSHONG Yifan # include <obstack.h>
22*6912a2beSHONG Yifan #else
23*6912a2beSHONG Yifan # include <config.h>
24*6912a2beSHONG Yifan # include "obstack.h"
25*6912a2beSHONG Yifan #endif
26*6912a2beSHONG Yifan 
27*6912a2beSHONG Yifan /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
28*6912a2beSHONG Yifan    obstack.h must be incremented whenever callers compiled using an old
29*6912a2beSHONG Yifan    obstack.h can no longer properly call the functions in this file.  */
30*6912a2beSHONG Yifan 
31*6912a2beSHONG Yifan /* Comment out all this code if we are using the GNU C Library, and are not
32*6912a2beSHONG Yifan    actually compiling the library itself, and the installed library
33*6912a2beSHONG Yifan    supports the same library interface we do.  This code is part of the GNU
34*6912a2beSHONG Yifan    C Library, but also included in many other GNU distributions.  Compiling
35*6912a2beSHONG Yifan    and linking in this code is a waste when using the GNU C library
36*6912a2beSHONG Yifan    (especially if it is a shared library).  Rather than having every GNU
37*6912a2beSHONG Yifan    program understand 'configure --with-gnu-libc' and omit the object
38*6912a2beSHONG Yifan    files, it is simpler to just do this in the source for each such file.  */
39*6912a2beSHONG Yifan #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
40*6912a2beSHONG Yifan # include <gnu-versions.h>
41*6912a2beSHONG Yifan # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION	      \
42*6912a2beSHONG Yifan       || (_GNU_OBSTACK_INTERFACE_VERSION == 1				      \
43*6912a2beSHONG Yifan           && _OBSTACK_INTERFACE_VERSION == 2				      \
44*6912a2beSHONG Yifan           && defined SIZEOF_INT && defined SIZEOF_SIZE_T		      \
45*6912a2beSHONG Yifan           && SIZEOF_INT == SIZEOF_SIZE_T))
46*6912a2beSHONG Yifan #  define _OBSTACK_ELIDE_CODE
47*6912a2beSHONG Yifan # endif
48*6912a2beSHONG Yifan #endif
49*6912a2beSHONG Yifan 
50*6912a2beSHONG Yifan #ifndef _OBSTACK_ELIDE_CODE
51*6912a2beSHONG Yifan /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
52*6912a2beSHONG Yifan    use the already-supplied __alignof__.  Otherwise, this must be Gnulib
53*6912a2beSHONG Yifan    (as glibc assumes GCC); defer to Gnulib's alignof_type.  */
54*6912a2beSHONG Yifan # if !defined __GNUC__ && !defined __IBM__ALIGNOF__ && !defined __alignof__
55*6912a2beSHONG Yifan #  if defined __cplusplus
56*6912a2beSHONG Yifan template <class type> struct alignof_helper { char __slot1; type __slot2; };
57*6912a2beSHONG Yifan #   define __alignof__(type) offsetof (alignof_helper<type>, __slot2)
58*6912a2beSHONG Yifan #  else
59*6912a2beSHONG Yifan #   define __alignof__(type)						      \
60*6912a2beSHONG Yifan   offsetof (struct { char __slot1; type __slot2; }, __slot2)
61*6912a2beSHONG Yifan #  endif
62*6912a2beSHONG Yifan # endif
63*6912a2beSHONG Yifan # include <stdlib.h>
64*6912a2beSHONG Yifan # include <stdint.h>
65*6912a2beSHONG Yifan 
66*6912a2beSHONG Yifan # ifndef MAX
67*6912a2beSHONG Yifan #  define MAX(a,b) ((a) > (b) ? (a) : (b))
68*6912a2beSHONG Yifan # endif
69*6912a2beSHONG Yifan 
70*6912a2beSHONG Yifan /* Determine default alignment.  */
71*6912a2beSHONG Yifan 
72*6912a2beSHONG Yifan /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
73*6912a2beSHONG Yifan    But in fact it might be less smart and round addresses to as much as
74*6912a2beSHONG Yifan    DEFAULT_ROUNDING.  So we prepare for it to do that.
75*6912a2beSHONG Yifan 
76*6912a2beSHONG Yifan    DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h.  */
77*6912a2beSHONG Yifan #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double),		      \
78*6912a2beSHONG Yifan                                MAX (__alignof__ (uintmax_t),		      \
79*6912a2beSHONG Yifan                                     __alignof__ (void *)))
80*6912a2beSHONG Yifan #define DEFAULT_ROUNDING MAX (sizeof (long double),			      \
81*6912a2beSHONG Yifan                                MAX (sizeof (uintmax_t),			      \
82*6912a2beSHONG Yifan                                     sizeof (void *)))
83*6912a2beSHONG Yifan 
84*6912a2beSHONG Yifan /* Call functions with either the traditional malloc/free calling
85*6912a2beSHONG Yifan    interface, or the mmalloc/mfree interface (that adds an extra first
86*6912a2beSHONG Yifan    argument), based on the value of use_extra_arg.  */
87*6912a2beSHONG Yifan 
88*6912a2beSHONG Yifan static void *
call_chunkfun(struct obstack * h,size_t size)89*6912a2beSHONG Yifan call_chunkfun (struct obstack *h, size_t size)
90*6912a2beSHONG Yifan {
91*6912a2beSHONG Yifan   if (h->use_extra_arg)
92*6912a2beSHONG Yifan     return h->chunkfun.extra (h->extra_arg, size);
93*6912a2beSHONG Yifan   else
94*6912a2beSHONG Yifan     return h->chunkfun.plain (size);
95*6912a2beSHONG Yifan }
96*6912a2beSHONG Yifan 
97*6912a2beSHONG Yifan static void
call_freefun(struct obstack * h,void * old_chunk)98*6912a2beSHONG Yifan call_freefun (struct obstack *h, void *old_chunk)
99*6912a2beSHONG Yifan {
100*6912a2beSHONG Yifan   if (h->use_extra_arg)
101*6912a2beSHONG Yifan     h->freefun.extra (h->extra_arg, old_chunk);
102*6912a2beSHONG Yifan   else
103*6912a2beSHONG Yifan     h->freefun.plain (old_chunk);
104*6912a2beSHONG Yifan }
105*6912a2beSHONG Yifan 
106*6912a2beSHONG Yifan 
107*6912a2beSHONG Yifan /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
108*6912a2beSHONG Yifan    Objects start on multiples of ALIGNMENT (0 means use default).
109*6912a2beSHONG Yifan 
110*6912a2beSHONG Yifan    Return nonzero if successful, calls obstack_alloc_failed_handler if
111*6912a2beSHONG Yifan    allocation fails.  */
112*6912a2beSHONG Yifan 
113*6912a2beSHONG Yifan static int
_obstack_begin_worker(struct obstack * h,_OBSTACK_SIZE_T size,_OBSTACK_SIZE_T alignment)114*6912a2beSHONG Yifan _obstack_begin_worker (struct obstack *h,
115*6912a2beSHONG Yifan                        _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
116*6912a2beSHONG Yifan {
117*6912a2beSHONG Yifan   struct _obstack_chunk *chunk; /* points to new chunk */
118*6912a2beSHONG Yifan 
119*6912a2beSHONG Yifan   if (alignment == 0)
120*6912a2beSHONG Yifan     alignment = DEFAULT_ALIGNMENT;
121*6912a2beSHONG Yifan   if (size == 0)
122*6912a2beSHONG Yifan     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
123*6912a2beSHONG Yifan     {
124*6912a2beSHONG Yifan       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
125*6912a2beSHONG Yifan          Use the values for range checking, because if range checking is off,
126*6912a2beSHONG Yifan          the extra bytes won't be missed terribly, but if range checking is on
127*6912a2beSHONG Yifan          and we used a larger request, a whole extra 4096 bytes would be
128*6912a2beSHONG Yifan          allocated.
129*6912a2beSHONG Yifan 
130*6912a2beSHONG Yifan          These number are irrelevant to the new GNU malloc.  I suspect it is
131*6912a2beSHONG Yifan          less sensitive to the size of the request.  */
132*6912a2beSHONG Yifan       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
133*6912a2beSHONG Yifan                     + 4 + DEFAULT_ROUNDING - 1)
134*6912a2beSHONG Yifan                    & ~(DEFAULT_ROUNDING - 1));
135*6912a2beSHONG Yifan       size = 4096 - extra;
136*6912a2beSHONG Yifan     }
137*6912a2beSHONG Yifan 
138*6912a2beSHONG Yifan   h->chunk_size = size;
139*6912a2beSHONG Yifan   h->alignment_mask = alignment - 1;
140*6912a2beSHONG Yifan 
141*6912a2beSHONG Yifan   chunk = (struct _obstack_chunk *) call_chunkfun (h, h->chunk_size);
142*6912a2beSHONG Yifan   if (!chunk)
143*6912a2beSHONG Yifan     (*obstack_alloc_failed_handler) ();
144*6912a2beSHONG Yifan   h->chunk = chunk;
145*6912a2beSHONG Yifan   h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
146*6912a2beSHONG Yifan                                                alignment - 1);
147*6912a2beSHONG Yifan   h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
148*6912a2beSHONG Yifan   chunk->prev = 0;
149*6912a2beSHONG Yifan   /* The initial chunk now contains no empty object.  */
150*6912a2beSHONG Yifan   h->maybe_empty_object = 0;
151*6912a2beSHONG Yifan   h->alloc_failed = 0;
152*6912a2beSHONG Yifan   return 1;
153*6912a2beSHONG Yifan }
154*6912a2beSHONG Yifan 
155*6912a2beSHONG Yifan int
_obstack_begin(struct obstack * h,_OBSTACK_SIZE_T size,_OBSTACK_SIZE_T alignment,void * (* chunkfun)(size_t),void (* freefun)(void *))156*6912a2beSHONG Yifan _obstack_begin (struct obstack *h,
157*6912a2beSHONG Yifan                 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
158*6912a2beSHONG Yifan                 void *(*chunkfun) (size_t),
159*6912a2beSHONG Yifan                 void (*freefun) (void *))
160*6912a2beSHONG Yifan {
161*6912a2beSHONG Yifan   h->chunkfun.plain = chunkfun;
162*6912a2beSHONG Yifan   h->freefun.plain = freefun;
163*6912a2beSHONG Yifan   h->use_extra_arg = 0;
164*6912a2beSHONG Yifan   return _obstack_begin_worker (h, size, alignment);
165*6912a2beSHONG Yifan }
166*6912a2beSHONG Yifan 
167*6912a2beSHONG Yifan int
_obstack_begin_1(struct obstack * h,_OBSTACK_SIZE_T size,_OBSTACK_SIZE_T alignment,void * (* chunkfun)(void *,size_t),void (* freefun)(void *,void *),void * arg)168*6912a2beSHONG Yifan _obstack_begin_1 (struct obstack *h,
169*6912a2beSHONG Yifan                   _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
170*6912a2beSHONG Yifan                   void *(*chunkfun) (void *, size_t),
171*6912a2beSHONG Yifan                   void (*freefun) (void *, void *),
172*6912a2beSHONG Yifan                   void *arg)
173*6912a2beSHONG Yifan {
174*6912a2beSHONG Yifan   h->chunkfun.extra = chunkfun;
175*6912a2beSHONG Yifan   h->freefun.extra = freefun;
176*6912a2beSHONG Yifan   h->extra_arg = arg;
177*6912a2beSHONG Yifan   h->use_extra_arg = 1;
178*6912a2beSHONG Yifan   return _obstack_begin_worker (h, size, alignment);
179*6912a2beSHONG Yifan }
180*6912a2beSHONG Yifan 
181*6912a2beSHONG Yifan /* Allocate a new current chunk for the obstack *H
182*6912a2beSHONG Yifan    on the assumption that LENGTH bytes need to be added
183*6912a2beSHONG Yifan    to the current object, or a new object of length LENGTH allocated.
184*6912a2beSHONG Yifan    Copies any partial object from the end of the old chunk
185*6912a2beSHONG Yifan    to the beginning of the new one.  */
186*6912a2beSHONG Yifan 
187*6912a2beSHONG Yifan void
_obstack_newchunk(struct obstack * h,_OBSTACK_SIZE_T length)188*6912a2beSHONG Yifan _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
189*6912a2beSHONG Yifan {
190*6912a2beSHONG Yifan   struct _obstack_chunk *old_chunk = h->chunk;
191*6912a2beSHONG Yifan   struct _obstack_chunk *new_chunk = 0;
192*6912a2beSHONG Yifan   size_t obj_size = h->next_free - h->object_base;
193*6912a2beSHONG Yifan   char *object_base;
194*6912a2beSHONG Yifan 
195*6912a2beSHONG Yifan   /* Compute size for new chunk.  */
196*6912a2beSHONG Yifan   size_t sum1 = obj_size + length;
197*6912a2beSHONG Yifan   size_t sum2 = sum1 + h->alignment_mask;
198*6912a2beSHONG Yifan   size_t new_size = sum2 + (obj_size >> 3) + 100;
199*6912a2beSHONG Yifan   if (new_size < sum2)
200*6912a2beSHONG Yifan     new_size = sum2;
201*6912a2beSHONG Yifan   if (new_size < h->chunk_size)
202*6912a2beSHONG Yifan     new_size = h->chunk_size;
203*6912a2beSHONG Yifan 
204*6912a2beSHONG Yifan   /* Allocate and initialize the new chunk.  */
205*6912a2beSHONG Yifan   if (obj_size <= sum1 && sum1 <= sum2)
206*6912a2beSHONG Yifan     new_chunk = (struct _obstack_chunk *) call_chunkfun (h, new_size);
207*6912a2beSHONG Yifan   if (!new_chunk)
208*6912a2beSHONG Yifan     (*obstack_alloc_failed_handler)();
209*6912a2beSHONG Yifan   h->chunk = new_chunk;
210*6912a2beSHONG Yifan   new_chunk->prev = old_chunk;
211*6912a2beSHONG Yifan   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
212*6912a2beSHONG Yifan 
213*6912a2beSHONG Yifan   /* Compute an aligned object_base in the new chunk */
214*6912a2beSHONG Yifan   object_base =
215*6912a2beSHONG Yifan     __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
216*6912a2beSHONG Yifan 
217*6912a2beSHONG Yifan   /* Move the existing object to the new chunk.  */
218*6912a2beSHONG Yifan   memcpy (object_base, h->object_base, obj_size);
219*6912a2beSHONG Yifan 
220*6912a2beSHONG Yifan   /* If the object just copied was the only data in OLD_CHUNK,
221*6912a2beSHONG Yifan      free that chunk and remove it from the chain.
222*6912a2beSHONG Yifan      But not if that chunk might contain an empty object.  */
223*6912a2beSHONG Yifan   if (!h->maybe_empty_object
224*6912a2beSHONG Yifan       && (h->object_base
225*6912a2beSHONG Yifan           == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
226*6912a2beSHONG Yifan                           h->alignment_mask)))
227*6912a2beSHONG Yifan     {
228*6912a2beSHONG Yifan       new_chunk->prev = old_chunk->prev;
229*6912a2beSHONG Yifan       call_freefun (h, old_chunk);
230*6912a2beSHONG Yifan     }
231*6912a2beSHONG Yifan 
232*6912a2beSHONG Yifan   h->object_base = object_base;
233*6912a2beSHONG Yifan   h->next_free = h->object_base + obj_size;
234*6912a2beSHONG Yifan   /* The new chunk certainly contains no empty object yet.  */
235*6912a2beSHONG Yifan   h->maybe_empty_object = 0;
236*6912a2beSHONG Yifan }
237*6912a2beSHONG Yifan 
238*6912a2beSHONG Yifan /* Return nonzero if object OBJ has been allocated from obstack H.
239*6912a2beSHONG Yifan    This is here for debugging.
240*6912a2beSHONG Yifan    If you use it in a program, you are probably losing.  */
241*6912a2beSHONG Yifan 
242*6912a2beSHONG Yifan /* Suppress -Wmissing-prototypes warning.  We don't want to declare this in
243*6912a2beSHONG Yifan    obstack.h because it is just for debugging.  */
244*6912a2beSHONG Yifan int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
245*6912a2beSHONG Yifan 
246*6912a2beSHONG Yifan int
_obstack_allocated_p(struct obstack * h,void * obj)247*6912a2beSHONG Yifan _obstack_allocated_p (struct obstack *h, void *obj)
248*6912a2beSHONG Yifan {
249*6912a2beSHONG Yifan   struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
250*6912a2beSHONG Yifan   struct _obstack_chunk *plp;   /* point to previous chunk if any */
251*6912a2beSHONG Yifan 
252*6912a2beSHONG Yifan   lp = (h)->chunk;
253*6912a2beSHONG Yifan   /* We use >= rather than > since the object cannot be exactly at
254*6912a2beSHONG Yifan      the beginning of the chunk but might be an empty object exactly
255*6912a2beSHONG Yifan      at the end of an adjacent chunk.  */
256*6912a2beSHONG Yifan   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
257*6912a2beSHONG Yifan     {
258*6912a2beSHONG Yifan       plp = lp->prev;
259*6912a2beSHONG Yifan       lp = plp;
260*6912a2beSHONG Yifan     }
261*6912a2beSHONG Yifan   return lp != 0;
262*6912a2beSHONG Yifan }
263*6912a2beSHONG Yifan 
264*6912a2beSHONG Yifan /* Free objects in obstack H, including OBJ and everything allocate
265*6912a2beSHONG Yifan    more recently than OBJ.  If OBJ is zero, free everything in H.  */
266*6912a2beSHONG Yifan 
267*6912a2beSHONG Yifan void
_obstack_free(struct obstack * h,void * obj)268*6912a2beSHONG Yifan _obstack_free (struct obstack *h, void *obj)
269*6912a2beSHONG Yifan {
270*6912a2beSHONG Yifan   struct _obstack_chunk *lp;    /* below addr of any objects in this chunk */
271*6912a2beSHONG Yifan   struct _obstack_chunk *plp;   /* point to previous chunk if any */
272*6912a2beSHONG Yifan 
273*6912a2beSHONG Yifan   lp = h->chunk;
274*6912a2beSHONG Yifan   /* We use >= because there cannot be an object at the beginning of a chunk.
275*6912a2beSHONG Yifan      But there can be an empty object at that address
276*6912a2beSHONG Yifan      at the end of another chunk.  */
277*6912a2beSHONG Yifan   while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
278*6912a2beSHONG Yifan     {
279*6912a2beSHONG Yifan       plp = lp->prev;
280*6912a2beSHONG Yifan       call_freefun (h, lp);
281*6912a2beSHONG Yifan       lp = plp;
282*6912a2beSHONG Yifan       /* If we switch chunks, we can't tell whether the new current
283*6912a2beSHONG Yifan          chunk contains an empty object, so assume that it may.  */
284*6912a2beSHONG Yifan       h->maybe_empty_object = 1;
285*6912a2beSHONG Yifan     }
286*6912a2beSHONG Yifan   if (lp)
287*6912a2beSHONG Yifan     {
288*6912a2beSHONG Yifan       h->object_base = h->next_free = (char *) (obj);
289*6912a2beSHONG Yifan       h->chunk_limit = lp->limit;
290*6912a2beSHONG Yifan       h->chunk = lp;
291*6912a2beSHONG Yifan     }
292*6912a2beSHONG Yifan   else if (obj != 0)
293*6912a2beSHONG Yifan     /* obj is not in any of the chunks! */
294*6912a2beSHONG Yifan     abort ();
295*6912a2beSHONG Yifan }
296*6912a2beSHONG Yifan 
297*6912a2beSHONG Yifan _OBSTACK_SIZE_T
_obstack_memory_used(struct obstack * h)298*6912a2beSHONG Yifan _obstack_memory_used (struct obstack *h)
299*6912a2beSHONG Yifan {
300*6912a2beSHONG Yifan   struct _obstack_chunk *lp;
301*6912a2beSHONG Yifan   _OBSTACK_SIZE_T nbytes = 0;
302*6912a2beSHONG Yifan 
303*6912a2beSHONG Yifan   for (lp = h->chunk; lp != 0; lp = lp->prev)
304*6912a2beSHONG Yifan     {
305*6912a2beSHONG Yifan       nbytes += lp->limit - (char *) lp;
306*6912a2beSHONG Yifan     }
307*6912a2beSHONG Yifan   return nbytes;
308*6912a2beSHONG Yifan }
309*6912a2beSHONG Yifan 
310*6912a2beSHONG Yifan # ifndef _OBSTACK_NO_ERROR_HANDLER
311*6912a2beSHONG Yifan /* Define the error handler.  */
312*6912a2beSHONG Yifan #  include <stdio.h>
313*6912a2beSHONG Yifan 
314*6912a2beSHONG Yifan /* Exit value used when 'print_and_abort' is used.  */
315*6912a2beSHONG Yifan #  ifdef _LIBC
316*6912a2beSHONG Yifan int obstack_exit_failure = EXIT_FAILURE;
317*6912a2beSHONG Yifan #  else
318*6912a2beSHONG Yifan #   ifndef EXIT_FAILURE
319*6912a2beSHONG Yifan #    define EXIT_FAILURE 1
320*6912a2beSHONG Yifan #   endif
321*6912a2beSHONG Yifan #   define obstack_exit_failure EXIT_FAILURE
322*6912a2beSHONG Yifan #  endif
323*6912a2beSHONG Yifan 
324*6912a2beSHONG Yifan #  if defined _LIBC || (HAVE_LIBINTL_H && ENABLE_NLS)
325*6912a2beSHONG Yifan #   include <libintl.h>
326*6912a2beSHONG Yifan #   ifndef _
327*6912a2beSHONG Yifan #    define _(msgid) gettext (msgid)
328*6912a2beSHONG Yifan #   endif
329*6912a2beSHONG Yifan #  else
330*6912a2beSHONG Yifan #   ifndef _
331*6912a2beSHONG Yifan #    define _(msgid) (msgid)
332*6912a2beSHONG Yifan #   endif
333*6912a2beSHONG Yifan #  endif
334*6912a2beSHONG Yifan 
335*6912a2beSHONG Yifan #  if !(defined _Noreturn						      \
336*6912a2beSHONG Yifan         || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112))
337*6912a2beSHONG Yifan #   if ((defined __GNUC__						      \
338*6912a2beSHONG Yifan 	 && (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)))	      \
339*6912a2beSHONG Yifan 	|| (defined __SUNPRO_C && __SUNPRO_C >= 0x5110))
340*6912a2beSHONG Yifan #    define _Noreturn __attribute__ ((__noreturn__))
341*6912a2beSHONG Yifan #   elif defined _MSC_VER && _MSC_VER >= 1200
342*6912a2beSHONG Yifan #    define _Noreturn __declspec (noreturn)
343*6912a2beSHONG Yifan #   else
344*6912a2beSHONG Yifan #    define _Noreturn
345*6912a2beSHONG Yifan #   endif
346*6912a2beSHONG Yifan #  endif
347*6912a2beSHONG Yifan 
348*6912a2beSHONG Yifan #  ifdef _LIBC
349*6912a2beSHONG Yifan #   include <libio/iolibio.h>
350*6912a2beSHONG Yifan #  endif
351*6912a2beSHONG Yifan 
352*6912a2beSHONG Yifan static _Noreturn void
print_and_abort(void)353*6912a2beSHONG Yifan print_and_abort (void)
354*6912a2beSHONG Yifan {
355*6912a2beSHONG Yifan   /* Don't change any of these strings.  Yes, it would be possible to add
356*6912a2beSHONG Yifan      the newline to the string and use fputs or so.  But this must not
357*6912a2beSHONG Yifan      happen because the "memory exhausted" message appears in other places
358*6912a2beSHONG Yifan      like this and the translation should be reused instead of creating
359*6912a2beSHONG Yifan      a very similar string which requires a separate translation.  */
360*6912a2beSHONG Yifan #  ifdef _LIBC
361*6912a2beSHONG Yifan   (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
362*6912a2beSHONG Yifan #  else
363*6912a2beSHONG Yifan   fprintf (stderr, "%s\n", _("memory exhausted"));
364*6912a2beSHONG Yifan #  endif
365*6912a2beSHONG Yifan   exit (obstack_exit_failure);
366*6912a2beSHONG Yifan }
367*6912a2beSHONG Yifan 
368*6912a2beSHONG Yifan /* The functions allocating more room by calling 'obstack_chunk_alloc'
369*6912a2beSHONG Yifan    jump to the handler pointed to by 'obstack_alloc_failed_handler'.
370*6912a2beSHONG Yifan    This can be set to a user defined function which should either
371*6912a2beSHONG Yifan    abort gracefully or use longjump - but shouldn't return.  This
372*6912a2beSHONG Yifan    variable by default points to the internal function
373*6912a2beSHONG Yifan    'print_and_abort'.  */
374*6912a2beSHONG Yifan void (*obstack_alloc_failed_handler) (void) = print_and_abort;
375*6912a2beSHONG Yifan # endif /* !_OBSTACK_NO_ERROR_HANDLER */
376*6912a2beSHONG Yifan #endif /* !_OBSTACK_ELIDE_CODE */
377