source: vendor/glibc/current/malloc/obstack.c

Last change on this file was 2152, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 14.2 KB
Line 
1/* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 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.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#ifdef _LIBC
27# include <obstack.h>
28# include <shlib-compat.h>
29#else
30# include "obstack.h"
31#endif
32
33/* NOTE BEFORE MODIFYING THIS FILE: This version number must be
34 incremented whenever callers compiled using an old obstack.h can no
35 longer properly call the functions in this obstack.c. */
36#define OBSTACK_INTERFACE_VERSION 1
37
38/* Comment out all this code if we are using the GNU C Library, and are not
39 actually compiling the library itself, and the installed library
40 supports the same library interface we do. This code is part of the GNU
41 C Library, but also included in many other GNU distributions. Compiling
42 and linking in this code is a waste when using the GNU C library
43 (especially if it is a shared library). Rather than having every GNU
44 program understand `configure --with-gnu-libc' and omit the object
45 files, it is simpler to just do this in the source for each such file. */
46
47#include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
48#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
49# include <gnu-versions.h>
50# if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
51# define ELIDE_CODE
52# endif
53#endif
54
55#if defined _LIBC && defined USE_IN_LIBIO
56# include <wchar.h>
57#endif
58
59#ifndef ELIDE_CODE
60
61
62/* Determine default alignment. */
63struct fooalign {char x; double d;};
64# define DEFAULT_ALIGNMENT \
65 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
66/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
67 But in fact it might be less smart and round addresses to as much as
68 DEFAULT_ROUNDING. So we prepare for it to do that. */
69union fooround {long x; double d;};
70# define DEFAULT_ROUNDING (sizeof (union fooround))
71
72/* When we copy a long block of data, this is the unit to do it with.
73 On some machines, copying successive ints does not work;
74 in such a case, redefine COPYING_UNIT to `long' (if that works)
75 or `char' as a last resort. */
76# ifndef COPYING_UNIT
77# define COPYING_UNIT int
78# endif
79
80
81/* The functions allocating more room by calling `obstack_chunk_alloc'
82 jump to the handler pointed to by `obstack_alloc_failed_handler'.
83 This can be set to a user defined function which should either
84 abort gracefully or use longjump - but shouldn't return. This
85 variable by default points to the internal function
86 `print_and_abort'. */
87static void print_and_abort (void);
88void (*obstack_alloc_failed_handler) (void) = print_and_abort;
89
90/* Exit value used when `print_and_abort' is used. */
91# include <stdlib.h>
92# ifdef _LIBC
93int obstack_exit_failure = EXIT_FAILURE;
94# else
95# include "exitfail.h"
96# define obstack_exit_failure exit_failure
97# endif
98
99# ifdef _LIBC
100# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
101/* A looong time ago (before 1994, anyway; we're not sure) this global variable
102 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
103 library still exports it because somebody might use it. */
104struct obstack *_obstack_compat;
105compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
106# endif
107# endif
108
109/* Define a macro that either calls functions with the traditional malloc/free
110 calling interface, or calls functions with the mmalloc/mfree interface
111 (that adds an extra first argument), based on the state of use_extra_arg.
112 For free, do not use ?:, since some compilers, like the MIPS compilers,
113 do not allow (expr) ? void : void. */
114
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
128
129
130/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
131 Objects start on multiples of ALIGNMENT (0 means use default).
132 CHUNKFUN is the function to use to allocate chunks,
133 and FREEFUN the function to free them.
134
135 Return nonzero if successful, calls obstack_alloc_failed_handler if
136 allocation fails. */
137
138int
139_obstack_begin (struct obstack *h,
140 int size, int alignment,
141 void *(*chunkfun) (long),
142 void (*freefun) (void *))
143{
144 register struct _obstack_chunk *chunk; /* points to new chunk */
145
146 if (alignment == 0)
147 alignment = (int) DEFAULT_ALIGNMENT;
148 if (size == 0)
149 /* Default size is what GNU malloc can fit in a 4096-byte block. */
150 {
151 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
152 Use the values for range checking, because if range checking is off,
153 the extra bytes won't be missed terribly, but if range checking is on
154 and we used a larger request, a whole extra 4096 bytes would be
155 allocated.
156
157 These number are irrelevant to the new GNU malloc. I suspect it is
158 less sensitive to the size of the request. */
159 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
160 + 4 + DEFAULT_ROUNDING - 1)
161 & ~(DEFAULT_ROUNDING - 1));
162 size = 4096 - extra;
163 }
164
165 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
166 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
167 h->chunk_size = size;
168 h->alignment_mask = alignment - 1;
169 h->use_extra_arg = 0;
170
171 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
172 if (!chunk)
173 (*obstack_alloc_failed_handler) ();
174 h->next_free = h->object_base = chunk->contents;
175 h->chunk_limit = chunk->limit
176 = (char *) chunk + h->chunk_size;
177 chunk->prev = 0;
178 /* The initial chunk now contains no empty object. */
179 h->maybe_empty_object = 0;
180 h->alloc_failed = 0;
181 return 1;
182}
183
184int
185_obstack_begin_1 (struct obstack *h, int size, int alignment,
186 void *(*chunkfun) (void *, long),
187 void (*freefun) (void *, void *),
188 void *arg)
189{
190 register struct _obstack_chunk *chunk; /* points to new chunk */
191
192 if (alignment == 0)
193 alignment = (int) DEFAULT_ALIGNMENT;
194 if (size == 0)
195 /* Default size is what GNU malloc can fit in a 4096-byte block. */
196 {
197 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
198 Use the values for range checking, because if range checking is off,
199 the extra bytes won't be missed terribly, but if range checking is on
200 and we used a larger request, a whole extra 4096 bytes would be
201 allocated.
202
203 These number are irrelevant to the new GNU malloc. I suspect it is
204 less sensitive to the size of the request. */
205 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
206 + 4 + DEFAULT_ROUNDING - 1)
207 & ~(DEFAULT_ROUNDING - 1));
208 size = 4096 - extra;
209 }
210
211 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
212 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
213 h->chunk_size = size;
214 h->alignment_mask = alignment - 1;
215 h->extra_arg = arg;
216 h->use_extra_arg = 1;
217
218 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
219 if (!chunk)
220 (*obstack_alloc_failed_handler) ();
221 h->next_free = h->object_base = chunk->contents;
222 h->chunk_limit = chunk->limit
223 = (char *) chunk + h->chunk_size;
224 chunk->prev = 0;
225 /* The initial chunk now contains no empty object. */
226 h->maybe_empty_object = 0;
227 h->alloc_failed = 0;
228 return 1;
229}
230
231/* Allocate a new current chunk for the obstack *H
232 on the assumption that LENGTH bytes need to be added
233 to the current object, or a new object of length LENGTH allocated.
234 Copies any partial object from the end of the old chunk
235 to the beginning of the new one. */
236
237void
238_obstack_newchunk (struct obstack *h, int length)
239{
240 register struct _obstack_chunk *old_chunk = h->chunk;
241 register struct _obstack_chunk *new_chunk;
242 register long new_size;
243 register long obj_size = h->next_free - h->object_base;
244 register long i;
245 long already;
246 char *object_base;
247
248 /* Compute size for new chunk. */
249 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
250 if (new_size < h->chunk_size)
251 new_size = h->chunk_size;
252
253 /* Allocate and initialize the new chunk. */
254 new_chunk = CALL_CHUNKFUN (h, new_size);
255 if (!new_chunk)
256 (*obstack_alloc_failed_handler) ();
257 h->chunk = new_chunk;
258 new_chunk->prev = old_chunk;
259 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
260
261 /* Compute an aligned object_base in the new chunk */
262 object_base =
263 __INT_TO_PTR ((__PTR_TO_INT (new_chunk->contents) + h->alignment_mask)
264 & ~ (h->alignment_mask));
265
266 /* Move the existing object to the new chunk.
267 Word at a time is fast and is safe if the object
268 is sufficiently aligned. */
269 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
270 {
271 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
272 i >= 0; i--)
273 ((COPYING_UNIT *)object_base)[i]
274 = ((COPYING_UNIT *)h->object_base)[i];
275 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
276 but that can cross a page boundary on a machine
277 which does not do strict alignment for COPYING_UNITS. */
278 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
279 }
280 else
281 already = 0;
282 /* Copy remaining bytes one by one. */
283 for (i = already; i < obj_size; i++)
284 object_base[i] = h->object_base[i];
285
286 /* If the object just copied was the only data in OLD_CHUNK,
287 free that chunk and remove it from the chain.
288 But not if that chunk might contain an empty object. */
289 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
290 {
291 new_chunk->prev = old_chunk->prev;
292 CALL_FREEFUN (h, old_chunk);
293 }
294
295 h->object_base = object_base;
296 h->next_free = h->object_base + obj_size;
297 /* The new chunk certainly contains no empty object yet. */
298 h->maybe_empty_object = 0;
299}
300# ifdef _LIBC
301libc_hidden_def (_obstack_newchunk)
302# endif
303
304/* Return nonzero if object OBJ has been allocated from obstack H.
305 This is here for debugging.
306 If you use it in a program, you are probably losing. */
307
308/* Suppress -Wmissing-prototypes warning. We don't want to declare this in
309 obstack.h because it is just for debugging. */
310int _obstack_allocated_p (struct obstack *h, void *obj);
311
312int
313_obstack_allocated_p (struct obstack *h, void *obj)
314{
315 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
316 register struct _obstack_chunk *plp; /* point to previous chunk if any */
317
318 lp = (h)->chunk;
319 /* We use >= rather than > since the object cannot be exactly at
320 the beginning of the chunk but might be an empty object exactly
321 at the end of an adjacent chunk. */
322 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
323 {
324 plp = lp->prev;
325 lp = plp;
326 }
327 return lp != 0;
328}
329
330
331/* Free objects in obstack H, including OBJ and everything allocate
332 more recently than OBJ. If OBJ is zero, free everything in H. */
333
334# undef obstack_free
335
336void
337obstack_free (struct obstack *h, void *obj)
338{
339 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
340 register struct _obstack_chunk *plp; /* point to previous chunk if any */
341
342 lp = h->chunk;
343 /* We use >= because there cannot be an object at the beginning of a chunk.
344 But there can be an empty object at that address
345 at the end of another chunk. */
346 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
347 {
348 plp = lp->prev;
349 CALL_FREEFUN (h, lp);
350 lp = plp;
351 /* If we switch chunks, we can't tell whether the new current
352 chunk contains an empty object, so assume that it may. */
353 h->maybe_empty_object = 1;
354 }
355 if (lp)
356 {
357 h->object_base = h->next_free = (char *) (obj);
358 h->chunk_limit = lp->limit;
359 h->chunk = lp;
360 }
361 else if (obj != 0)
362 /* obj is not in any of the chunks! */
363 abort ();
364}
365
366# ifdef _LIBC
367/* Older versions of libc used a function _obstack_free intended to be
368 called by non-GCC compilers. */
369strong_alias (obstack_free, _obstack_free)
370# endif
371
372
373int
374_obstack_memory_used (struct obstack *h)
375{
376 register struct _obstack_chunk* lp;
377 register int nbytes = 0;
378
379 for (lp = h->chunk; lp != 0; lp = lp->prev)
380 {
381 nbytes += lp->limit - (char *) lp;
382 }
383 return nbytes;
384}
385
386
387/* Define the error handler. */
388# ifdef _LIBC
389# include <libintl.h>
390# else
391# include "gettext.h"
392# endif
393# ifndef _
394# define _(msgid) gettext (msgid)
395# endif
396
397# ifdef _LIBC
398# include <libio/iolibio.h>
399# endif
400
401# ifndef __attribute__
402/* This feature is available in gcc versions 2.5 and later. */
403# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
404# define __attribute__(Spec) /* empty */
405# endif
406# endif
407
408static void
409__attribute__ ((noreturn))
410print_and_abort (void)
411{
412 /* Don't change any of these strings. Yes, it would be possible to add
413 the newline to the string and use fputs or so. But this must not
414 happen because the "memory exhausted" message appears in other places
415 like this and the translation should be reused instead of creating
416 a very similar string which requires a separate translation. */
417# if defined _LIBC && defined USE_IN_LIBIO
418 if (_IO_fwide (stderr, 0) > 0)
419 __fwprintf (stderr, L"%s\n", _("memory exhausted"));
420 else
421# endif
422 fprintf (stderr, "%s\n", _("memory exhausted"));
423 exit (obstack_exit_failure);
424}
425
426#endif /* !ELIDE_CODE */
Note: See TracBrowser for help on using the repository browser.