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.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
27 #define POINTER void *
29 #define POINTER char *
32 /* Determine default alignment. */
33 struct fooalign
{char x
; double d
;};
34 #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
35 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
36 But in fact it might be less smart and round addresses to as much as
37 DEFAULT_ROUNDING. So we prepare for it to do that. */
38 union fooround
{long x
; double d
;};
39 #define DEFAULT_ROUNDING (sizeof (union fooround))
41 /* When we copy a long block of data, this is the unit to do it with.
42 On some machines, copying successive ints does not work;
43 in such a case, redefine COPYING_UNIT to `long' (if that works)
44 or `char' as a last resort. */
46 #define COPYING_UNIT int
49 /* The non-GNU-C macros copy the obstack into this global variable
50 to avoid multiple evaluation. */
52 struct obstack
*_obstack
;
54 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
55 Objects start on multiples of ALIGNMENT (0 means use default).
56 CHUNKFUN is the function to use to allocate chunks,
57 and FREEFUN the function to free them. */
60 _obstack_begin (h
, size
, alignment
, chunkfun
, freefun
)
64 POINTER (*chunkfun
) ();
67 register struct _obstack_chunk
* chunk
; /* points to new chunk */
70 alignment
= DEFAULT_ALIGNMENT
;
72 /* Default size is what GNU malloc can fit in a 4096-byte block.
73 Pick a number small enough that when rounded up to DEFAULT_ROUNDING
74 it is still smaller than 4096 - 4. */
77 if (extra
< DEFAULT_ROUNDING
)
78 extra
= DEFAULT_ROUNDING
;
82 h
->chunkfun
= (struct _obstack_chunk
* (*)()) chunkfun
;
85 h
->alignment_mask
= alignment
- 1;
87 chunk
= h
->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. */
101 _obstack_newchunk (h
, length
)
105 register struct _obstack_chunk
* old_chunk
= h
->chunk
;
106 register struct _obstack_chunk
* new_chunk
;
107 register long new_size
;
108 register int obj_size
= h
->next_free
- h
->object_base
;
112 /* Compute size for new chunk. */
113 new_size
= (obj_size
+ length
) + (obj_size
>> 3) + 100;
114 if (new_size
< h
->chunk_size
)
115 new_size
= h
->chunk_size
;
117 /* Allocate and initialize the new chunk. */
118 new_chunk
= h
->chunk
= (*h
->chunkfun
) (new_size
);
119 new_chunk
->prev
= old_chunk
;
120 new_chunk
->limit
= h
->chunk_limit
= (char *) new_chunk
+ new_size
;
122 /* Move the existing object to the new chunk.
123 Word at a time is fast and is safe if the object
124 is sufficiently aligned. */
125 if (h
->alignment_mask
+ 1 >= DEFAULT_ALIGNMENT
)
127 for (i
= obj_size
/ sizeof (COPYING_UNIT
) - 1;
129 ((COPYING_UNIT
*)new_chunk
->contents
)[i
]
130 = ((COPYING_UNIT
*)h
->object_base
)[i
];
131 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
132 but that can cross a page boundary on a machine
133 which does not do strict alignment for COPYING_UNITS. */
134 already
= obj_size
/ sizeof (COPYING_UNIT
) * sizeof (COPYING_UNIT
);
138 /* Copy remaining bytes one by one. */
139 for (i
= already
; i
< obj_size
; i
++)
140 new_chunk
->contents
[i
] = h
->object_base
[i
];
142 h
->object_base
= new_chunk
->contents
;
143 h
->next_free
= h
->object_base
+ obj_size
;
146 /* Return nonzero if object OBJ has been allocated from obstack H.
147 This is here for debugging.
148 If you use it in a program, you are probably losing. */
151 _obstack_allocated_p (h
, obj
)
155 register struct _obstack_chunk
* lp
; /* below addr of any objects in this chunk */
156 register struct _obstack_chunk
* plp
; /* point to previous chunk if any */
159 while (lp
!= 0 && ((POINTER
)lp
> obj
|| (POINTER
)(lp
)->limit
< obj
))
167 /* Free objects in obstack H, including OBJ and everything allocate
168 more recently than OBJ. If OBJ is zero, free everything in H. */
173 obstack_free (struct obstack
*h
, POINTER obj
)
175 _obstack_free (h
, obj
)
180 register struct _obstack_chunk
* lp
; /* below addr of any objects in this chunk */
181 register struct _obstack_chunk
* plp
; /* point to previous chunk if any */
184 while (lp
!= 0 && ((POINTER
)lp
> obj
|| (POINTER
)(lp
)->limit
< obj
))
192 (h
)->object_base
= (h
)->next_free
= (char *)(obj
);
193 (h
)->chunk_limit
= lp
->limit
;
197 /* obj is not in any of the chunks! */
201 /* Let same .o link with output of gcc and other compilers. */
205 _obstack_free (h
, obj
)
209 obstack_free (h
, obj
);
214 /* These are now turned off because the applications do not use it
215 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
217 /* Now define the functional versions of the obstack macros.
218 Define them to simply use the corresponding macros to do the job. */
221 /* These function definitions do not work with non-ANSI preprocessors;
222 they won't pass through the macro names in parentheses. */
224 /* The function names appear in parentheses in order to prevent
225 the macro-definitions of the names from being expanded there. */
227 POINTER (obstack_base
) (obstack
)
228 struct obstack
*obstack
;
230 return obstack_base (obstack
);
233 POINTER (obstack_next_free
) (obstack
)
234 struct obstack
*obstack
;
236 return obstack_next_free (obstack
);
239 int (obstack_object_size
) (obstack
)
240 struct obstack
*obstack
;
242 return obstack_object_size (obstack
);
245 int (obstack_room
) (obstack
)
246 struct obstack
*obstack
;
248 return obstack_room (obstack
);
251 void (obstack_grow
) (obstack
, pointer
, length
)
252 struct obstack
*obstack
;
256 obstack_grow (obstack
, pointer
, length
);
259 void (obstack_grow0
) (obstack
, pointer
, length
)
260 struct obstack
*obstack
;
264 obstack_grow0 (obstack
, pointer
, length
);
267 void (obstack_1grow
) (obstack
, character
)
268 struct obstack
*obstack
;
271 obstack_1grow (obstack
, character
);
274 void (obstack_blank
) (obstack
, length
)
275 struct obstack
*obstack
;
278 obstack_blank (obstack
, length
);
281 void (obstack_1grow_fast
) (obstack
, character
)
282 struct obstack
*obstack
;
285 obstack_1grow_fast (obstack
, character
);
288 void (obstack_blank_fast
) (obstack
, length
)
289 struct obstack
*obstack
;
292 obstack_blank_fast (obstack
, length
);
295 POINTER (obstack_finish
) (obstack
)
296 struct obstack
*obstack
;
298 return obstack_finish (obstack
);
301 POINTER (obstack_alloc
) (obstack
, length
)
302 struct obstack
*obstack
;
305 return obstack_alloc (obstack
, length
);
308 POINTER (obstack_copy
) (obstack
, pointer
, length
)
309 struct obstack
*obstack
;
313 return obstack_copy (obstack
, pointer
, length
);
316 POINTER (obstack_copy0
) (obstack
, pointer
, length
)
317 struct obstack
*obstack
;
321 return obstack_copy0 (obstack
, pointer
, length
);
324 #endif /* __STDC__ */