1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988 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. */
21 #define POINTER void *
23 #define POINTER char *
26 /* Determine default alignment. */
33 #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
34 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
35 But in fact it might be less smart and round addresses to as much as
36 DEFAULT_ROUNDING. So we prepare for it to do that. */
43 #define DEFAULT_ROUNDING (sizeof (union fooround))
45 /* When we copy a long block of data, this is the unit to do it with.
46 On some machines, copying successive ints does not work;
47 in such a case, redefine COPYING_UNIT to `long' (if that works)
48 or `char' as a last resort. */
50 #define COPYING_UNIT int
53 /* The non-GNU-C macros copy the obstack into this global variable
54 to avoid multiple evaluation. */
56 struct obstack *_obstack;
58 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
59 Objects start on multiples of ALIGNMENT (0 means use default).
60 CHUNKFUN is the function to use to allocate chunks,
61 and FREEFUN the function to free them. */
64 _obstack_begin (h, size, alignment, chunkfun, freefun)
68 POINTER (*chunkfun) ();
71 register struct _obstack_chunk *chunk; /* points to new chunk */
74 alignment = DEFAULT_ALIGNMENT;
76 /* Default size is what GNU malloc can fit in a 4096-byte block. */
78 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
79 Use the values for range checking, because if range checking is off,
80 the extra bytes won't be missed terribly, but if range checking is on
81 and we used a larger request, a whole extra 4096 bytes would be
84 These number are irrelevant to the new GNU malloc. I suspect it is
85 less sensitive to the size of the request. */
86 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
87 + 4 + DEFAULT_ROUNDING - 1)
88 & ~(DEFAULT_ROUNDING - 1));
92 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
95 h->alignment_mask = alignment - 1;
97 chunk = h->chunk = (*h->chunkfun) (h->chunk_size);
98 h->next_free = h->object_base = chunk->contents;
99 h->chunk_limit = chunk->limit
100 = (char *) chunk + h->chunk_size;
102 /* The initial chunk now contains no empty object. */
103 h->maybe_empty_object = 0;
106 /* Allocate a new current chunk for the obstack *H
107 on the assumption that LENGTH bytes need to be added
108 to the current object, or a new object of length LENGTH allocated.
109 Copies any partial object from the end of the old chunk
110 to the beginning of the new one. */
113 _obstack_newchunk (h, length)
117 register struct _obstack_chunk *old_chunk = h->chunk;
118 register struct _obstack_chunk *new_chunk;
119 register long new_size;
120 register int obj_size = h->next_free - h->object_base;
124 /* Compute size for new chunk. */
125 new_size = (obj_size + length) + (obj_size >> 3) + 100;
126 if (new_size < h->chunk_size)
127 new_size = h->chunk_size;
129 /* Allocate and initialize the new chunk. */
130 new_chunk = h->chunk = (*h->chunkfun) (new_size);
131 new_chunk->prev = old_chunk;
132 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
134 /* Move the existing object to the new chunk.
135 Word at a time is fast and is safe if the object
136 is sufficiently aligned. */
137 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
139 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
141 ((COPYING_UNIT *) new_chunk->contents)[i]
142 = ((COPYING_UNIT *) h->object_base)[i];
143 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
144 but that can cross a page boundary on a machine
145 which does not do strict alignment for COPYING_UNITS. */
146 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
150 /* Copy remaining bytes one by one. */
151 for (i = already; i < obj_size; i++)
152 new_chunk->contents[i] = h->object_base[i];
154 /* If the object just copied was the only data in OLD_CHUNK,
155 free that chunk and remove it from the chain.
156 But not if that chunk might contain an empty object. */
157 if (h->object_base == old_chunk->contents && !h->maybe_empty_object)
159 new_chunk->prev = old_chunk->prev;
160 (*h->freefun) (old_chunk);
163 h->object_base = new_chunk->contents;
164 h->next_free = h->object_base + obj_size;
165 /* The new chunk certainly contains no empty object yet. */
166 h->maybe_empty_object = 0;
169 /* Return nonzero if object OBJ has been allocated from obstack H.
170 This is here for debugging.
171 If you use it in a program, you are probably losing. */
174 _obstack_allocated_p (h, obj)
178 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
179 register struct _obstack_chunk *plp; /* point to previous chunk if any */
182 /* We use >= rather than > since the object cannot be exactly at
183 the beginning of the chunk but might be an empty object exactly
184 at the end of an adjacent chunk. */
185 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
193 /* Free objects in obstack H, including OBJ and everything allocate
194 more recently than OBJ. If OBJ is zero, free everything in H. */
198 /* This function has two names with identical definitions.
199 This is the first one, called from non-ANSI code. */
202 _obstack_free (h, obj)
206 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
207 register struct _obstack_chunk *plp; /* point to previous chunk if any */
210 /* We use >= because there cannot be an object at the beginning of a chunk.
211 But there can be an empty object at that address
212 at the end of another chunk. */
213 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
218 /* If we switch chunks, we can't tell whether the new current
219 chunk contains an empty object, so assume that it may. */
220 h->maybe_empty_object = 1;
224 h->object_base = h->next_free = (char *) (obj);
225 h->chunk_limit = lp->limit;
229 /* obj is not in any of the chunks! */
233 /* This function is used from ANSI code. */
236 obstack_free (h, obj)
240 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
241 register struct _obstack_chunk *plp; /* point to previous chunk if any */
244 /* We use >= because there cannot be an object at the beginning of a chunk.
245 But there can be an empty object at that address
246 at the end of another chunk. */
247 while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
252 /* If we switch chunks, we can't tell whether the new current
253 chunk contains an empty object, so assume that it may. */
254 h->maybe_empty_object = 1;
258 h->object_base = h->next_free = (char *) (obj);
259 h->chunk_limit = lp->limit;
263 /* obj is not in any of the chunks! */
268 /* These are now turned off because the applications do not use it
269 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
271 /* Now define the functional versions of the obstack macros.
272 Define them to simply use the corresponding macros to do the job. */
275 /* These function definitions do not work with non-ANSI preprocessors;
276 they won't pass through the macro names in parentheses. */
278 /* The function names appear in parentheses in order to prevent
279 the macro-definitions of the names from being expanded there. */
281 POINTER (obstack_base) (obstack)
282 struct obstack *obstack;
284 return obstack_base (obstack);
287 POINTER (obstack_next_free) (obstack)
288 struct obstack *obstack;
290 return obstack_next_free (obstack);
293 int (obstack_object_size) (obstack)
294 struct obstack *obstack;
296 return obstack_object_size (obstack);
299 int (obstack_room) (obstack)
300 struct obstack *obstack;
302 return obstack_room (obstack);
305 void (obstack_grow) (obstack, pointer, length)
306 struct obstack *obstack;
310 obstack_grow (obstack, pointer, length);
313 void (obstack_grow0) (obstack, pointer, length)
314 struct obstack *obstack;
318 obstack_grow0 (obstack, pointer, length);
321 void (obstack_1grow) (obstack, character)
322 struct obstack *obstack;
325 obstack_1grow (obstack, character);
328 void (obstack_blank) (obstack, length)
329 struct obstack *obstack;
332 obstack_blank (obstack, length);
335 void (obstack_1grow_fast) (obstack, character)
336 struct obstack *obstack;
339 obstack_1grow_fast (obstack, character);
342 void (obstack_blank_fast) (obstack, length)
343 struct obstack *obstack;
346 obstack_blank_fast (obstack, length);
349 POINTER (obstack_finish) (obstack)
350 struct obstack *obstack;
352 return obstack_finish (obstack);
355 POINTER (obstack_alloc) (obstack, length)
356 struct obstack *obstack;
359 return obstack_alloc (obstack, length);
362 POINTER (obstack_copy) (obstack, pointer, length)
363 struct obstack *obstack;
367 return obstack_copy (obstack, pointer, length);
370 POINTER (obstack_copy0) (obstack, pointer, length)
371 struct obstack *obstack;
375 return obstack_copy0 (obstack, pointer, length);
378 #endif /* __STDC__ */