1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
21 incremented whenever callers compiled using an old obstack.h can no
22 longer properly call the functions in this obstack.c. */
23 #define OBSTACK_INTERFACE_VERSION 1
25 /* Comment out all this code if we are using the GNU C Library, and are not
26 actually compiling the library itself, and the installed library
27 supports the same library interface we do. This code is part of the GNU
28 C Library, but also included in many other GNU distributions. Compiling
29 and linking in this code is a waste when using the GNU C library
30 (especially if it is a shared library). Rather than having every GNU
31 program understand `configure --with-gnu-libc' and omit the object
32 files, it is simpler to just do this in the source for each such file. */
34 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
35 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
36 #include <gnu-versions.h>
37 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
42 /* CYGNUS LOCAL (not to be elided!) */
45 _obstack_memory_used (h
)
48 register struct _obstack_chunk
* lp
;
49 register int nbytes
= 0;
51 for (lp
= h
->chunk
; lp
!= 0; lp
= lp
->prev
)
53 nbytes
+= lp
->limit
- (char *) lp
;
58 /* END CYGNUS LOCAL */
63 #if defined (__STDC__) && __STDC__
64 #define POINTER void *
66 #define POINTER char *
69 /* Determine default alignment. */
70 struct fooalign
{char x
; double d
;};
71 #define DEFAULT_ALIGNMENT \
72 ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
73 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
74 But in fact it might be less smart and round addresses to as much as
75 DEFAULT_ROUNDING. So we prepare for it to do that. */
76 union fooround
{long x
; double d
;};
77 #define DEFAULT_ROUNDING (sizeof (union fooround))
79 /* When we copy a long block of data, this is the unit to do it with.
80 On some machines, copying successive ints does not work;
81 in such a case, redefine COPYING_UNIT to `long' (if that works)
82 or `char' as a last resort. */
84 #define COPYING_UNIT int
87 /* The non-GNU-C macros copy the obstack into this global variable
88 to avoid multiple evaluation. */
90 struct obstack
*_obstack
;
92 /* Define a macro that either calls functions with the traditional malloc/free
93 calling interface, or calls functions with the mmalloc/mfree interface
94 (that adds an extra first argument), based on the state of use_extra_arg.
95 For free, do not use ?:, since some compilers, like the MIPS compilers,
96 do not allow (expr) ? void : void. */
98 #define CALL_CHUNKFUN(h, size) \
99 (((h) -> use_extra_arg) \
100 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
101 : (*(struct _obstack_chunk *(*) ()) (h)->chunkfun) ((size)))
103 #define CALL_FREEFUN(h, old_chunk) \
105 if ((h) -> use_extra_arg) \
106 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
108 (*(void (*) ()) (h)->freefun) ((old_chunk)); \
112 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
113 Objects start on multiples of ALIGNMENT (0 means use default).
114 CHUNKFUN is the function to use to allocate chunks,
115 and FREEFUN the function to free them.
117 Return nonzero if successful, zero if out of memory.
118 To recover from an out of memory error,
119 free up some memory, then call this again. */
122 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
126 POINTER (*chunkfun
) ();
129 register struct _obstack_chunk
*chunk
; /* points to new chunk */
132 alignment
= DEFAULT_ALIGNMENT
;
134 /* Default size is what GNU malloc can fit in a 4096-byte block. */
136 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
137 Use the values for range checking, because if range checking is off,
138 the extra bytes won't be missed terribly, but if range checking is on
139 and we used a larger request, a whole extra 4096 bytes would be
142 These number are irrelevant to the new GNU malloc. I suspect it is
143 less sensitive to the size of the request. */
144 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
145 + 4 + DEFAULT_ROUNDING
- 1)
146 & ~(DEFAULT_ROUNDING
- 1));
150 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
151 h
->freefun
= freefun
;
152 h
->chunk_size
= size
;
153 h
->alignment_mask
= alignment
- 1;
154 h
->use_extra_arg
= 0;
156 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
163 h
->next_free
= h
->object_base
= chunk
->contents
;
164 h
->chunk_limit
= chunk
->limit
165 = (char *) chunk
+ h
->chunk_size
;
167 /* The initial chunk now contains no empty object. */
168 h
->maybe_empty_object
= 0;
173 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
177 POINTER (*chunkfun
) ();
181 register struct _obstack_chunk
*chunk
; /* points to new chunk */
184 alignment
= DEFAULT_ALIGNMENT
;
186 /* Default size is what GNU malloc can fit in a 4096-byte block. */
188 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
189 Use the values for range checking, because if range checking is off,
190 the extra bytes won't be missed terribly, but if range checking is on
191 and we used a larger request, a whole extra 4096 bytes would be
194 These number are irrelevant to the new GNU malloc. I suspect it is
195 less sensitive to the size of the request. */
196 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
197 + 4 + DEFAULT_ROUNDING
- 1)
198 & ~(DEFAULT_ROUNDING
- 1));
202 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
203 h
->freefun
= freefun
;
204 h
->chunk_size
= size
;
205 h
->alignment_mask
= alignment
- 1;
207 h
->use_extra_arg
= 1;
209 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
216 h
->next_free
= h
->object_base
= chunk
->contents
;
217 h
->chunk_limit
= chunk
->limit
218 = (char *) chunk
+ h
->chunk_size
;
220 /* The initial chunk now contains no empty object. */
221 h
->maybe_empty_object
= 0;
225 /* Allocate a new current chunk for the obstack *H
226 on the assumption that LENGTH bytes need to be added
227 to the current object, or a new object of length LENGTH allocated.
228 Copies any partial object from the end of the old chunk
229 to the beginning of the new one. */
232 _obstack_newchunk (h
, length
)
236 register struct _obstack_chunk
*old_chunk
= h
->chunk
;
237 register struct _obstack_chunk
*new_chunk
;
238 register long new_size
;
239 register int obj_size
= h
->next_free
- h
->object_base
;
243 /* Compute size for new chunk. */
244 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + 100;
245 if (new_size
< h
->chunk_size
)
246 new_size
= h
->chunk_size
;
248 /* Allocate and initialize the new chunk. */
249 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
256 h
->chunk
= new_chunk
;
257 new_chunk
->prev
= old_chunk
;
258 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
260 /* Move the existing object to the new chunk.
261 Word at a time is fast and is safe if the object
262 is sufficiently aligned. */
263 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
265 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
267 ((COPYING_UNIT
*)new_chunk
->contents
)[i
]
268 = ((COPYING_UNIT
*)h
->object_base
)[i
];
269 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
270 but that can cross a page boundary on a machine
271 which does not do strict alignment for COPYING_UNITS. */
272 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
276 /* Copy remaining bytes one by one. */
277 for (i
= already
; i
< obj_size
; i
++)
278 new_chunk
->contents
[i
] = h
->object_base
[i
];
280 /* If the object just copied was the only data in OLD_CHUNK,
281 free that chunk and remove it from the chain.
282 But not if that chunk might contain an empty object. */
283 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
285 new_chunk
->prev
= old_chunk
->prev
;
286 CALL_FREEFUN (h
, old_chunk
);
289 h
->object_base
= new_chunk
->contents
;
290 h
->next_free
= h
->object_base
+ obj_size
;
291 /* The new chunk certainly contains no empty object yet. */
292 h
->maybe_empty_object
= 0;
295 /* Return nonzero if object OBJ has been allocated from obstack H.
296 This is here for debugging.
297 If you use it in a program, you are probably losing. */
299 #if defined (__STDC__) && __STDC__
300 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
301 obstack.h because it is just for debugging. */
302 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
306 _obstack_allocated_p (h
, obj
)
310 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
311 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
314 /* We use >= rather than > since the object cannot be exactly at
315 the beginning of the chunk but might be an empty object exactly
316 at the end of an adjacent chunk. */
317 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
325 /* Free objects in obstack H, including OBJ and everything allocate
326 more recently than OBJ. If OBJ is zero, free everything in H. */
330 /* This function has two names with identical definitions.
331 This is the first one, called from non-ANSI code. */
334 _obstack_free (h
, obj
)
338 register struct _obstack_chunk
*lp
; /* below addr of any objects in this chunk */
339 register struct _obstack_chunk
*plp
; /* point to previous chunk if any */
342 /* We use >= because there cannot be an object at the beginning of a chunk.
343 But there can be an empty object at that address
344 at the end of another chunk. */
345 while (lp
!= 0 && ((POINTER
) lp
>= obj
|| (POINTER
) (lp
)->limit
< obj
))
348 CALL_FREEFUN (h
, lp
);
350 /* If we switch chunks, we can't tell whether the new current
351 chunk contains an empty object, so assume that it may. */
352 h
->maybe_empty_object
= 1;
356 h
->object_base
= h
->next_free
= (char *) (obj
);
357 h
->chunk_limit
= lp
->limit
;
361 /* obj is not in any of the chunks! */
365 /* This function is used from ANSI code. */
368 obstack_free (h
, 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 */
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
))
382 CALL_FREEFUN (h
, lp
);
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;
390 h
->object_base
= h
->next_free
= (char *) (obj
);
391 h
->chunk_limit
= lp
->limit
;
395 /* obj is not in any of the chunks! */
400 /* These are now turned off because the applications do not use it
401 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
403 /* Now define the functional versions of the obstack macros.
404 Define them to simply use the corresponding macros to do the job. */
406 #if defined (__STDC__) && __STDC__
407 /* These function definitions do not work with non-ANSI preprocessors;
408 they won't pass through the macro names in parentheses. */
410 /* The function names appear in parentheses in order to prevent
411 the macro-definitions of the names from being expanded there. */
413 POINTER (obstack_base
) (obstack
)
414 struct obstack
*obstack
;
416 return obstack_base (obstack
);
419 POINTER (obstack_next_free
) (obstack
)
420 struct obstack
*obstack
;
422 return obstack_next_free (obstack
);
425 int (obstack_object_size
) (obstack
)
426 struct obstack
*obstack
;
428 return obstack_object_size (obstack
);
431 int (obstack_room
) (obstack
)
432 struct obstack
*obstack
;
434 return obstack_room (obstack
);
437 void (obstack_grow
) (obstack
, pointer
, length
)
438 struct obstack
*obstack
;
442 obstack_grow (obstack
, pointer
, length
);
445 void (obstack_grow0
) (obstack
, pointer
, length
)
446 struct obstack
*obstack
;
450 obstack_grow0 (obstack
, pointer
, length
);
453 void (obstack_1grow
) (obstack
, character
)
454 struct obstack
*obstack
;
457 obstack_1grow (obstack
, character
);
460 void (obstack_blank
) (obstack
, length
)
461 struct obstack
*obstack
;
464 obstack_blank (obstack
, length
);
467 void (obstack_1grow_fast
) (obstack
, character
)
468 struct obstack
*obstack
;
471 obstack_1grow_fast (obstack
, character
);
474 void (obstack_blank_fast
) (obstack
, length
)
475 struct obstack
*obstack
;
478 obstack_blank_fast (obstack
, length
);
481 POINTER (obstack_finish
) (obstack
)
482 struct obstack
*obstack
;
484 return obstack_finish (obstack
);
487 POINTER (obstack_alloc
) (obstack
, length
)
488 struct obstack
*obstack
;
491 return obstack_alloc (obstack
, length
);
494 POINTER (obstack_copy
) (obstack
, pointer
, length
)
495 struct obstack
*obstack
;
499 return obstack_copy (obstack
, pointer
, length
);
502 POINTER (obstack_copy0
) (obstack
, pointer
, length
)
503 struct obstack
*obstack
;
507 return obstack_copy0 (obstack
, pointer
, length
);
510 #endif /* __STDC__ */
514 #endif /* !ELIDE_CODE */