1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* This is just to get __GNU_LIBRARY__ defined. */
23 /* Comment out all this code if we are using the GNU C Library, and are not
24 actually compiling the library itself. This code is part of the GNU C
25 Library, but also included in many other GNU distributions. Compiling
26 and linking in this code is a waste when using the GNU C library
27 (especially if it is a shared library). Rather than having every GNU
28 program understand `configure --with-gnu-libc' and omit the object files,
29 it is simpler to just do this in the source for each such file. */
31 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
34 #if defined (__STDC__) && __STDC__
35 #define POINTER void *
37 #define POINTER char *
40 /* Determine default alignment. */
41 struct fooalign
{char x
; double d
;};
42 #define DEFAULT_ALIGNMENT \
43 ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0))
44 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
45 But in fact it might be less smart and round addresses to as much as
46 DEFAULT_ROUNDING. So we prepare for it to do that. */
47 union fooround
{long x
; double d
;};
48 #define DEFAULT_ROUNDING (sizeof (union fooround))
50 /* When we copy a long block of data, this is the unit to do it with.
51 On some machines, copying successive ints does not work;
52 in such a case, redefine COPYING_UNIT to `long' (if that works)
53 or `char' as a last resort. */
55 #define COPYING_UNIT int
58 /* The non-GNU-C macros copy the obstack into this global variable
59 to avoid multiple evaluation. */
61 struct obstack
*_obstack
;
63 /* Define a macro that either calls functions with the traditional malloc/free
64 calling interface, or calls functions with the mmalloc/mfree interface
65 (that adds an extra first argument), based on the state of use_extra_arg.
66 For free, do not use ?:, since some compilers, like the MIPS compilers,
67 do not allow (expr) ? void : void. */
69 #define CALL_CHUNKFUN(h, size) \
70 (((h) -> use_extra_arg) \
71 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
72 : (*(h)->chunkfun) ((size)))
74 #define CALL_FREEFUN(h, old_chunk) \
76 if ((h) -> use_extra_arg) \
77 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
79 (*(h)->freefun) ((old_chunk)); \
83 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
84 Objects start on multiples of ALIGNMENT (0 means use default).
85 CHUNKFUN is the function to use to allocate chunks,
86 and FREEFUN the function to free them.
88 Return nonzero if successful, zero if out of memory.
89 To recover from an out of memory error,
90 free up some memory, then call this again. */
93 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
97 POINTER (*chunkfun
) ();
100 register struct _obstack_chunk
* chunk
; /* points to new chunk */
103 alignment
= DEFAULT_ALIGNMENT
;
105 /* Default size is what GNU malloc can fit in a 4096-byte block. */
107 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
108 Use the values for range checking, because if range checking is off,
109 the extra bytes won't be missed terribly, but if range checking is on
110 and we used a larger request, a whole extra 4096 bytes would be
113 These number are irrelevant to the new GNU malloc. I suspect it is
114 less sensitive to the size of the request. */
115 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
116 + 4 + DEFAULT_ROUNDING
- 1)
117 & ~(DEFAULT_ROUNDING
- 1));
121 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
122 h
->freefun
= freefun
;
123 h
->chunk_size
= size
;
124 h
->alignment_mask
= alignment
- 1;
125 h
->use_extra_arg
= 0;
127 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
134 h
->next_free
= h
->object_base
= chunk
->contents
;
135 h
->chunk_limit
= chunk
->limit
136 = (char *) chunk
+ h
->chunk_size
;
138 /* The initial chunk now contains no empty object. */
139 h
->maybe_empty_object
= 0;
144 _obstack_begin_1 (h
, size
, alignment
, chunkfun
, freefun
, arg
)
148 POINTER (*chunkfun
) ();
152 register struct _obstack_chunk
* chunk
; /* points to new chunk */
155 alignment
= DEFAULT_ALIGNMENT
;
157 /* Default size is what GNU malloc can fit in a 4096-byte block. */
159 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
160 Use the values for range checking, because if range checking is off,
161 the extra bytes won't be missed terribly, but if range checking is on
162 and we used a larger request, a whole extra 4096 bytes would be
165 These number are irrelevant to the new GNU malloc. I suspect it is
166 less sensitive to the size of the request. */
167 int extra
= ((((12 + DEFAULT_ROUNDING
- 1) & ~(DEFAULT_ROUNDING
- 1))
168 + 4 + DEFAULT_ROUNDING
- 1)
169 & ~(DEFAULT_ROUNDING
- 1));
173 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
174 h
->freefun
= freefun
;
175 h
->chunk_size
= size
;
176 h
->alignment_mask
= alignment
- 1;
178 h
->use_extra_arg
= 1;
180 chunk
= h
->chunk
= CALL_CHUNKFUN (h
, h
-> chunk_size
);
187 h
->next_free
= h
->object_base
= chunk
->contents
;
188 h
->chunk_limit
= chunk
->limit
189 = (char *) chunk
+ h
->chunk_size
;
191 /* The initial chunk now contains no empty object. */
192 h
->maybe_empty_object
= 0;
196 /* Allocate a new current chunk for the obstack *H
197 on the assumption that LENGTH bytes need to be added
198 to the current object, or a new object of length LENGTH allocated.
199 Copies any partial object from the end of the old chunk
200 to the beginning of the new one. */
203 _obstack_newchunk (h
, length
)
207 register struct _obstack_chunk
* old_chunk
= h
->chunk
;
208 register struct _obstack_chunk
* new_chunk
;
209 register long new_size
;
210 register int obj_size
= h
->next_free
- h
->object_base
;
214 /* Compute size for new chunk. */
215 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + 100;
216 if (new_size
< h
->chunk_size
)
217 new_size
= h
->chunk_size
;
219 /* Allocate and initialize the new chunk. */
220 new_chunk
= CALL_CHUNKFUN (h
, new_size
);
227 h
->chunk
= new_chunk
;
228 new_chunk
->prev
= old_chunk
;
229 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
231 /* Move the existing object to the new chunk.
232 Word at a time is fast and is safe if the object
233 is sufficiently aligned. */
234 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
236 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
238 ((COPYING_UNIT
*)new_chunk
->contents
)[i
]
239 = ((COPYING_UNIT
*)h
->object_base
)[i
];
240 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
241 but that can cross a page boundary on a machine
242 which does not do strict alignment for COPYING_UNITS. */
243 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
247 /* Copy remaining bytes one by one. */
248 for (i
= already
; i
< obj_size
; i
++)
249 new_chunk
->contents
[i
] = h
->object_base
[i
];
251 /* If the object just copied was the only data in OLD_CHUNK,
252 free that chunk and remove it from the chain.
253 But not if that chunk might contain an empty object. */
254 if (h
->object_base
== old_chunk
->contents
&& ! h
->maybe_empty_object
)
256 new_chunk
->prev
= old_chunk
->prev
;
257 CALL_FREEFUN (h
, old_chunk
);
260 h
->object_base
= new_chunk
->contents
;
261 h
->next_free
= h
->object_base
+ obj_size
;
262 /* The new chunk certainly contains no empty object yet. */
263 h
->maybe_empty_object
= 0;
266 /* Return nonzero if object OBJ has been allocated from obstack H.
267 This is here for debugging.
268 If you use it in a program, you are probably losing. */
270 #if defined (__STDC__) && __STDC__
271 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
272 obstack.h because it is just for debugging. */
273 int _obstack_allocated_p (struct obstack
*h
, POINTER obj
);
277 _obstack_allocated_p (h
, obj
)
281 register struct _obstack_chunk
* lp
; /* below addr of any objects in this chunk */
282 register struct _obstack_chunk
* plp
; /* point to previous chunk if any */
285 /* We use >= rather than > since the object cannot be exactly at
286 the beginning of the chunk but might be an empty object exactly
287 at the end of an adjacent chunk. */
288 while (lp
!= 0 && ((POINTER
)lp
>= obj
|| (POINTER
)(lp
)->limit
< obj
))
296 /* Free objects in obstack H, including OBJ and everything allocate
297 more recently than OBJ. If OBJ is zero, free everything in H. */
301 /* This function has two names with identical definitions.
302 This is the first one, called from non-ANSI code. */
305 _obstack_free (h
, 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 */
313 /* We use >= because there cannot be an object at the beginning of a chunk.
314 But there can be an empty object at that address
315 at the end of another chunk. */
316 while (lp
!= 0 && ((POINTER
)lp
>= obj
|| (POINTER
)(lp
)->limit
< obj
))
319 CALL_FREEFUN (h
, lp
);
321 /* If we switch chunks, we can't tell whether the new current
322 chunk contains an empty object, so assume that it may. */
323 h
->maybe_empty_object
= 1;
327 h
->object_base
= h
->next_free
= (char *)(obj
);
328 h
->chunk_limit
= lp
->limit
;
332 /* obj is not in any of the chunks! */
336 /* This function is used from ANSI code. */
339 obstack_free (h
, obj
)
343 register struct _obstack_chunk
* lp
; /* below addr of any objects in this chunk */
344 register struct _obstack_chunk
* plp
; /* point to previous chunk if any */
347 /* We use >= because there cannot be an object at the beginning of a chunk.
348 But there can be an empty object at that address
349 at the end of another chunk. */
350 while (lp
!= 0 && ((POINTER
)lp
>= obj
|| (POINTER
)(lp
)->limit
< obj
))
353 CALL_FREEFUN (h
, lp
);
355 /* If we switch chunks, we can't tell whether the new current
356 chunk contains an empty object, so assume that it may. */
357 h
->maybe_empty_object
= 1;
361 h
->object_base
= h
->next_free
= (char *)(obj
);
362 h
->chunk_limit
= lp
->limit
;
366 /* obj is not in any of the chunks! */
371 /* These are now turned off because the applications do not use it
372 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
374 /* Now define the functional versions of the obstack macros.
375 Define them to simply use the corresponding macros to do the job. */
377 #if defined (__STDC__) && __STDC__
378 /* These function definitions do not work with non-ANSI preprocessors;
379 they won't pass through the macro names in parentheses. */
381 /* The function names appear in parentheses in order to prevent
382 the macro-definitions of the names from being expanded there. */
384 POINTER (obstack_base
) (obstack
)
385 struct obstack
*obstack
;
387 return obstack_base (obstack
);
390 POINTER (obstack_next_free
) (obstack
)
391 struct obstack
*obstack
;
393 return obstack_next_free (obstack
);
396 int (obstack_object_size
) (obstack
)
397 struct obstack
*obstack
;
399 return obstack_object_size (obstack
);
402 int (obstack_room
) (obstack
)
403 struct obstack
*obstack
;
405 return obstack_room (obstack
);
408 void (obstack_grow
) (obstack
, pointer
, length
)
409 struct obstack
*obstack
;
413 obstack_grow (obstack
, pointer
, length
);
416 void (obstack_grow0
) (obstack
, pointer
, length
)
417 struct obstack
*obstack
;
421 obstack_grow0 (obstack
, pointer
, length
);
424 void (obstack_1grow
) (obstack
, character
)
425 struct obstack
*obstack
;
428 obstack_1grow (obstack
, character
);
431 void (obstack_blank
) (obstack
, length
)
432 struct obstack
*obstack
;
435 obstack_blank (obstack
, length
);
438 void (obstack_1grow_fast
) (obstack
, character
)
439 struct obstack
*obstack
;
442 obstack_1grow_fast (obstack
, character
);
445 void (obstack_blank_fast
) (obstack
, length
)
446 struct obstack
*obstack
;
449 obstack_blank_fast (obstack
, length
);
452 POINTER (obstack_finish
) (obstack
)
453 struct obstack
*obstack
;
455 return obstack_finish (obstack
);
458 POINTER (obstack_alloc
) (obstack
, length
)
459 struct obstack
*obstack
;
462 return obstack_alloc (obstack
, length
);
465 POINTER (obstack_copy
) (obstack
, pointer
, length
)
466 struct obstack
*obstack
;
470 return obstack_copy (obstack
, pointer
, length
);
473 POINTER (obstack_copy0
) (obstack
, pointer
, length
)
474 struct obstack
*obstack
;
478 return obstack_copy0 (obstack
, pointer
, length
);
481 #endif /* __STDC__ */
485 #endif /* _LIBC or not __GNU_LIBRARY__. */