update from main archive 961217
[glibc.git] / malloc / obstack.c
blobe059b5a7d0200500b8f3fedc9b6c33c910a3e6a3
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 (__STDC__) && __STDC__
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 #define CALL_CHUNKFUN(h, size) \
108 (((h) -> use_extra_arg) \
109 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
110 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
112 #define CALL_FREEFUN(h, old_chunk) \
113 do { \
114 if ((h) -> use_extra_arg) \
115 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
116 else \
117 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
118 } while (0)
121 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
122 Objects start on multiples of ALIGNMENT (0 means use default).
123 CHUNKFUN is the function to use to allocate chunks,
124 and FREEFUN the function to free them.
126 Return nonzero if successful, zero if out of memory.
127 To recover from an out of memory error,
128 free up some memory, then call this again. */
131 _obstack_begin (h, size, alignment, chunkfun, freefun)
132 struct obstack *h;
133 int size;
134 int alignment;
135 POINTER (*chunkfun) ();
136 void (*freefun) ();
138 register struct _obstack_chunk *chunk; /* points to new chunk */
140 if (alignment == 0)
141 alignment = DEFAULT_ALIGNMENT;
142 if (size == 0)
143 /* Default size is what GNU malloc can fit in a 4096-byte block. */
145 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
146 Use the values for range checking, because if range checking is off,
147 the extra bytes won't be missed terribly, but if range checking is on
148 and we used a larger request, a whole extra 4096 bytes would be
149 allocated.
151 These number are irrelevant to the new GNU malloc. I suspect it is
152 less sensitive to the size of the request. */
153 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
154 + 4 + DEFAULT_ROUNDING - 1)
155 & ~(DEFAULT_ROUNDING - 1));
156 size = 4096 - extra;
159 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
160 h->freefun = freefun;
161 h->chunk_size = size;
162 h->alignment_mask = alignment - 1;
163 h->use_extra_arg = 0;
165 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
166 if (!chunk)
167 (*obstack_alloc_failed_handler) ();
168 h->next_free = h->object_base = chunk->contents;
169 h->chunk_limit = chunk->limit
170 = (char *) chunk + h->chunk_size;
171 chunk->prev = 0;
172 /* The initial chunk now contains no empty object. */
173 h->maybe_empty_object = 0;
174 h->alloc_failed = 0;
175 return 1;
179 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
180 struct obstack *h;
181 int size;
182 int alignment;
183 POINTER (*chunkfun) ();
184 void (*freefun) ();
185 POINTER arg;
187 register struct _obstack_chunk *chunk; /* points to new chunk */
189 if (alignment == 0)
190 alignment = DEFAULT_ALIGNMENT;
191 if (size == 0)
192 /* Default size is what GNU malloc can fit in a 4096-byte block. */
194 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
195 Use the values for range checking, because if range checking is off,
196 the extra bytes won't be missed terribly, but if range checking is on
197 and we used a larger request, a whole extra 4096 bytes would be
198 allocated.
200 These number are irrelevant to the new GNU malloc. I suspect it is
201 less sensitive to the size of the request. */
202 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
203 + 4 + DEFAULT_ROUNDING - 1)
204 & ~(DEFAULT_ROUNDING - 1));
205 size = 4096 - extra;
208 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
209 h->freefun = freefun;
210 h->chunk_size = size;
211 h->alignment_mask = alignment - 1;
212 h->extra_arg = arg;
213 h->use_extra_arg = 1;
215 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
216 if (!chunk)
217 (*obstack_alloc_failed_handler) ();
218 h->next_free = h->object_base = chunk->contents;
219 h->chunk_limit = chunk->limit
220 = (char *) chunk + h->chunk_size;
221 chunk->prev = 0;
222 /* The initial chunk now contains no empty object. */
223 h->maybe_empty_object = 0;
224 h->alloc_failed = 0;
225 return 1;
228 /* Allocate a new current chunk for the obstack *H
229 on the assumption that LENGTH bytes need to be added
230 to the current object, or a new object of length LENGTH allocated.
231 Copies any partial object from the end of the old chunk
232 to the beginning of the new one. */
234 void
235 _obstack_newchunk (h, length)
236 struct obstack *h;
237 int length;
239 register struct _obstack_chunk *old_chunk = h->chunk;
240 register struct _obstack_chunk *new_chunk;
241 register long new_size;
242 register int obj_size = h->next_free - h->object_base;
243 register int i;
244 int already;
246 /* Compute size for new chunk. */
247 new_size = (obj_size + length) + (obj_size >> 3) + 100;
248 if (new_size < h->chunk_size)
249 new_size = h->chunk_size;
251 /* Allocate and initialize the new chunk. */
252 new_chunk = CALL_CHUNKFUN (h, new_size);
253 if (!new_chunk)
254 (*obstack_alloc_failed_handler) ();
255 h->chunk = new_chunk;
256 new_chunk->prev = old_chunk;
257 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
259 /* Move the existing object to the new chunk.
260 Word at a time is fast and is safe if the object
261 is sufficiently aligned. */
262 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
264 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
265 i >= 0; i--)
266 ((COPYING_UNIT *)new_chunk->contents)[i]
267 = ((COPYING_UNIT *)h->object_base)[i];
268 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
269 but that can cross a page boundary on a machine
270 which does not do strict alignment for COPYING_UNITS. */
271 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
273 else
274 already = 0;
275 /* Copy remaining bytes one by one. */
276 for (i = already; i < obj_size; i++)
277 new_chunk->contents[i] = h->object_base[i];
279 /* If the object just copied was the only data in OLD_CHUNK,
280 free that chunk and remove it from the chain.
281 But not if that chunk might contain an empty object. */
282 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
284 new_chunk->prev = old_chunk->prev;
285 CALL_FREEFUN (h, old_chunk);
288 h->object_base = new_chunk->contents;
289 h->next_free = h->object_base + obj_size;
290 /* The new chunk certainly contains no empty object yet. */
291 h->maybe_empty_object = 0;
294 /* Return nonzero if object OBJ has been allocated from obstack H.
295 This is here for debugging.
296 If you use it in a program, you are probably losing. */
298 #if defined (__STDC__) && __STDC__
299 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
300 obstack.h because it is just for debugging. */
301 int _obstack_allocated_p (struct obstack *h, POINTER obj);
302 #endif
305 _obstack_allocated_p (h, obj)
306 struct obstack *h;
307 POINTER obj;
309 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
310 register struct _obstack_chunk *plp; /* point to previous chunk if any */
312 lp = (h)->chunk;
313 /* We use >= rather than > since the object cannot be exactly at
314 the beginning of the chunk but might be an empty object exactly
315 at the end of an adjacent chunk. */
316 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
318 plp = lp->prev;
319 lp = plp;
321 return lp != 0;
324 /* Free objects in obstack H, including OBJ and everything allocate
325 more recently than OBJ. If OBJ is zero, free everything in H. */
327 #undef obstack_free
329 /* This function has two names with identical definitions.
330 This is the first one, called from non-ANSI code. */
332 void
333 _obstack_free (h, obj)
334 struct obstack *h;
335 POINTER obj;
337 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
338 register struct _obstack_chunk *plp; /* point to previous chunk if any */
340 lp = h->chunk;
341 /* We use >= because there cannot be an object at the beginning of a chunk.
342 But there can be an empty object at that address
343 at the end of another chunk. */
344 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
346 plp = lp->prev;
347 CALL_FREEFUN (h, lp);
348 lp = plp;
349 /* If we switch chunks, we can't tell whether the new current
350 chunk contains an empty object, so assume that it may. */
351 h->maybe_empty_object = 1;
353 if (lp)
355 h->object_base = h->next_free = (char *) (obj);
356 h->chunk_limit = lp->limit;
357 h->chunk = lp;
359 else if (obj != 0)
360 /* obj is not in any of the chunks! */
361 abort ();
364 /* This function is used from ANSI code. */
366 void
367 obstack_free (h, obj)
368 struct obstack *h;
369 POINTER obj;
371 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
372 register struct _obstack_chunk *plp; /* point to previous chunk if any */
374 lp = h->chunk;
375 /* We use >= because there cannot be an object at the beginning of a chunk.
376 But there can be an empty object at that address
377 at the end of another chunk. */
378 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
380 plp = lp->prev;
381 CALL_FREEFUN (h, lp);
382 lp = plp;
383 /* If we switch chunks, we can't tell whether the new current
384 chunk contains an empty object, so assume that it may. */
385 h->maybe_empty_object = 1;
387 if (lp)
389 h->object_base = h->next_free = (char *) (obj);
390 h->chunk_limit = lp->limit;
391 h->chunk = lp;
393 else if (obj != 0)
394 /* obj is not in any of the chunks! */
395 abort ();
399 _obstack_memory_used (h)
400 struct obstack *h;
402 register struct _obstack_chunk* lp;
403 register int nbytes = 0;
405 for (lp = h->chunk; lp != 0; lp = lp->prev)
407 nbytes += lp->limit - (char *) lp;
409 return nbytes;
412 /* Define the error handler. */
413 #ifndef _
414 # ifdef HAVE_LIBINTL_H
415 # include <libintl.h>
416 # ifndef _
417 # define _(Str) gettext (Str)
418 # endif
419 # else
420 # define _(Str) (Str)
421 # endif
422 #endif
424 static void
425 print_and_abort ()
427 fputs (_("memory exhausted\n"), stderr);
428 exit (obstack_exit_failure);
431 #if 0
432 /* These are now turned off because the applications do not use it
433 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
435 /* Now define the functional versions of the obstack macros.
436 Define them to simply use the corresponding macros to do the job. */
438 #if defined (__STDC__) && __STDC__
439 /* These function definitions do not work with non-ANSI preprocessors;
440 they won't pass through the macro names in parentheses. */
442 /* The function names appear in parentheses in order to prevent
443 the macro-definitions of the names from being expanded there. */
445 POINTER (obstack_base) (obstack)
446 struct obstack *obstack;
448 return obstack_base (obstack);
451 POINTER (obstack_next_free) (obstack)
452 struct obstack *obstack;
454 return obstack_next_free (obstack);
457 int (obstack_object_size) (obstack)
458 struct obstack *obstack;
460 return obstack_object_size (obstack);
463 int (obstack_room) (obstack)
464 struct obstack *obstack;
466 return obstack_room (obstack);
469 int (obstack_make_room) (obstack, length)
470 struct obstack *obstack;
471 int length;
473 return obstack_make_room (obstack, length);
476 void (obstack_grow) (obstack, pointer, length)
477 struct obstack *obstack;
478 POINTER pointer;
479 int length;
481 obstack_grow (obstack, pointer, length);
484 void (obstack_grow0) (obstack, pointer, length)
485 struct obstack *obstack;
486 POINTER pointer;
487 int length;
489 obstack_grow0 (obstack, pointer, length);
492 void (obstack_1grow) (obstack, character)
493 struct obstack *obstack;
494 int character;
496 obstack_1grow (obstack, character);
499 void (obstack_blank) (obstack, length)
500 struct obstack *obstack;
501 int length;
503 obstack_blank (obstack, length);
506 void (obstack_1grow_fast) (obstack, character)
507 struct obstack *obstack;
508 int character;
510 obstack_1grow_fast (obstack, character);
513 void (obstack_blank_fast) (obstack, length)
514 struct obstack *obstack;
515 int length;
517 obstack_blank_fast (obstack, length);
520 POINTER (obstack_finish) (obstack)
521 struct obstack *obstack;
523 return obstack_finish (obstack);
526 POINTER (obstack_alloc) (obstack, length)
527 struct obstack *obstack;
528 int length;
530 return obstack_alloc (obstack, length);
533 POINTER (obstack_copy) (obstack, pointer, length)
534 struct obstack *obstack;
535 POINTER pointer;
536 int length;
538 return obstack_copy (obstack, pointer, length);
541 POINTER (obstack_copy0) (obstack, pointer, length)
542 struct obstack *obstack;
543 POINTER pointer;
544 int length;
546 return obstack_copy0 (obstack, pointer, length);
549 #endif /* __STDC__ */
551 #endif /* 0 */
553 #endif /* !ELIDE_CODE */