Remove handling of static-only-routines.
[glibc.git] / malloc / obstack.c
blob731762f5a98d06beb0911546cad3224bac884edc
1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988,89,90,91,92,93,94,96,97 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include "obstack.h"
28 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
29 incremented whenever callers compiled using an old obstack.h can no
30 longer properly call the functions in this obstack.c. */
31 #define OBSTACK_INTERFACE_VERSION 1
33 /* Comment out all this code if we are using the GNU C Library, and are not
34 actually compiling the library itself, and the installed library
35 supports the same library interface we do. This code is part of the GNU
36 C Library, but also included in many other GNU distributions. Compiling
37 and linking in this code is a waste when using the GNU C library
38 (especially if it is a shared library). Rather than having every GNU
39 program understand `configure --with-gnu-libc' and omit the object
40 files, it is simpler to just do this in the source for each such file. */
42 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
43 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
44 #include <gnu-versions.h>
45 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
46 #define ELIDE_CODE
47 #endif
48 #endif
51 #ifndef ELIDE_CODE
54 #if defined (__STDC__) && __STDC__
55 #define POINTER void *
56 #else
57 #define POINTER char *
58 #endif
60 /* Determine default alignment. */
61 struct fooalign {char x; double d;};
62 #define DEFAULT_ALIGNMENT \
63 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
64 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
65 But in fact it might be less smart and round addresses to as much as
66 DEFAULT_ROUNDING. So we prepare for it to do that. */
67 union fooround {long x; double d;};
68 #define DEFAULT_ROUNDING (sizeof (union fooround))
70 /* When we copy a long block of data, this is the unit to do it with.
71 On some machines, copying successive ints does not work;
72 in such a case, redefine COPYING_UNIT to `long' (if that works)
73 or `char' as a last resort. */
74 #ifndef COPYING_UNIT
75 #define COPYING_UNIT int
76 #endif
79 /* The functions allocating more room by calling `obstack_chunk_alloc'
80 jump to the handler pointed to by `obstack_alloc_failed_handler'.
81 This variable by default points to the internal function
82 `print_and_abort'. */
83 #if defined (__STDC__) && __STDC__
84 static void print_and_abort (void);
85 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
86 #else
87 static void print_and_abort ();
88 void (*obstack_alloc_failed_handler) () = print_and_abort;
89 #endif
91 /* Exit value used when `print_and_abort' is used. */
92 #if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
93 #include <stdlib.h>
94 #endif
95 #ifndef EXIT_FAILURE
96 #define EXIT_FAILURE 1
97 #endif
98 int obstack_exit_failure = EXIT_FAILURE;
100 /* The non-GNU-C macros copy the obstack into this global variable
101 to avoid multiple evaluation. */
103 struct obstack *_obstack;
105 /* Define a macro that either calls functions with the traditional malloc/free
106 calling interface, or calls functions with the mmalloc/mfree interface
107 (that adds an extra first argument), based on the state of use_extra_arg.
108 For free, do not use ?:, since some compilers, like the MIPS compilers,
109 do not allow (expr) ? void : void. */
111 #if defined (__STDC__) && __STDC__
112 #define CALL_CHUNKFUN(h, size) \
113 (((h) -> use_extra_arg) \
114 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
115 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
117 #define CALL_FREEFUN(h, old_chunk) \
118 do { \
119 if ((h) -> use_extra_arg) \
120 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
121 else \
122 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
123 } while (0)
124 #else
125 #define CALL_CHUNKFUN(h, size) \
126 (((h) -> use_extra_arg) \
127 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
128 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
130 #define CALL_FREEFUN(h, old_chunk) \
131 do { \
132 if ((h) -> use_extra_arg) \
133 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
134 else \
135 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
136 } while (0)
137 #endif
140 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
141 Objects start on multiples of ALIGNMENT (0 means use default).
142 CHUNKFUN is the function to use to allocate chunks,
143 and FREEFUN the function to free them.
145 Return nonzero if successful, zero if out of memory.
146 To recover from an out of memory error,
147 free up some memory, then call this again. */
150 _obstack_begin (h, size, alignment, chunkfun, freefun)
151 struct obstack *h;
152 int size;
153 int alignment;
154 #if defined (__STDC__) && __STDC__
155 POINTER (*chunkfun) (long);
156 void (*freefun) (void *);
157 #else
158 POINTER (*chunkfun) ();
159 void (*freefun) ();
160 #endif
162 register struct _obstack_chunk *chunk; /* points to new chunk */
164 if (alignment == 0)
165 alignment = DEFAULT_ALIGNMENT;
166 if (size == 0)
167 /* Default size is what GNU malloc can fit in a 4096-byte block. */
169 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
170 Use the values for range checking, because if range checking is off,
171 the extra bytes won't be missed terribly, but if range checking is on
172 and we used a larger request, a whole extra 4096 bytes would be
173 allocated.
175 These number are irrelevant to the new GNU malloc. I suspect it is
176 less sensitive to the size of the request. */
177 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
178 + 4 + DEFAULT_ROUNDING - 1)
179 & ~(DEFAULT_ROUNDING - 1));
180 size = 4096 - extra;
183 #if defined (__STDC__) && __STDC__
184 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
185 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
186 #else
187 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
188 h->freefun = freefun;
189 #endif
190 h->chunk_size = size;
191 h->alignment_mask = alignment - 1;
192 h->use_extra_arg = 0;
194 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
195 if (!chunk)
196 (*obstack_alloc_failed_handler) ();
197 h->next_free = h->object_base = chunk->contents;
198 h->chunk_limit = chunk->limit
199 = (char *) chunk + h->chunk_size;
200 chunk->prev = 0;
201 /* The initial chunk now contains no empty object. */
202 h->maybe_empty_object = 0;
203 h->alloc_failed = 0;
204 return 1;
208 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
209 struct obstack *h;
210 int size;
211 int alignment;
212 #if defined (__STDC__) && __STDC__
213 POINTER (*chunkfun) (POINTER, long);
214 void (*freefun) (POINTER, POINTER);
215 #else
216 POINTER (*chunkfun) ();
217 void (*freefun) ();
218 #endif
219 POINTER arg;
221 register struct _obstack_chunk *chunk; /* points to new chunk */
223 if (alignment == 0)
224 alignment = DEFAULT_ALIGNMENT;
225 if (size == 0)
226 /* Default size is what GNU malloc can fit in a 4096-byte block. */
228 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
229 Use the values for range checking, because if range checking is off,
230 the extra bytes won't be missed terribly, but if range checking is on
231 and we used a larger request, a whole extra 4096 bytes would be
232 allocated.
234 These number are irrelevant to the new GNU malloc. I suspect it is
235 less sensitive to the size of the request. */
236 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
237 + 4 + DEFAULT_ROUNDING - 1)
238 & ~(DEFAULT_ROUNDING - 1));
239 size = 4096 - extra;
242 #if defined(__STDC__) && __STDC__
243 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
244 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
245 #else
246 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
247 h->freefun = freefun;
248 #endif
249 h->chunk_size = size;
250 h->alignment_mask = alignment - 1;
251 h->extra_arg = arg;
252 h->use_extra_arg = 1;
254 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
255 if (!chunk)
256 (*obstack_alloc_failed_handler) ();
257 h->next_free = h->object_base = chunk->contents;
258 h->chunk_limit = chunk->limit
259 = (char *) chunk + h->chunk_size;
260 chunk->prev = 0;
261 /* The initial chunk now contains no empty object. */
262 h->maybe_empty_object = 0;
263 h->alloc_failed = 0;
264 return 1;
267 /* Allocate a new current chunk for the obstack *H
268 on the assumption that LENGTH bytes need to be added
269 to the current object, or a new object of length LENGTH allocated.
270 Copies any partial object from the end of the old chunk
271 to the beginning of the new one. */
273 void
274 _obstack_newchunk (h, length)
275 struct obstack *h;
276 int length;
278 register struct _obstack_chunk *old_chunk = h->chunk;
279 register struct _obstack_chunk *new_chunk;
280 register long new_size;
281 register int obj_size = h->next_free - h->object_base;
282 register int i;
283 int already;
285 /* Compute size for new chunk. */
286 new_size = (obj_size + length) + (obj_size >> 3) + 100;
287 if (new_size < h->chunk_size)
288 new_size = h->chunk_size;
290 /* Allocate and initialize the new chunk. */
291 new_chunk = CALL_CHUNKFUN (h, new_size);
292 if (!new_chunk)
293 (*obstack_alloc_failed_handler) ();
294 h->chunk = new_chunk;
295 new_chunk->prev = old_chunk;
296 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
298 /* Move the existing object to the new chunk.
299 Word at a time is fast and is safe if the object
300 is sufficiently aligned. */
301 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
303 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
304 i >= 0; i--)
305 ((COPYING_UNIT *)new_chunk->contents)[i]
306 = ((COPYING_UNIT *)h->object_base)[i];
307 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
308 but that can cross a page boundary on a machine
309 which does not do strict alignment for COPYING_UNITS. */
310 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
312 else
313 already = 0;
314 /* Copy remaining bytes one by one. */
315 for (i = already; i < obj_size; i++)
316 new_chunk->contents[i] = h->object_base[i];
318 /* If the object just copied was the only data in OLD_CHUNK,
319 free that chunk and remove it from the chain.
320 But not if that chunk might contain an empty object. */
321 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
323 new_chunk->prev = old_chunk->prev;
324 CALL_FREEFUN (h, old_chunk);
327 h->object_base = new_chunk->contents;
328 h->next_free = h->object_base + obj_size;
329 /* The new chunk certainly contains no empty object yet. */
330 h->maybe_empty_object = 0;
333 /* Return nonzero if object OBJ has been allocated from obstack H.
334 This is here for debugging.
335 If you use it in a program, you are probably losing. */
337 #if defined (__STDC__) && __STDC__
338 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
339 obstack.h because it is just for debugging. */
340 int _obstack_allocated_p (struct obstack *h, POINTER obj);
341 #endif
344 _obstack_allocated_p (h, obj)
345 struct obstack *h;
346 POINTER obj;
348 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
349 register struct _obstack_chunk *plp; /* point to previous chunk if any */
351 lp = (h)->chunk;
352 /* We use >= rather than > since the object cannot be exactly at
353 the beginning of the chunk but might be an empty object exactly
354 at the end of an adjacent chunk. */
355 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
357 plp = lp->prev;
358 lp = plp;
360 return lp != 0;
363 /* Free objects in obstack H, including OBJ and everything allocate
364 more recently than OBJ. If OBJ is zero, free everything in H. */
366 #undef obstack_free
368 /* This function has two names with identical definitions.
369 This is the first one, called from non-ANSI code. */
371 void
372 _obstack_free (h, obj)
373 struct obstack *h;
374 POINTER obj;
376 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
377 register struct _obstack_chunk *plp; /* point to previous chunk if any */
379 lp = h->chunk;
380 /* We use >= because there cannot be an object at the beginning of a chunk.
381 But there can be an empty object at that address
382 at the end of another chunk. */
383 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
385 plp = lp->prev;
386 CALL_FREEFUN (h, lp);
387 lp = plp;
388 /* If we switch chunks, we can't tell whether the new current
389 chunk contains an empty object, so assume that it may. */
390 h->maybe_empty_object = 1;
392 if (lp)
394 h->object_base = h->next_free = (char *) (obj);
395 h->chunk_limit = lp->limit;
396 h->chunk = lp;
398 else if (obj != 0)
399 /* obj is not in any of the chunks! */
400 abort ();
403 /* This function is used from ANSI code. */
405 void
406 obstack_free (h, obj)
407 struct obstack *h;
408 POINTER obj;
410 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
411 register struct _obstack_chunk *plp; /* point to previous chunk if any */
413 lp = h->chunk;
414 /* We use >= because there cannot be an object at the beginning of a chunk.
415 But there can be an empty object at that address
416 at the end of another chunk. */
417 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
419 plp = lp->prev;
420 CALL_FREEFUN (h, lp);
421 lp = plp;
422 /* If we switch chunks, we can't tell whether the new current
423 chunk contains an empty object, so assume that it may. */
424 h->maybe_empty_object = 1;
426 if (lp)
428 h->object_base = h->next_free = (char *) (obj);
429 h->chunk_limit = lp->limit;
430 h->chunk = lp;
432 else if (obj != 0)
433 /* obj is not in any of the chunks! */
434 abort ();
438 _obstack_memory_used (h)
439 struct obstack *h;
441 register struct _obstack_chunk* lp;
442 register int nbytes = 0;
444 for (lp = h->chunk; lp != 0; lp = lp->prev)
446 nbytes += lp->limit - (char *) lp;
448 return nbytes;
451 /* Define the error handler. */
452 #ifndef _
453 # ifdef HAVE_LIBINTL_H
454 # include <libintl.h>
455 # ifndef _
456 # define _(Str) gettext (Str)
457 # endif
458 # else
459 # define _(Str) (Str)
460 # endif
461 #endif
463 static void
464 print_and_abort ()
466 fputs (_("memory exhausted\n"), stderr);
467 exit (obstack_exit_failure);
470 #if 0
471 /* These are now turned off because the applications do not use it
472 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
474 /* Now define the functional versions of the obstack macros.
475 Define them to simply use the corresponding macros to do the job. */
477 #if defined (__STDC__) && __STDC__
478 /* These function definitions do not work with non-ANSI preprocessors;
479 they won't pass through the macro names in parentheses. */
481 /* The function names appear in parentheses in order to prevent
482 the macro-definitions of the names from being expanded there. */
484 POINTER (obstack_base) (obstack)
485 struct obstack *obstack;
487 return obstack_base (obstack);
490 POINTER (obstack_next_free) (obstack)
491 struct obstack *obstack;
493 return obstack_next_free (obstack);
496 int (obstack_object_size) (obstack)
497 struct obstack *obstack;
499 return obstack_object_size (obstack);
502 int (obstack_room) (obstack)
503 struct obstack *obstack;
505 return obstack_room (obstack);
508 int (obstack_make_room) (obstack, length)
509 struct obstack *obstack;
510 int length;
512 return obstack_make_room (obstack, length);
515 void (obstack_grow) (obstack, pointer, length)
516 struct obstack *obstack;
517 POINTER pointer;
518 int length;
520 obstack_grow (obstack, pointer, length);
523 void (obstack_grow0) (obstack, pointer, length)
524 struct obstack *obstack;
525 POINTER pointer;
526 int length;
528 obstack_grow0 (obstack, pointer, length);
531 void (obstack_1grow) (obstack, character)
532 struct obstack *obstack;
533 int character;
535 obstack_1grow (obstack, character);
538 void (obstack_blank) (obstack, length)
539 struct obstack *obstack;
540 int length;
542 obstack_blank (obstack, length);
545 void (obstack_1grow_fast) (obstack, character)
546 struct obstack *obstack;
547 int character;
549 obstack_1grow_fast (obstack, character);
552 void (obstack_blank_fast) (obstack, length)
553 struct obstack *obstack;
554 int length;
556 obstack_blank_fast (obstack, length);
559 POINTER (obstack_finish) (obstack)
560 struct obstack *obstack;
562 return obstack_finish (obstack);
565 POINTER (obstack_alloc) (obstack, length)
566 struct obstack *obstack;
567 int length;
569 return obstack_alloc (obstack, length);
572 POINTER (obstack_copy) (obstack, pointer, length)
573 struct obstack *obstack;
574 POINTER pointer;
575 int length;
577 return obstack_copy (obstack, pointer, length);
580 POINTER (obstack_copy0) (obstack, pointer, length)
581 struct obstack *obstack;
582 POINTER pointer;
583 int length;
585 return obstack_copy0 (obstack, pointer, length);
588 #endif /* __STDC__ */
590 #endif /* 0 */
592 #endif /* !ELIDE_CODE */