add copyright/no-warranty comment
[coreutils.git] / lib / obstack.c
blob1ff4155611884c6db65780490282e2268f091f2a
1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988,89,90,91,92,93,94,96 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "obstack.h"
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
29 /* Comment out all this code if we are using the GNU C Library, and are not
30 actually compiling the library itself, and the installed library
31 supports the same library interface we do. This code is part of the GNU
32 C Library, but also included in many other GNU distributions. Compiling
33 and linking in this code is a waste when using the GNU C library
34 (especially if it is a shared library). Rather than having every GNU
35 program understand `configure --with-gnu-libc' and omit the object
36 files, it is simpler to just do this in the source for each such file. */
38 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
39 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
40 #include <gnu-versions.h>
41 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
42 #define ELIDE_CODE
43 #endif
44 #endif
47 #ifndef ELIDE_CODE
50 #if defined (__STDC__) && __STDC__
51 #define POINTER void *
52 #else
53 #define POINTER char *
54 #endif
56 /* Determine default alignment. */
57 struct fooalign {char x; double d;};
58 #define DEFAULT_ALIGNMENT \
59 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
60 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
61 But in fact it might be less smart and round addresses to as much as
62 DEFAULT_ROUNDING. So we prepare for it to do that. */
63 union fooround {long x; double d;};
64 #define DEFAULT_ROUNDING (sizeof (union fooround))
66 /* When we copy a long block of data, this is the unit to do it with.
67 On some machines, copying successive ints does not work;
68 in such a case, redefine COPYING_UNIT to `long' (if that works)
69 or `char' as a last resort. */
70 #ifndef COPYING_UNIT
71 #define COPYING_UNIT int
72 #endif
75 /* The functions allocating more room by calling `obstack_chunk_alloc'
76 jump to the handler pointed to by `obstack_alloc_failed_handler'.
77 This variable by default points to the internal function
78 `print_and_abort'. */
79 #if defined (__STDC__) && __STDC__
80 static void print_and_abort (void);
81 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
82 #else
83 static void print_and_abort ();
84 void (*obstack_alloc_failed_handler) () = print_and_abort;
85 #endif
87 /* Exit value used when `print_and_abort' is used. */
88 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
89 #include <stdlib.h>
90 #endif
91 #ifndef EXIT_FAILURE
92 #define EXIT_FAILURE 1
93 #endif
94 int obstack_exit_failure = EXIT_FAILURE;
96 /* The non-GNU-C macros copy the obstack into this global variable
97 to avoid multiple evaluation. */
99 struct obstack *_obstack;
101 /* Define a macro that either calls functions with the traditional malloc/free
102 calling interface, or calls functions with the mmalloc/mfree interface
103 (that adds an extra first argument), based on the state of use_extra_arg.
104 For free, do not use ?:, since some compilers, like the MIPS compilers,
105 do not allow (expr) ? void : void. */
107 #if defined (__STDC__) && __STDC__
108 #define CALL_CHUNKFUN(h, size) \
109 (((h) -> use_extra_arg) \
110 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
111 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
113 #define CALL_FREEFUN(h, old_chunk) \
114 do { \
115 if ((h) -> use_extra_arg) \
116 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
117 else \
118 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
119 } while (0)
120 #else
121 #define CALL_CHUNKFUN(h, size) \
122 (((h) -> use_extra_arg) \
123 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
124 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
126 #define CALL_FREEFUN(h, old_chunk) \
127 do { \
128 if ((h) -> use_extra_arg) \
129 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
130 else \
131 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
132 } while (0)
133 #endif
136 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
137 Objects start on multiples of ALIGNMENT (0 means use default).
138 CHUNKFUN is the function to use to allocate chunks,
139 and FREEFUN the function to free them.
141 Return nonzero if successful, zero if out of memory.
142 To recover from an out of memory error,
143 free up some memory, then call this again. */
146 _obstack_begin (h, size, alignment, chunkfun, freefun)
147 struct obstack *h;
148 int size;
149 int alignment;
150 #if defined (__STDC__) && __STDC__
151 POINTER (*chunkfun) (long);
152 void (*freefun) (void *);
153 #else
154 POINTER (*chunkfun) ();
155 void (*freefun) ();
156 #endif
158 register struct _obstack_chunk *chunk; /* points to new chunk */
160 if (alignment == 0)
161 alignment = DEFAULT_ALIGNMENT;
162 if (size == 0)
163 /* Default size is what GNU malloc can fit in a 4096-byte block. */
165 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
166 Use the values for range checking, because if range checking is off,
167 the extra bytes won't be missed terribly, but if range checking is on
168 and we used a larger request, a whole extra 4096 bytes would be
169 allocated.
171 These number are irrelevant to the new GNU malloc. I suspect it is
172 less sensitive to the size of the request. */
173 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
174 + 4 + DEFAULT_ROUNDING - 1)
175 & ~(DEFAULT_ROUNDING - 1));
176 size = 4096 - extra;
179 #if defined (__STDC__) && __STDC__
180 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
181 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
182 #else
183 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
184 h->freefun = freefun;
185 #endif
186 h->chunk_size = size;
187 h->alignment_mask = alignment - 1;
188 h->use_extra_arg = 0;
190 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
191 if (!chunk)
192 (*obstack_alloc_failed_handler) ();
193 h->next_free = h->object_base = chunk->contents;
194 h->chunk_limit = chunk->limit
195 = (char *) chunk + h->chunk_size;
196 chunk->prev = 0;
197 /* The initial chunk now contains no empty object. */
198 h->maybe_empty_object = 0;
199 h->alloc_failed = 0;
200 return 1;
204 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
205 struct obstack *h;
206 int size;
207 int alignment;
208 #if defined (__STDC__) && __STDC__
209 POINTER (*chunkfun) (POINTER, long);
210 void (*freefun) (POINTER, POINTER);
211 #else
212 POINTER (*chunkfun) ();
213 void (*freefun) ();
214 #endif
215 POINTER arg;
217 register struct _obstack_chunk *chunk; /* points to new chunk */
219 if (alignment == 0)
220 alignment = DEFAULT_ALIGNMENT;
221 if (size == 0)
222 /* Default size is what GNU malloc can fit in a 4096-byte block. */
224 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
225 Use the values for range checking, because if range checking is off,
226 the extra bytes won't be missed terribly, but if range checking is on
227 and we used a larger request, a whole extra 4096 bytes would be
228 allocated.
230 These number are irrelevant to the new GNU malloc. I suspect it is
231 less sensitive to the size of the request. */
232 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
233 + 4 + DEFAULT_ROUNDING - 1)
234 & ~(DEFAULT_ROUNDING - 1));
235 size = 4096 - extra;
238 #if defined(__STDC__) && __STDC__
239 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
240 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
241 #else
242 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
243 h->freefun = freefun;
244 #endif
245 h->chunk_size = size;
246 h->alignment_mask = alignment - 1;
247 h->extra_arg = arg;
248 h->use_extra_arg = 1;
250 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
251 if (!chunk)
252 (*obstack_alloc_failed_handler) ();
253 h->next_free = h->object_base = chunk->contents;
254 h->chunk_limit = chunk->limit
255 = (char *) chunk + h->chunk_size;
256 chunk->prev = 0;
257 /* The initial chunk now contains no empty object. */
258 h->maybe_empty_object = 0;
259 h->alloc_failed = 0;
260 return 1;
263 /* Allocate a new current chunk for the obstack *H
264 on the assumption that LENGTH bytes need to be added
265 to the current object, or a new object of length LENGTH allocated.
266 Copies any partial object from the end of the old chunk
267 to the beginning of the new one. */
269 void
270 _obstack_newchunk (h, length)
271 struct obstack *h;
272 int length;
274 register struct _obstack_chunk *old_chunk = h->chunk;
275 register struct _obstack_chunk *new_chunk;
276 register long new_size;
277 register int obj_size = h->next_free - h->object_base;
278 register int i;
279 int already;
281 /* Compute size for new chunk. */
282 new_size = (obj_size + length) + (obj_size >> 3) + 100;
283 if (new_size < h->chunk_size)
284 new_size = h->chunk_size;
286 /* Allocate and initialize the new chunk. */
287 new_chunk = CALL_CHUNKFUN (h, new_size);
288 if (!new_chunk)
289 (*obstack_alloc_failed_handler) ();
290 h->chunk = new_chunk;
291 new_chunk->prev = old_chunk;
292 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
294 /* Move the existing object to the new chunk.
295 Word at a time is fast and is safe if the object
296 is sufficiently aligned. */
297 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
299 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
300 i >= 0; i--)
301 ((COPYING_UNIT *)new_chunk->contents)[i]
302 = ((COPYING_UNIT *)h->object_base)[i];
303 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
304 but that can cross a page boundary on a machine
305 which does not do strict alignment for COPYING_UNITS. */
306 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
308 else
309 already = 0;
310 /* Copy remaining bytes one by one. */
311 for (i = already; i < obj_size; i++)
312 new_chunk->contents[i] = h->object_base[i];
314 /* If the object just copied was the only data in OLD_CHUNK,
315 free that chunk and remove it from the chain.
316 But not if that chunk might contain an empty object. */
317 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
319 new_chunk->prev = old_chunk->prev;
320 CALL_FREEFUN (h, old_chunk);
323 h->object_base = new_chunk->contents;
324 h->next_free = h->object_base + obj_size;
325 /* The new chunk certainly contains no empty object yet. */
326 h->maybe_empty_object = 0;
329 /* Return nonzero if object OBJ has been allocated from obstack H.
330 This is here for debugging.
331 If you use it in a program, you are probably losing. */
333 #if defined (__STDC__) && __STDC__
334 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
335 obstack.h because it is just for debugging. */
336 int _obstack_allocated_p (struct obstack *h, POINTER obj);
337 #endif
340 _obstack_allocated_p (h, obj)
341 struct obstack *h;
342 POINTER obj;
344 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
345 register struct _obstack_chunk *plp; /* point to previous chunk if any */
347 lp = (h)->chunk;
348 /* We use >= rather than > since the object cannot be exactly at
349 the beginning of the chunk but might be an empty object exactly
350 at the end of an adjacent chunk. */
351 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
353 plp = lp->prev;
354 lp = plp;
356 return lp != 0;
359 /* Free objects in obstack H, including OBJ and everything allocate
360 more recently than OBJ. If OBJ is zero, free everything in H. */
362 #undef obstack_free
364 /* This function has two names with identical definitions.
365 This is the first one, called from non-ANSI code. */
367 void
368 _obstack_free (h, obj)
369 struct obstack *h;
370 POINTER obj;
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 */
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))
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;
388 if (lp)
390 h->object_base = h->next_free = (char *) (obj);
391 h->chunk_limit = lp->limit;
392 h->chunk = lp;
394 else if (obj != 0)
395 /* obj is not in any of the chunks! */
396 abort ();
399 /* This function is used from ANSI code. */
401 void
402 obstack_free (h, obj)
403 struct obstack *h;
404 POINTER obj;
406 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
407 register struct _obstack_chunk *plp; /* point to previous chunk if any */
409 lp = h->chunk;
410 /* We use >= because there cannot be an object at the beginning of a chunk.
411 But there can be an empty object at that address
412 at the end of another chunk. */
413 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
415 plp = lp->prev;
416 CALL_FREEFUN (h, lp);
417 lp = plp;
418 /* If we switch chunks, we can't tell whether the new current
419 chunk contains an empty object, so assume that it may. */
420 h->maybe_empty_object = 1;
422 if (lp)
424 h->object_base = h->next_free = (char *) (obj);
425 h->chunk_limit = lp->limit;
426 h->chunk = lp;
428 else if (obj != 0)
429 /* obj is not in any of the chunks! */
430 abort ();
434 _obstack_memory_used (h)
435 struct obstack *h;
437 register struct _obstack_chunk* lp;
438 register int nbytes = 0;
440 for (lp = h->chunk; lp != 0; lp = lp->prev)
442 nbytes += lp->limit - (char *) lp;
444 return nbytes;
447 /* Define the error handler. */
448 #ifndef _
449 # ifdef HAVE_LIBINTL_H
450 # include <libintl.h>
451 # ifndef _
452 # define _(Str) gettext (Str)
453 # endif
454 # else
455 # define _(Str) (Str)
456 # endif
457 #endif
459 static void
460 print_and_abort ()
462 fputs (_("memory exhausted\n"), stderr);
463 exit (obstack_exit_failure);
466 #if 0
467 /* These are now turned off because the applications do not use it
468 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
470 /* Now define the functional versions of the obstack macros.
471 Define them to simply use the corresponding macros to do the job. */
473 #if defined (__STDC__) && __STDC__
474 /* These function definitions do not work with non-ANSI preprocessors;
475 they won't pass through the macro names in parentheses. */
477 /* The function names appear in parentheses in order to prevent
478 the macro-definitions of the names from being expanded there. */
480 POINTER (obstack_base) (obstack)
481 struct obstack *obstack;
483 return obstack_base (obstack);
486 POINTER (obstack_next_free) (obstack)
487 struct obstack *obstack;
489 return obstack_next_free (obstack);
492 int (obstack_object_size) (obstack)
493 struct obstack *obstack;
495 return obstack_object_size (obstack);
498 int (obstack_room) (obstack)
499 struct obstack *obstack;
501 return obstack_room (obstack);
504 int (obstack_make_room) (obstack, length)
505 struct obstack *obstack;
506 int length;
508 return obstack_make_room (obstack, length);
511 void (obstack_grow) (obstack, pointer, length)
512 struct obstack *obstack;
513 POINTER pointer;
514 int length;
516 obstack_grow (obstack, pointer, length);
519 void (obstack_grow0) (obstack, pointer, length)
520 struct obstack *obstack;
521 POINTER pointer;
522 int length;
524 obstack_grow0 (obstack, pointer, length);
527 void (obstack_1grow) (obstack, character)
528 struct obstack *obstack;
529 int character;
531 obstack_1grow (obstack, character);
534 void (obstack_blank) (obstack, length)
535 struct obstack *obstack;
536 int length;
538 obstack_blank (obstack, length);
541 void (obstack_1grow_fast) (obstack, character)
542 struct obstack *obstack;
543 int character;
545 obstack_1grow_fast (obstack, character);
548 void (obstack_blank_fast) (obstack, length)
549 struct obstack *obstack;
550 int length;
552 obstack_blank_fast (obstack, length);
555 POINTER (obstack_finish) (obstack)
556 struct obstack *obstack;
558 return obstack_finish (obstack);
561 POINTER (obstack_alloc) (obstack, length)
562 struct obstack *obstack;
563 int length;
565 return obstack_alloc (obstack, length);
568 POINTER (obstack_copy) (obstack, pointer, length)
569 struct obstack *obstack;
570 POINTER pointer;
571 int length;
573 return obstack_copy (obstack, pointer, length);
576 POINTER (obstack_copy0) (obstack, pointer, length)
577 struct obstack *obstack;
578 POINTER pointer;
579 int length;
581 return obstack_copy0 (obstack, pointer, length);
584 #endif /* __STDC__ */
586 #endif /* 0 */
588 #endif /* !ELIDE_CODE */