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 \
29
((long) ((char *)&((struct fooalign *) 0)->d - (char *)0))
30
/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
31
But in fact it might be less smart and round addresses to as much as
32
DEFAULT_ROUNDING. So we prepare for it to do that. */
33
union fooround {long x; double d;};
34
#define DEFAULT_ROUNDING (sizeof (union fooround))
36
/* When we copy a long block of data, this is the unit to do it with.
37
On some machines, copying successive ints does not work;
38
in such a case, redefine COPYING_UNIT to `long' (if that works)
39
or `char' as a last resort. */
41
#define COPYING_UNIT int
44
/* The non-GNU-C macros copy the obstack into this global variable
45
to avoid multiple evaluation. */
47
struct obstack *_obstack;
49
/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
50
Objects start on multiples of ALIGNMENT (0 means use default).
51
CHUNKFUN is the function to use to allocate chunks,
52
and FREEFUN the function to free them. */
55
_obstack_begin (h, size, alignment, chunkfun, freefun)
59
POINTER EXFUN ((*chunkfun), (long));
60
void EXFUN ((*freefun), (PTR));
62
register struct _obstack_chunk* chunk; /* points to new chunk */
65
alignment = DEFAULT_ALIGNMENT;
67
/* Default size is what GNU malloc can fit in a 4096-byte block. */
69
/* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
70
Use the values for range checking, because if range checking is off,
71
the extra bytes won't be missed terribly, but if range checking is on
72
and we used a larger request, a whole extra 4096 bytes would be
75
These number are irrelevant to the new GNU malloc. I suspect it is
76
less sensitive to the size of the request. */
77
int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
78
+ 4 + DEFAULT_ROUNDING - 1)
79
& ~(DEFAULT_ROUNDING - 1));
83
h->chunkfun = (struct _obstack_chunk * EXFUN((*),(long))) chunkfun;
86
h->alignment_mask = alignment - 1;
88
chunk = h->chunk = (*h->chunkfun) (h->chunk_size);
89
h->next_free = h->object_base = chunk->contents;
90
h->chunk_limit = chunk->limit
91
= (char *) chunk + h->chunk_size;
95
/* Allocate a new current chunk for the obstack *H
96
on the assumption that LENGTH bytes need to be added
97
to the current object, or a new object of length LENGTH allocated.
98
Copies any partial object from the end of the old chunk
99
to the beginning of the new one. */
102
_obstack_newchunk (h, length)
106
register struct _obstack_chunk* old_chunk = h->chunk;
107
register struct _obstack_chunk* new_chunk;
108
register long new_size;
109
register int obj_size = h->next_free - h->object_base;
113
/* Compute size for new chunk. */
114
new_size = (obj_size + length) + (obj_size >> 3) + 100;
115
if (new_size < h->chunk_size)
116
new_size = h->chunk_size;
118
/* Allocate and initialize the new chunk. */
119
new_chunk = h->chunk = (*h->chunkfun) (new_size);
120
new_chunk->prev = old_chunk;
121
new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
123
/* Move the existing object to the new chunk.
124
Word at a time is fast and is safe if the object
125
is sufficiently aligned. */
126
if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
128
for (i = obj_size / sizeof (COPYING_UNIT) - 1;
130
((COPYING_UNIT *)new_chunk->contents)[i]
131
= ((COPYING_UNIT *)h->object_base)[i];
132
/* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
133
but that can cross a page boundary on a machine
134
which does not do strict alignment for COPYING_UNITS. */
135
already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
139
/* Copy remaining bytes one by one. */
140
for (i = already; i < obj_size; i++)
141
new_chunk->contents[i] = h->object_base[i];
143
/* If the object just copied was the only data in OLD_CHUNK,
144
free that chunk and remove it from the chain. */
145
if (h->object_base == old_chunk->contents)
147
new_chunk->prev = old_chunk->prev;
148
(*h->freefun) (old_chunk);
151
h->object_base = new_chunk->contents;
152
h->next_free = h->object_base + obj_size;
155
/* Return nonzero if object OBJ has been allocated from obstack H.
156
This is here for debugging.
157
If you use it in a program, you are probably losing. */
159
extern int EXFUN (_obstack_allocated_p, (struct obstack *, POINTER));
162
_obstack_allocated_p (h, obj)
166
register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
167
register struct _obstack_chunk* plp; /* point to previous chunk if any */
170
while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
178
/* Free objects in obstack H, including OBJ and everything allocate
179
more recently than OBJ. If OBJ is zero, free everything in H. */
184
obstack_free (struct obstack *h, POINTER obj)
186
_obstack_free (h, obj)
191
register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
192
register struct _obstack_chunk* plp; /* point to previous chunk if any */
195
/* We use >= because there cannot be an object at the beginning of a chunk.
196
But there can be an empty object at that address
197
at the end of another chunk. */
198
while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
206
(h)->object_base = (h)->next_free = (char *)(obj);
207
(h)->chunk_limit = lp->limit;
211
/* obj is not in any of the chunks! */
215
/* Let same .o link with output of gcc and other compilers. */
219
_obstack_free (h, obj)
223
obstack_free (h, obj);
228
/* These are now turned off because the applications do not use it
229
and it uses bcopy via obstack_grow, which causes trouble on sysV. */
231
/* Now define the functional versions of the obstack macros.
232
Define them to simply use the corresponding macros to do the job. */
235
/* These function definitions do not work with non-ANSI preprocessors;
236
they won't pass through the macro names in parentheses. */
238
/* The function names appear in parentheses in order to prevent
239
the macro-definitions of the names from being expanded there. */
241
POINTER (obstack_base) (obstack)
242
struct obstack *obstack;
244
return obstack_base (obstack);
247
POINTER (obstack_next_free) (obstack)
248
struct obstack *obstack;
250
return obstack_next_free (obstack);
253
int (obstack_object_size) (obstack)
254
struct obstack *obstack;
256
return obstack_object_size (obstack);
259
int (obstack_room) (obstack)
260
struct obstack *obstack;
262
return obstack_room (obstack);
265
void (obstack_grow) (obstack, pointer, length)
266
struct obstack *obstack;
270
obstack_grow (obstack, pointer, length);
273
void (obstack_grow0) (obstack, pointer, length)
274
struct obstack *obstack;
278
obstack_grow0 (obstack, pointer, length);
281
void (obstack_1grow) (obstack, character)
282
struct obstack *obstack;
285
obstack_1grow (obstack, character);
288
void (obstack_blank) (obstack, length)
289
struct obstack *obstack;
292
obstack_blank (obstack, length);
295
void (obstack_1grow_fast) (obstack, character)
296
struct obstack *obstack;
299
obstack_1grow_fast (obstack, character);
302
void (obstack_blank_fast) (obstack, length)
303
struct obstack *obstack;
306
obstack_blank_fast (obstack, length);
309
POINTER (obstack_finish) (obstack)
310
struct obstack *obstack;
312
return obstack_finish (obstack);
315
POINTER (obstack_alloc) (obstack, length)
316
struct obstack *obstack;
319
return obstack_alloc (obstack, length);
322
POINTER (obstack_copy) (obstack, pointer, length)
323
struct obstack *obstack;
327
return obstack_copy (obstack, pointer, length);
330
POINTER (obstack_copy0) (obstack, pointer, length)
331
struct obstack *obstack;
335
return obstack_copy0 (obstack, pointer, length);
338
#endif /* HAVE_STDC */