]> Git Repo - binutils.git/blob - gas/obstack.c
no longer using gas-format.el
[binutils.git] / gas / obstack.c
1 /* obstack.c - subroutines used implicitly by object stack macros
2    Copyright (C) 1988 Free Software Foundation, Inc.
3
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
7 later version.
8
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.
13
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.  */
17
18 #include "obstack.h"
19
20 #ifdef __STDC__
21 #define POINTER void *
22 #else
23 #define POINTER char *
24 #endif
25
26 /* Determine default alignment.  */
27 struct fooalign
28   {
29     char x;
30     double d;
31   };
32
33 #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
34 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
35    But in fact it might be less smart and round addresses to as much as
36    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
37 union fooround
38   {
39     long x;
40     double d;
41   };
42
43 #define DEFAULT_ROUNDING (sizeof (union fooround))
44
45 /* When we copy a long block of data, this is the unit to do it with.
46    On some machines, copying successive ints does not work;
47    in such a case, redefine COPYING_UNIT to `long' (if that works)
48    or `char' as a last resort.  */
49 #ifndef COPYING_UNIT
50 #define COPYING_UNIT int
51 #endif
52
53 /* The non-GNU-C macros copy the obstack into this global variable
54    to avoid multiple evaluation.  */
55
56 struct obstack *_obstack;
57 \f
58 /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
59    Objects start on multiples of ALIGNMENT (0 means use default).
60    CHUNKFUN is the function to use to allocate chunks,
61    and FREEFUN the function to free them.  */
62
63 void
64 _obstack_begin (h, size, alignment, chunkfun, freefun)
65      struct obstack *h;
66      int size;
67      int alignment;
68 POINTER (*chunkfun) ();
69      void (*freefun) ();
70 {
71   register struct _obstack_chunk *chunk;        /* points to new chunk */
72
73   if (alignment == 0)
74     alignment = DEFAULT_ALIGNMENT;
75   if (size == 0)
76     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
77     {
78       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
79          Use the values for range checking, because if range checking is off,
80          the extra bytes won't be missed terribly, but if range checking is on
81          and we used a larger request, a whole extra 4096 bytes would be
82          allocated.
83
84          These number are irrelevant to the new GNU malloc.  I suspect it is
85          less sensitive to the size of the request.  */
86       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
87                     + 4 + DEFAULT_ROUNDING - 1)
88                    & ~(DEFAULT_ROUNDING - 1));
89       size = 4096 - extra;
90     }
91
92   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
93   h->freefun = freefun;
94   h->chunk_size = size;
95   h->alignment_mask = alignment - 1;
96
97   chunk = h->chunk = (*h->chunkfun) (h->chunk_size);
98   h->next_free = h->object_base = chunk->contents;
99   h->chunk_limit = chunk->limit
100     = (char *) chunk + h->chunk_size;
101   chunk->prev = 0;
102   /* The initial chunk now contains no empty object.  */
103   h->maybe_empty_object = 0;
104 }
105
106 /* Allocate a new current chunk for the obstack *H
107    on the assumption that LENGTH bytes need to be added
108    to the current object, or a new object of length LENGTH allocated.
109    Copies any partial object from the end of the old chunk
110    to the beginning of the new one.  */
111
112 void
113 _obstack_newchunk (h, length)
114      struct obstack *h;
115      int length;
116 {
117   register struct _obstack_chunk *old_chunk = h->chunk;
118   register struct _obstack_chunk *new_chunk;
119   register long new_size;
120   register int obj_size = h->next_free - h->object_base;
121   register int i;
122   int already;
123
124   /* Compute size for new chunk.  */
125   new_size = (obj_size + length) + (obj_size >> 3) + 100;
126   if (new_size < h->chunk_size)
127     new_size = h->chunk_size;
128
129   /* Allocate and initialize the new chunk.  */
130   new_chunk = h->chunk = (*h->chunkfun) (new_size);
131   new_chunk->prev = old_chunk;
132   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
133
134   /* Move the existing object to the new chunk.
135      Word at a time is fast and is safe if the object
136      is sufficiently aligned.  */
137   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
138     {
139       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
140            i >= 0; i--)
141         ((COPYING_UNIT *) new_chunk->contents)[i]
142           = ((COPYING_UNIT *) h->object_base)[i];
143       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
144          but that can cross a page boundary on a machine
145          which does not do strict alignment for COPYING_UNITS.  */
146       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
147     }
148   else
149     already = 0;
150   /* Copy remaining bytes one by one.  */
151   for (i = already; i < obj_size; i++)
152     new_chunk->contents[i] = h->object_base[i];
153
154   /* If the object just copied was the only data in OLD_CHUNK,
155      free that chunk and remove it from the chain.
156      But not if that chunk might contain an empty object.  */
157   if (h->object_base == old_chunk->contents && !h->maybe_empty_object)
158     {
159       new_chunk->prev = old_chunk->prev;
160       (*h->freefun) (old_chunk);
161     }
162
163   h->object_base = new_chunk->contents;
164   h->next_free = h->object_base + obj_size;
165   /* The new chunk certainly contains no empty object yet.  */
166   h->maybe_empty_object = 0;
167 }
168
169 /* Return nonzero if object OBJ has been allocated from obstack H.
170    This is here for debugging.
171    If you use it in a program, you are probably losing.  */
172
173 int
174 _obstack_allocated_p (h, obj)
175      struct obstack *h;
176      POINTER obj;
177 {
178   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
179   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
180
181   lp = (h)->chunk;
182   /* We use >= rather than > since the object cannot be exactly at
183      the beginning of the chunk but might be an empty object exactly
184      at the end of an adjacent chunk. */
185   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
186     {
187       plp = lp->prev;
188       lp = plp;
189     }
190   return lp != 0;
191 }
192 \f
193 /* Free objects in obstack H, including OBJ and everything allocate
194    more recently than OBJ.  If OBJ is zero, free everything in H.  */
195
196 #undef obstack_free
197
198 /* This function has two names with identical definitions.
199    This is the first one, called from non-ANSI code.  */
200
201 void
202 _obstack_free (h, obj)
203      struct obstack *h;
204      POINTER obj;
205 {
206   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
207   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
208
209   lp = h->chunk;
210   /* We use >= because there cannot be an object at the beginning of a chunk.
211      But there can be an empty object at that address
212      at the end of another chunk.  */
213   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
214     {
215       plp = lp->prev;
216       (*h->freefun) (lp);
217       lp = plp;
218       /* If we switch chunks, we can't tell whether the new current
219          chunk contains an empty object, so assume that it may.  */
220       h->maybe_empty_object = 1;
221     }
222   if (lp)
223     {
224       h->object_base = h->next_free = (char *) (obj);
225       h->chunk_limit = lp->limit;
226       h->chunk = lp;
227     }
228   else if (obj != 0)
229     /* obj is not in any of the chunks! */
230     abort ();
231 }
232
233 /* This function is used from ANSI code.  */
234
235 void
236 obstack_free (h, obj)
237      struct obstack *h;
238      POINTER obj;
239 {
240   register struct _obstack_chunk *lp;   /* below addr of any objects in this chunk */
241   register struct _obstack_chunk *plp;  /* point to previous chunk if any */
242
243   lp = h->chunk;
244   /* We use >= because there cannot be an object at the beginning of a chunk.
245      But there can be an empty object at that address
246      at the end of another chunk.  */
247   while (lp != 0 && ((POINTER) lp >= obj || (POINTER) (lp)->limit < obj))
248     {
249       plp = lp->prev;
250       (*h->freefun) (lp);
251       lp = plp;
252       /* If we switch chunks, we can't tell whether the new current
253          chunk contains an empty object, so assume that it may.  */
254       h->maybe_empty_object = 1;
255     }
256   if (lp)
257     {
258       h->object_base = h->next_free = (char *) (obj);
259       h->chunk_limit = lp->limit;
260       h->chunk = lp;
261     }
262   else if (obj != 0)
263     /* obj is not in any of the chunks! */
264     abort ();
265 }
266 \f
267 #if 0
268 /* These are now turned off because the applications do not use it
269    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
270
271 /* Now define the functional versions of the obstack macros.
272    Define them to simply use the corresponding macros to do the job.  */
273
274 #ifdef __STDC__
275 /* These function definitions do not work with non-ANSI preprocessors;
276    they won't pass through the macro names in parentheses.  */
277
278 /* The function names appear in parentheses in order to prevent
279    the macro-definitions of the names from being expanded there.  */
280
281 POINTER (obstack_base) (obstack)
282      struct obstack *obstack;
283 {
284   return obstack_base (obstack);
285 }
286
287 POINTER (obstack_next_free) (obstack)
288      struct obstack *obstack;
289 {
290   return obstack_next_free (obstack);
291 }
292
293 int (obstack_object_size) (obstack)
294      struct obstack *obstack;
295 {
296   return obstack_object_size (obstack);
297 }
298
299 int (obstack_room) (obstack)
300      struct obstack *obstack;
301 {
302   return obstack_room (obstack);
303 }
304
305 void (obstack_grow) (obstack, pointer, length)
306      struct obstack *obstack;
307      POINTER pointer;
308      int length;
309 {
310   obstack_grow (obstack, pointer, length);
311 }
312
313 void (obstack_grow0) (obstack, pointer, length)
314      struct obstack *obstack;
315      POINTER pointer;
316      int length;
317 {
318   obstack_grow0 (obstack, pointer, length);
319 }
320
321 void (obstack_1grow) (obstack, character)
322      struct obstack *obstack;
323      int character;
324 {
325   obstack_1grow (obstack, character);
326 }
327
328 void (obstack_blank) (obstack, length)
329      struct obstack *obstack;
330      int length;
331 {
332   obstack_blank (obstack, length);
333 }
334
335 void (obstack_1grow_fast) (obstack, character)
336      struct obstack *obstack;
337      int character;
338 {
339   obstack_1grow_fast (obstack, character);
340 }
341
342 void (obstack_blank_fast) (obstack, length)
343      struct obstack *obstack;
344      int length;
345 {
346   obstack_blank_fast (obstack, length);
347 }
348
349 POINTER (obstack_finish) (obstack)
350      struct obstack *obstack;
351 {
352   return obstack_finish (obstack);
353 }
354
355 POINTER (obstack_alloc) (obstack, length)
356      struct obstack *obstack;
357      int length;
358 {
359   return obstack_alloc (obstack, length);
360 }
361
362 POINTER (obstack_copy) (obstack, pointer, length)
363      struct obstack *obstack;
364      POINTER pointer;
365      int length;
366 {
367   return obstack_copy (obstack, pointer, length);
368 }
369
370 POINTER (obstack_copy0) (obstack, pointer, length)
371      struct obstack *obstack;
372      POINTER pointer;
373      int length;
374 {
375   return obstack_copy0 (obstack, pointer, length);
376 }
377
378 #endif /* __STDC__ */
379
380 #endif /* 0 */
This page took 0.07162 seconds and 4 git commands to generate.