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