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. */
19 * $FreeBSD: src/gnu/usr.bin/as/obstack.c,v 1.5 1999/08/27 23:34:19 peter Exp $
20 * $DragonFly: src/gnu/usr.bin/as/Attic/obstack.c,v 1.2 2003/06/17 04:25:44 dillon Exp $
25 #define POINTER void *
27 #define POINTER char *
30 /* Determine default alignment. */
31 struct fooalign {char x; double d;};
32 #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
33 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
34 But in fact it might be less smart and round addresses to as much as
35 DEFAULT_ROUNDING. So we prepare for it to do that. */
36 union fooround {long x; double d;};
37 #define DEFAULT_ROUNDING (sizeof (union fooround))
39 /* When we copy a long block of data, this is the unit to do it with.
40 On some machines, copying successive ints does not work;
41 in such a case, redefine COPYING_UNIT to `long' (if that works)
42 or `char' as a last resort. */
44 #define COPYING_UNIT int
47 /* The non-GNU-C macros copy the obstack into this global variable
48 to avoid multiple evaluation. */
50 struct obstack *_obstack;
52 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
53 Objects start on multiples of ALIGNMENT (0 means use default).
54 CHUNKFUN is the function to use to allocate chunks,
55 and FREEFUN the function to free them. */
58 _obstack_begin (h, size, alignment, chunkfun, freefun)
62 POINTER (*chunkfun) ();
65 register struct _obstack_chunk* chunk; /* points to new chunk */
68 alignment = DEFAULT_ALIGNMENT;
70 /* Default size is what GNU malloc can fit in a 4096-byte block. */
72 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
73 Use the values for range checking, because if range checking is off,
74 the extra bytes won't be missed terribly, but if range checking is on
75 and we used a larger request, a whole extra 4096 bytes would be
78 These number are irrelevant to the new GNU malloc. I suspect it is
79 less sensitive to the size of the request. */
80 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
81 + 4 + DEFAULT_ROUNDING - 1)
82 & ~(DEFAULT_ROUNDING - 1));
86 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
89 h->alignment_mask = alignment - 1;
91 chunk = h->chunk = (*h->chunkfun) (h->chunk_size);
92 h->next_free = h->object_base = chunk->contents;
93 h->chunk_limit = chunk->limit
94 = (char *) chunk + h->chunk_size;
96 /* The initial chunk now contains no empty object. */
97 h->maybe_empty_object = 0;
100 /* Allocate a new current chunk for the obstack *H
101 on the assumption that LENGTH bytes need to be added
102 to the current object, or a new object of length LENGTH allocated.
103 Copies any partial object from the end of the old chunk
104 to the beginning of the new one. */
107 _obstack_newchunk (h, length)
111 register struct _obstack_chunk* old_chunk = h->chunk;
112 register struct _obstack_chunk* new_chunk;
113 register long new_size;
114 register int obj_size = h->next_free - h->object_base;
118 /* Compute size for new chunk. */
119 new_size = (obj_size + length) + (obj_size >> 3) + 100;
120 if (new_size < h->chunk_size)
121 new_size = h->chunk_size;
123 /* Allocate and initialize the new chunk. */
124 new_chunk = h->chunk = (*h->chunkfun) (new_size);
125 new_chunk->prev = old_chunk;
126 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
128 /* Move the existing object to the new chunk.
129 Word at a time is fast and is safe if the object
130 is sufficiently aligned. */
131 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
133 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
135 ((COPYING_UNIT *)new_chunk->contents)[i]
136 = ((COPYING_UNIT *)h->object_base)[i];
137 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
138 but that can cross a page boundary on a machine
139 which does not do strict alignment for COPYING_UNITS. */
140 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
144 /* Copy remaining bytes one by one. */
145 for (i = already; i < obj_size; i++)
146 new_chunk->contents[i] = h->object_base[i];
148 /* If the object just copied was the only data in OLD_CHUNK,
149 free that chunk and remove it from the chain.
150 But not if that chunk might contain an empty object. */
151 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
153 new_chunk->prev = old_chunk->prev;
154 (*h->freefun) (old_chunk);
157 h->object_base = new_chunk->contents;
158 h->next_free = h->object_base + obj_size;
159 /* The new chunk certainly contains no empty object yet. */
160 h->maybe_empty_object = 0;
163 /* Return nonzero if object OBJ has been allocated from obstack H.
164 This is here for debugging.
165 If you use it in a program, you are probably losing. */
168 _obstack_allocated_p (h, obj)
172 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
173 register struct _obstack_chunk* plp; /* point to previous chunk if any */
176 /* We use >= rather than > since the object cannot be exactly at
177 the beginning of the chunk but might be an empty object exactly
178 at the end of an adjacent chunk. */
179 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
187 /* Free objects in obstack H, including OBJ and everything allocate
188 more recently than OBJ. If OBJ is zero, free everything in H. */
192 /* This function has two names with identical definitions.
193 This is the first one, called from non-ANSI code. */
196 _obstack_free (h, obj)
200 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
201 register struct _obstack_chunk* plp; /* point to previous chunk if any */
204 /* We use >= because there cannot be an object at the beginning of a chunk.
205 But there can be an empty object at that address
206 at the end of another chunk. */
207 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
212 /* If we switch chunks, we can't tell whether the new current
213 chunk contains an empty object, so assume that it may. */
214 h->maybe_empty_object = 1;
218 h->object_base = h->next_free = (char *)(obj);
219 h->chunk_limit = lp->limit;
223 /* obj is not in any of the chunks! */
227 /* This function is used from ANSI code. */
230 obstack_free (h, obj)
234 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
235 register struct _obstack_chunk* plp; /* point to previous chunk if any */
238 /* We use >= because there cannot be an object at the beginning of a chunk.
239 But there can be an empty object at that address
240 at the end of another chunk. */
241 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
246 /* If we switch chunks, we can't tell whether the new current
247 chunk contains an empty object, so assume that it may. */
248 h->maybe_empty_object = 1;
252 h->object_base = h->next_free = (char *)(obj);
253 h->chunk_limit = lp->limit;
257 /* obj is not in any of the chunks! */
262 /* These are now turned off because the applications do not use it
263 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
265 /* Now define the functional versions of the obstack macros.
266 Define them to simply use the corresponding macros to do the job. */
269 /* These function definitions do not work with non-ANSI preprocessors;
270 they won't pass through the macro names in parentheses. */
272 /* The function names appear in parentheses in order to prevent
273 the macro-definitions of the names from being expanded there. */
275 POINTER (obstack_base) (obstack)
276 struct obstack *obstack;
278 return obstack_base (obstack);
281 POINTER (obstack_next_free) (obstack)
282 struct obstack *obstack;
284 return obstack_next_free (obstack);
287 int (obstack_object_size) (obstack)
288 struct obstack *obstack;
290 return obstack_object_size (obstack);
293 int (obstack_room) (obstack)
294 struct obstack *obstack;
296 return obstack_room (obstack);
299 void (obstack_grow) (obstack, pointer, length)
300 struct obstack *obstack;
304 obstack_grow (obstack, pointer, length);
307 void (obstack_grow0) (obstack, pointer, length)
308 struct obstack *obstack;
312 obstack_grow0 (obstack, pointer, length);
315 void (obstack_1grow) (obstack, character)
316 struct obstack *obstack;
319 obstack_1grow (obstack, character);
322 void (obstack_blank) (obstack, length)
323 struct obstack *obstack;
326 obstack_blank (obstack, length);
329 void (obstack_1grow_fast) (obstack, character)
330 struct obstack *obstack;
333 obstack_1grow_fast (obstack, character);
336 void (obstack_blank_fast) (obstack, length)
337 struct obstack *obstack;
340 obstack_blank_fast (obstack, length);
343 POINTER (obstack_finish) (obstack)
344 struct obstack *obstack;
346 return obstack_finish (obstack);
349 POINTER (obstack_alloc) (obstack, length)
350 struct obstack *obstack;
353 return obstack_alloc (obstack, length);
356 POINTER (obstack_copy) (obstack, pointer, length)
357 struct obstack *obstack;
361 return obstack_copy (obstack, pointer, length);
364 POINTER (obstack_copy0) (obstack, pointer, length)
365 struct obstack *obstack;
369 return obstack_copy0 (obstack, pointer, length);
372 #endif /* __STDC__ */