VirtualBox

source: kBuild/vendor/sed/4.1.5/lib/obstack.c@ 599

Last change on this file since 599 was 599, checked in by bird, 18 years ago

GNU sed 4.1.5.

File size: 16.8 KB
Line 
1/* obstack.c - subroutines used implicitly by object stack macros -*- C -*-
2 Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3
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 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.
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 Library General Public License for more details.
16
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include "obstack.h"
27
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
32
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. */
41
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
49
50
51#ifndef ELIDE_CODE
52
53
54#if defined (__STDC__) && __STDC__
55#define POINTER void *
56#else
57#define POINTER char *
58#endif
59
60/* Determine default alignment. */
61struct 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. */
67union fooround {long x; double d;};
68#define DEFAULT_ROUNDING (sizeof (union fooround))
69
70#ifdef original_glibc_code
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#endif
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 variable by default points to the internal function
83 `print_and_abort'. */
84#if defined (__STDC__) && __STDC__
85static void print_and_abort (void);
86void (*obstack_alloc_failed_handler) (void) = print_and_abort;
87#else
88static void print_and_abort ();
89void (*obstack_alloc_failed_handler) () = print_and_abort;
90#endif
91
92/* Exit value used when `print_and_abort' is used. */
93#if defined __GNU_LIBRARY__ || defined HAVE_STDLIB_H
94#include <stdlib.h>
95#endif
96#ifndef EXIT_FAILURE
97#define EXIT_FAILURE 1
98#endif
99int obstack_exit_failure = EXIT_FAILURE;
100
101/* The non-GNU-C macros copy the obstack into this global variable
102 to avoid multiple evaluation. */
103
104struct obstack *_obstack;
105
106/* Define a macro that either calls functions with the traditional malloc/free
107 calling interface, or calls functions with the mmalloc/mfree interface
108 (that adds an extra first argument), based on the state of use_extra_arg.
109 For free, do not use ?:, since some compilers, like the MIPS compilers,
110 do not allow (expr) ? void : void. */
111
112#if defined (__STDC__) && __STDC__
113#define CALL_CHUNKFUN(h, size) \
114 (((h) -> use_extra_arg) \
115 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
116 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
117
118#define CALL_FREEFUN(h, old_chunk) \
119 do { \
120 if ((h) -> use_extra_arg) \
121 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
122 else \
123 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
124 } while (0)
125#else
126#define CALL_CHUNKFUN(h, size) \
127 (((h) -> use_extra_arg) \
128 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
129 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
130
131#define CALL_FREEFUN(h, old_chunk) \
132 do { \
133 if ((h) -> use_extra_arg) \
134 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
135 else \
136 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
137 } while (0)
138#endif
139
140
141
142/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
143 Objects start on multiples of ALIGNMENT (0 means use default).
144 CHUNKFUN is the function to use to allocate chunks,
145 and FREEFUN the function to free them.
146
147 Return nonzero if successful, zero if out of memory.
148 To recover from an out of memory error,
149 free up some memory, then call this again. */
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 = 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 = 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 int obj_size = h->next_free - h->object_base;
284
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;
289
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;
297
298 _obstack_memcpy(new_chunk->contents, h->object_base, obj_size);
299
300 /* If the object just copied was the only data in OLD_CHUNK, */
301 /* free that chunk and remove it from the chain. */
302 /* But not if that chunk might contain an empty object. */
303 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
304 {
305 new_chunk->prev = old_chunk->prev;
306 CALL_FREEFUN (h, old_chunk);
307 }
308
309 h->object_base = new_chunk->contents;
310 h->next_free = h->object_base + obj_size;
311 /* The new chunk certainly contains no empty object yet. */
312 h->maybe_empty_object = 0;
313}
314
315/* Return nonzero if object OBJ has been allocated from obstack H.
316 This is here for debugging.
317 If you use it in a program, you are probably losing. */
318
319#if defined (__STDC__) && __STDC__
320/* Suppress -Wmissing-prototypes warning. We don't want to declare this in
321 obstack.h because it is just for debugging. */
322int _obstack_allocated_p (struct obstack *h, POINTER obj);
323#endif
324
325int
326_obstack_allocated_p (h, obj)
327 struct obstack *h;
328 POINTER obj;
329{
330 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
331 register struct _obstack_chunk *plp; /* point to previous chunk if any */
332
333 lp = (h)->chunk;
334 /* We use >= rather than > since the object cannot be exactly at
335 the beginning of the chunk but might be an empty object exactly
336 at the end of an adjacent chunk. */
337 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
338 {
339 plp = lp->prev;
340 lp = plp;
341 }
342 return lp != 0;
343}
344
345
346/* Free objects in obstack H, including OBJ and everything allocate
347 more recently than OBJ. If OBJ is zero, free everything in H. */
348
349#undef obstack_free
350
351/* This function has two names with identical definitions.
352 This is the first one, called from non-ANSI code. */
353
354void
355_obstack_free (h, obj)
356 struct obstack *h;
357 POINTER obj;
358{
359 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
360 register struct _obstack_chunk *plp; /* point to previous chunk if any */
361
362 lp = h->chunk;
363 /* We use >= because there cannot be an object at the beginning of a chunk.
364 But there can be an empty object at that address
365 at the end of another chunk. */
366 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
367 {
368 plp = lp->prev;
369 CALL_FREEFUN (h, lp);
370 lp = plp;
371 /* If we switch chunks, we can't tell whether the new current
372 chunk contains an empty object, so assume that it may. */
373 h->maybe_empty_object = 1;
374 }
375 if (lp)
376 {
377 h->object_base = h->next_free = (char *) (obj);
378 h->chunk_limit = lp->limit;
379 h->chunk = lp;
380 }
381 else if (obj != 0)
382 /* obj is not in any of the chunks! */
383 abort ();
384}
385
386/* This function is used from ANSI code. */
387
388void
389obstack_free (h, obj)
390 struct obstack *h;
391 POINTER obj;
392{
393 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
394 register struct _obstack_chunk *plp; /* point to previous chunk if any */
395
396 lp = h->chunk;
397 /* We use >= because there cannot be an object at the beginning of a chunk.
398 But there can be an empty object at that address
399 at the end of another chunk. */
400 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
401 {
402 plp = lp->prev;
403 CALL_FREEFUN (h, lp);
404 lp = plp;
405 /* If we switch chunks, we can't tell whether the new current
406 chunk contains an empty object, so assume that it may. */
407 h->maybe_empty_object = 1;
408 }
409 if (lp)
410 {
411 h->object_base = h->next_free = (char *) (obj);
412 h->chunk_limit = lp->limit;
413 h->chunk = lp;
414 }
415 else if (obj != 0)
416 /* obj is not in any of the chunks! */
417 abort ();
418}
419
420
421int
422_obstack_memory_used (h)
423 struct obstack *h;
424{
425 register struct _obstack_chunk* lp;
426 register int nbytes = 0;
427
428 for (lp = h->chunk; lp != 0; lp = lp->prev)
429 {
430 nbytes += lp->limit - (char *) lp;
431 }
432 return nbytes;
433}
434
435
436/* Define the error handler. */
437#ifndef _
438# ifdef HAVE_LIBINTL_H
439# include <libintl.h>
440# ifndef _
441# define _(Str) gettext (Str)
442# endif
443# else
444# define _(Str) (Str)
445# endif
446#endif
447
448static void
449print_and_abort ()
450{
451 fputs (_("memory exhausted\n"), stderr);
452 exit (obstack_exit_failure);
453}
454
455
456#if 0
457/* These are now turned off because the applications do not use it
458 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
459
460/* Now define the functional versions of the obstack macros.
461 Define them to simply use the corresponding macros to do the job. */
462
463#if defined (__STDC__) && __STDC__
464/* These function definitions do not work with non-ANSI preprocessors;
465 they won't pass through the macro names in parentheses. */
466
467/* The function names appear in parentheses in order to prevent
468 the macro-definitions of the names from being expanded there. */
469
470POINTER (obstack_base) (obstack)
471 struct obstack *obstack;
472{
473 return obstack_base (obstack);
474}
475
476POINTER (obstack_next_free) (obstack)
477 struct obstack *obstack;
478{
479 return obstack_next_free (obstack);
480}
481
482int (obstack_object_size) (obstack)
483 struct obstack *obstack;
484{
485 return obstack_object_size (obstack);
486}
487
488int (obstack_room) (obstack)
489 struct obstack *obstack;
490{
491 return obstack_room (obstack);
492}
493
494int (obstack_make_room) (obstack, length)
495 struct obstack *obstack;
496 int length;
497{
498 return obstack_make_room (obstack, length);
499}
500
501void (obstack_grow) (obstack, pointer, length)
502 struct obstack *obstack;
503 POINTER pointer;
504 int length;
505{
506 obstack_grow (obstack, pointer, length);
507}
508
509void (obstack_grow0) (obstack, pointer, length)
510 struct obstack *obstack;
511 POINTER pointer;
512 int length;
513{
514 obstack_grow0 (obstack, pointer, length);
515}
516
517void (obstack_1grow) (obstack, character)
518 struct obstack *obstack;
519 int character;
520{
521 obstack_1grow (obstack, character);
522}
523
524void (obstack_blank) (obstack, length)
525 struct obstack *obstack;
526 int length;
527{
528 obstack_blank (obstack, length);
529}
530
531void (obstack_1grow_fast) (obstack, character)
532 struct obstack *obstack;
533 int character;
534{
535 obstack_1grow_fast (obstack, character);
536}
537
538void (obstack_blank_fast) (obstack, length)
539 struct obstack *obstack;
540 int length;
541{
542 obstack_blank_fast (obstack, length);
543}
544
545POINTER (obstack_finish) (obstack)
546 struct obstack *obstack;
547{
548 return obstack_finish (obstack);
549}
550
551POINTER (obstack_alloc) (obstack, length)
552 struct obstack *obstack;
553 int length;
554{
555 return obstack_alloc (obstack, length);
556}
557
558POINTER (obstack_copy) (obstack, pointer, length)
559 struct obstack *obstack;
560 POINTER pointer;
561 int length;
562{
563 return obstack_copy (obstack, pointer, length);
564}
565
566POINTER (obstack_copy0) (obstack, pointer, length)
567 struct obstack *obstack;
568 POINTER pointer;
569 int length;
570{
571 return obstack_copy0 (obstack, pointer, length);
572}
573
574#endif /* __STDC__ */
575
576#endif /* 0 */
577
578#endif /* !ELIDE_CODE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette