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