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 1, 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. */
27 struct fooalign {char x; double d;};
28 #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
29 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
30 But in fact it might be less smart and round addresses to as much as
31 DEFAULT_ROUNDING. So we prepare for it to do that. */
32 union fooround {long x; double d;};
33 #define DEFAULT_ROUNDING (sizeof (union fooround))
35 /* When we copy a long block of data, this is the unit to do it with.
36 On some machines, copying successive ints does not work;
37 in such a case, redefine COPYING_UNIT to `long' (if that works)
38 or `char' as a last resort. */
40 #define COPYING_UNIT int
43 /* The non-GNU-C macros copy the obstack into this global variable
44 to avoid multiple evaluation. */
46 struct obstack *_obstack;
48 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
49 Objects start on multiples of ALIGNMENT (0 means use default).
50 CHUNKFUN is the function to use to allocate chunks,
51 and FREEFUN the function to free them. */
54 _obstack_begin (h, size, alignment, chunkfun, freefun)
58 POINTER (*chunkfun) ();
61 register struct _obstack_chunk* chunk; /* points to new chunk */
64 alignment = DEFAULT_ALIGNMENT;
66 /* Default size is what GNU malloc can fit in a 4096-byte block. */
68 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
69 Use the values for range checking, because if range checking is off,
70 the extra bytes won't be missed terribly, but if range checking is on
71 and we used a larger request, a whole extra 4096 bytes would be
74 These number are irrelevant to the new GNU malloc. I suspect it is
75 less sensitive to the size of the request. */
76 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
77 + 4 + DEFAULT_ROUNDING - 1)
78 & ~(DEFAULT_ROUNDING - 1));
82 h->chunkfun = chunkfun;
85 h->alignment_mask = alignment - 1;
87 chunk = h->chunk = (struct _obstack_chunk *)(*h->chunkfun) (h->chunk_size);
88 h->next_free = h->object_base = chunk->contents;
89 h->chunk_limit = chunk->limit
90 = (char *) chunk + h->chunk_size;
94 /* Allocate a new current chunk for the obstack *H
95 on the assumption that LENGTH bytes need to be added
96 to the current object, or a new object of length LENGTH allocated.
97 Copies any partial object from the end of the old chunk
98 to the beginning of the new one.
100 The function must be "int" so it can be used in non-ANSI C
101 compilers in a : expression. */
104 _obstack_newchunk (h, length)
108 register struct _obstack_chunk* old_chunk = h->chunk;
109 register struct _obstack_chunk* new_chunk;
110 register long new_size;
111 register int obj_size = h->next_free - h->object_base;
115 /* Compute size for new chunk. */
116 new_size = (obj_size + length) + (obj_size >> 3) + 100;
117 if (new_size < h->chunk_size)
118 new_size = h->chunk_size;
120 /* Allocate and initialize the new chunk. */
121 new_chunk = h->chunk = (struct _obstack_chunk *)(*h->chunkfun) (new_size);
122 new_chunk->prev = old_chunk;
123 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
125 /* Move the existing object to the new chunk.
126 Word at a time is fast and is safe if the object
127 is sufficiently aligned. */
128 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
130 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
132 ((COPYING_UNIT *)new_chunk->contents)[i]
133 = ((COPYING_UNIT *)h->object_base)[i];
134 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
135 but that can cross a page boundary on a machine
136 which does not do strict alignment for COPYING_UNITS. */
137 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
141 /* Copy remaining bytes one by one. */
142 for (i = already; i < obj_size; i++)
143 new_chunk->contents[i] = h->object_base[i];
145 h->object_base = new_chunk->contents;
146 h->next_free = h->object_base + obj_size;
149 /* Return nonzero if object OBJ has been allocated from obstack H.
150 This is here for debugging.
151 If you use it in a program, you are probably losing. */
154 _obstack_allocated_p (h, obj)
158 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
159 register struct _obstack_chunk* plp; /* point to previous chunk if any */
162 while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
170 /* Free objects in obstack H, including OBJ and everything allocate
171 more recently than OBJ. If OBJ is zero, free everything in H. */
176 obstack_free (struct obstack *h, POINTER obj)
179 _obstack_free (h, obj)
184 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
185 register struct _obstack_chunk* plp; /* point to previous chunk if any */
188 /* We use >= because there cannot be an object at the beginning of a chunk.
189 But there can be an empty object at that address
190 at the end of another chunk. */
191 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
194 (*h->freefun) ((POINTER) lp);
199 (h)->object_base = (h)->next_free = (char *)(obj);
200 (h)->chunk_limit = lp->limit;
204 /* obj is not in any of the chunks! */
208 /* Let same .o link with output of gcc and other compilers. */
212 _obstack_free (h, obj)
216 obstack_free (h, obj);
221 /* These are now turned off because the applications do not use it
222 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
224 /* Now define the functional versions of the obstack macros.
225 Define them to simply use the corresponding macros to do the job. */
228 /* These function definitions do not work with non-ANSI preprocessors;
229 they won't pass through the macro names in parentheses. */
231 /* The function names appear in parentheses in order to prevent
232 the macro-definitions of the names from being expanded there. */
234 POINTER (obstack_base) (obstack)
235 struct obstack *obstack;
237 return obstack_base (obstack);
240 POINTER (obstack_next_free) (obstack)
241 struct obstack *obstack;
243 return obstack_next_free (obstack);
246 int (obstack_object_size) (obstack)
247 struct obstack *obstack;
249 return obstack_object_size (obstack);
252 int (obstack_room) (obstack)
253 struct obstack *obstack;
255 return obstack_room (obstack);
258 void (obstack_grow) (obstack, pointer, length)
259 struct obstack *obstack;
263 obstack_grow (obstack, pointer, length);
266 void (obstack_grow0) (obstack, pointer, length)
267 struct obstack *obstack;
271 obstack_grow0 (obstack, pointer, length);
274 void (obstack_1grow) (obstack, character)
275 struct obstack *obstack;
278 obstack_1grow (obstack, character);
281 void (obstack_blank) (obstack, length)
282 struct obstack *obstack;
285 obstack_blank (obstack, length);
288 void (obstack_1grow_fast) (obstack, character)
289 struct obstack *obstack;
292 obstack_1grow_fast (obstack, character);
295 void (obstack_blank_fast) (obstack, length)
296 struct obstack *obstack;
299 obstack_blank_fast (obstack, length);
302 POINTER (obstack_finish) (obstack)
303 struct obstack *obstack;
305 return obstack_finish (obstack);
308 POINTER (obstack_alloc) (obstack, length)
309 struct obstack *obstack;
312 return obstack_alloc (obstack, length);
315 POINTER (obstack_copy) (obstack, pointer, length)
316 struct obstack *obstack;
320 return obstack_copy (obstack, pointer, length);
323 POINTER (obstack_copy0) (obstack, pointer, length)
324 struct obstack *obstack;
328 return obstack_copy0 (obstack, pointer, length);
331 #endif /* __STDC__ */