gdb-3.5
[deliverable/binutils-gdb.git] / gdb / obstack.c
CommitLineData
632ea0cc 1/* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988 Free Software Foundation, Inc.
3
4187119d 4This program is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6Free Software Foundation; either version 1, or (at your option) any
7later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
632ea0cc 17
18
19In other words, you are welcome to use, share and improve this program.
20You are forbidden to forbid anyone else to use, share and improve
21what you give them. Help stamp out software-hoarding! */
22
23
24#include "obstack.h"
25
26#ifdef __STDC__
27#define POINTER void *
28#else
29#define POINTER char *
30#endif
31
3bf57d21 32/* Determine default alignment. */
33struct 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. */
38union fooround {long x; double d;};
39#define DEFAULT_ROUNDING (sizeof (union fooround))
40
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. */
45#ifndef COPYING_UNIT
46#define COPYING_UNIT int
47#endif
48
632ea0cc 49/* The non-GNU-C macros copy the obstack into this global variable
50 to avoid multiple evaluation. */
51
52struct obstack *_obstack;
53\f
3bf57d21 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).
632ea0cc 56 CHUNKFUN is the function to use to allocate chunks,
57 and FREEFUN the function to free them. */
58
59void
60_obstack_begin (h, size, alignment, chunkfun, freefun)
61 struct obstack *h;
62 int size;
63 int alignment;
64 POINTER (*chunkfun) ();
65 void (*freefun) ();
66{
3bf57d21 67 register struct _obstack_chunk* chunk; /* points to new chunk */
68
69 if (alignment == 0)
70 alignment = DEFAULT_ALIGNMENT;
71 if (size == 0)
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. */
75 {
76 int extra = 4;
77 if (extra < DEFAULT_ROUNDING)
78 extra = DEFAULT_ROUNDING;
79 size = 4096 - extra;
80 }
81
632ea0cc 82 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
83 h->freefun = freefun;
84 h->chunk_size = size;
85 h->alignment_mask = alignment - 1;
86
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;
91 chunk->prev = 0;
92}
93
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. */
99
100void
101_obstack_newchunk (h, length)
102 struct obstack *h;
103 int length;
104{
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;
109 register int i;
110
111 /* Compute size for new chunk. */
112 new_size = (obj_size + length) << 1;
113 if (new_size < h->chunk_size)
114 new_size = h->chunk_size;
115
116 /* Allocate and initialize the new chunk. */
117 new_chunk = h->chunk = (*h->chunkfun) (new_size);
118 new_chunk->prev = old_chunk;
119 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
120
121 /* Move the existing object to the new chunk.
122 Word at a time is fast and is safe because these
123 structures are aligned at least that much. */
3bf57d21 124 for (i = (obj_size + sizeof (COPYING_UNIT) - 1) / sizeof (COPYING_UNIT) - 1;
125 i >= 0; i--)
126 ((COPYING_UNIT *)new_chunk->contents)[i]
127 = ((COPYING_UNIT *)h->object_base)[i];
632ea0cc 128
129 h->object_base = new_chunk->contents;
130 h->next_free = h->object_base + obj_size;
131}
132
e91b87a3 133/* Return nonzero if object OBJ has been allocated from obstack H.
134 This is here for debugging.
135 If you use it in a program, you are probably losing. */
136
137int
138_obstack_allocated_p (h, obj)
139 struct obstack *h;
140 POINTER obj;
141{
142 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
143 register struct _obstack_chunk* plp; /* point to previous chunk if any */
144
145 lp = (h)->chunk;
146 while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
147 {
148 plp = lp -> prev;
149 lp = plp;
150 }
151 return lp != 0;
152}
153
632ea0cc 154/* Free objects in obstack H, including OBJ and everything allocate
155 more recently than OBJ. If OBJ is zero, free everything in H. */
156
157void
158#ifdef __STDC__
159#undef obstack_free
4187119d 160obstack_free (struct obstack *h, POINTER obj)
632ea0cc 161#else
162_obstack_free (h, obj)
632ea0cc 163 struct obstack *h;
164 POINTER obj;
4187119d 165#endif
632ea0cc 166{
167 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
168 register struct _obstack_chunk* plp; /* point to previous chunk if any */
169
170 lp = (h)->chunk;
171 while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
172 {
173 plp = lp -> prev;
174 (*h->freefun) (lp);
175 lp = plp;
176 }
177 if (lp)
178 {
179 (h)->object_base = (h)->next_free = (char *)(obj);
180 (h)->chunk_limit = lp->limit;
181 (h)->chunk = lp;
182 }
183 else if (obj != 0)
184 /* obj is not in any of the chunks! */
185 abort ();
186}
4187119d 187
188/* Let same .o link with output of gcc and other compilers. */
189
190#ifdef __STDC__
191void
192_obstack_free (h, obj)
193 struct obstack *h;
194 POINTER obj;
195{
196 obstack_free (h, obj);
197}
198#endif
632ea0cc 199\f
3bf57d21 200#if 0
201/* These are now turned off because the applications do not use it
202 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
203
632ea0cc 204/* Now define the functional versions of the obstack macros.
205 Define them to simply use the corresponding macros to do the job. */
206
207#ifdef __STDC__
208/* These function definitions do not work with non-ANSI preprocessors;
209 they won't pass through the macro names in parentheses. */
210
211/* The function names appear in parentheses in order to prevent
212 the macro-definitions of the names from being expanded there. */
213
214POINTER (obstack_base) (obstack)
215 struct obstack *obstack;
216{
217 return obstack_base (obstack);
218}
219
220POINTER (obstack_next_free) (obstack)
221 struct obstack *obstack;
222{
223 return obstack_next_free (obstack);
224}
225
226int (obstack_object_size) (obstack)
227 struct obstack *obstack;
228{
229 return obstack_object_size (obstack);
230}
231
232int (obstack_room) (obstack)
233 struct obstack *obstack;
234{
235 return obstack_room (obstack);
236}
237
238void (obstack_grow) (obstack, pointer, length)
239 struct obstack *obstack;
240 POINTER pointer;
241 int length;
242{
243 obstack_grow (obstack, pointer, length);
244}
245
246void (obstack_grow0) (obstack, pointer, length)
247 struct obstack *obstack;
248 POINTER pointer;
249 int length;
250{
251 obstack_grow0 (obstack, pointer, length);
252}
253
254void (obstack_1grow) (obstack, character)
255 struct obstack *obstack;
256 int character;
257{
258 obstack_1grow (obstack, character);
259}
260
261void (obstack_blank) (obstack, length)
262 struct obstack *obstack;
263 int length;
264{
265 obstack_blank (obstack, length);
266}
267
268void (obstack_1grow_fast) (obstack, character)
269 struct obstack *obstack;
270 int character;
271{
272 obstack_1grow_fast (obstack, character);
273}
274
275void (obstack_blank_fast) (obstack, length)
276 struct obstack *obstack;
277 int length;
278{
279 obstack_blank_fast (obstack, length);
280}
281
282POINTER (obstack_finish) (obstack)
283 struct obstack *obstack;
284{
285 return obstack_finish (obstack);
286}
287
288POINTER (obstack_alloc) (obstack, length)
289 struct obstack *obstack;
290 int length;
291{
292 return obstack_alloc (obstack, length);
293}
294
295POINTER (obstack_copy) (obstack, pointer, length)
296 struct obstack *obstack;
297 POINTER pointer;
298 int length;
299{
300 return obstack_copy (obstack, pointer, length);
301}
302
303POINTER (obstack_copy0) (obstack, pointer, length)
304 struct obstack *obstack;
305 POINTER pointer;
306 int length;
307{
308 return obstack_copy0 (obstack, pointer, length);
309}
310
311#endif /* __STDC__ */
3bf57d21 312
313#endif /* 0 */
This page took 0.035916 seconds and 4 git commands to generate.