~ubuntu-branches/ubuntu/intrepid/mit-scheme/intrepid-updates

« back to all changes in this revision

Viewing changes to src/microcode/obstack.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Hanson
  • Date: 2002-03-14 17:04:07 UTC
  • Revision ID: james.westby@ubuntu.com-20020314170407-m5lg1d6bdsl9lv0s
Tags: upstream-7.7.0
ImportĀ upstreamĀ versionĀ 7.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 1, 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 HAVE_STDC
 
21
#define POINTER void *
 
22
#else
 
23
#define POINTER char *
 
24
#endif
 
25
 
 
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))
 
35
 
 
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.  */
 
40
#ifndef COPYING_UNIT
 
41
#define COPYING_UNIT int
 
42
#endif
 
43
 
 
44
/* The non-GNU-C macros copy the obstack into this global variable
 
45
   to avoid multiple evaluation.  */
 
46
 
 
47
struct obstack *_obstack;
 
48
 
 
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.  */
 
53
 
 
54
void
 
55
_obstack_begin (h, size, alignment, chunkfun, freefun)
 
56
     struct obstack *h;
 
57
     int size;
 
58
     long alignment;
 
59
     POINTER EXFUN ((*chunkfun), (long));
 
60
     void EXFUN ((*freefun), (PTR));
 
61
{
 
62
  register struct _obstack_chunk* chunk; /* points to new chunk */
 
63
 
 
64
  if (alignment == 0)
 
65
    alignment = DEFAULT_ALIGNMENT;
 
66
  if (size == 0)
 
67
    /* Default size is what GNU malloc can fit in a 4096-byte block.  */
 
68
    {
 
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
 
73
         allocated.
 
74
 
 
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));
 
80
      size = 4096 - extra;
 
81
    }
 
82
 
 
83
  h->chunkfun = (struct _obstack_chunk * EXFUN((*),(long))) chunkfun;
 
84
  h->freefun = freefun;
 
85
  h->chunk_size = size;
 
86
  h->alignment_mask = alignment - 1;
 
87
 
 
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;
 
92
  chunk->prev = 0;
 
93
}
 
94
 
 
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.  */
 
100
 
 
101
void
 
102
_obstack_newchunk (h, length)
 
103
     struct obstack *h;
 
104
     int length;
 
105
{
 
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;
 
110
  register int i;
 
111
  int already;
 
112
 
 
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;
 
117
 
 
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;
 
122
 
 
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)
 
127
    {
 
128
      for (i = obj_size / sizeof (COPYING_UNIT) - 1;
 
129
           i >= 0; i--)
 
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);
 
136
    }
 
137
  else
 
138
    already = 0;
 
139
  /* Copy remaining bytes one by one.  */
 
140
  for (i = already; i < obj_size; i++)
 
141
    new_chunk->contents[i] = h->object_base[i];
 
142
 
 
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)
 
146
    {
 
147
      new_chunk->prev = old_chunk->prev;
 
148
      (*h->freefun) (old_chunk);
 
149
    }
 
150
 
 
151
  h->object_base = new_chunk->contents;
 
152
  h->next_free = h->object_base + obj_size;
 
153
}
 
154
 
 
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.  */
 
158
 
 
159
extern int EXFUN (_obstack_allocated_p, (struct obstack *, POINTER));
 
160
 
 
161
int
 
162
_obstack_allocated_p (h, obj)
 
163
     struct obstack *h;
 
164
     POINTER obj;
 
165
{
 
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 */
 
168
 
 
169
  lp = (h)->chunk;
 
170
  while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
 
171
    {
 
172
      plp = lp -> prev;
 
173
      lp = plp;
 
174
    }
 
175
  return lp != 0;
 
176
}
 
177
 
 
178
/* Free objects in obstack H, including OBJ and everything allocate
 
179
   more recently than OBJ.  If OBJ is zero, free everything in H.  */
 
180
 
 
181
void
 
182
#ifdef HAVE_STDC
 
183
#undef obstack_free
 
184
obstack_free (struct obstack *h, POINTER obj)
 
185
#else
 
186
_obstack_free (h, obj)
 
187
     struct obstack *h;
 
188
     POINTER obj;
 
189
#endif
 
190
{
 
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 */
 
193
 
 
194
  lp = (h)->chunk;
 
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))
 
199
    {
 
200
      plp = lp -> prev;
 
201
      (*h->freefun) (lp);
 
202
      lp = plp;
 
203
    }
 
204
  if (lp)
 
205
    {
 
206
      (h)->object_base = (h)->next_free = (char *)(obj);
 
207
      (h)->chunk_limit = lp->limit;
 
208
      (h)->chunk = lp;
 
209
    }
 
210
  else if (obj != 0)
 
211
    /* obj is not in any of the chunks! */
 
212
    abort ();
 
213
}
 
214
 
 
215
/* Let same .o link with output of gcc and other compilers.  */
 
216
 
 
217
#ifdef HAVE_STDC
 
218
void
 
219
_obstack_free (h, obj)
 
220
     struct obstack *h;
 
221
     POINTER obj;
 
222
{
 
223
  obstack_free (h, obj);
 
224
}
 
225
#endif
 
226
 
 
227
#if 0
 
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.  */
 
230
 
 
231
/* Now define the functional versions of the obstack macros.
 
232
   Define them to simply use the corresponding macros to do the job.  */
 
233
 
 
234
#ifdef HAVE_STDC
 
235
/* These function definitions do not work with non-ANSI preprocessors;
 
236
   they won't pass through the macro names in parentheses.  */
 
237
 
 
238
/* The function names appear in parentheses in order to prevent
 
239
   the macro-definitions of the names from being expanded there.  */
 
240
 
 
241
POINTER (obstack_base) (obstack)
 
242
     struct obstack *obstack;
 
243
{
 
244
  return obstack_base (obstack);
 
245
}
 
246
 
 
247
POINTER (obstack_next_free) (obstack)
 
248
     struct obstack *obstack;
 
249
{
 
250
  return obstack_next_free (obstack);
 
251
}
 
252
 
 
253
int (obstack_object_size) (obstack)
 
254
     struct obstack *obstack;
 
255
{
 
256
  return obstack_object_size (obstack);
 
257
}
 
258
 
 
259
int (obstack_room) (obstack)
 
260
     struct obstack *obstack;
 
261
{
 
262
  return obstack_room (obstack);
 
263
}
 
264
 
 
265
void (obstack_grow) (obstack, pointer, length)
 
266
     struct obstack *obstack;
 
267
     POINTER pointer;
 
268
     int length;
 
269
{
 
270
  obstack_grow (obstack, pointer, length);
 
271
}
 
272
 
 
273
void (obstack_grow0) (obstack, pointer, length)
 
274
     struct obstack *obstack;
 
275
     POINTER pointer;
 
276
     int length;
 
277
{
 
278
  obstack_grow0 (obstack, pointer, length);
 
279
}
 
280
 
 
281
void (obstack_1grow) (obstack, character)
 
282
     struct obstack *obstack;
 
283
     int character;
 
284
{
 
285
  obstack_1grow (obstack, character);
 
286
}
 
287
 
 
288
void (obstack_blank) (obstack, length)
 
289
     struct obstack *obstack;
 
290
     int length;
 
291
{
 
292
  obstack_blank (obstack, length);
 
293
}
 
294
 
 
295
void (obstack_1grow_fast) (obstack, character)
 
296
     struct obstack *obstack;
 
297
     int character;
 
298
{
 
299
  obstack_1grow_fast (obstack, character);
 
300
}
 
301
 
 
302
void (obstack_blank_fast) (obstack, length)
 
303
     struct obstack *obstack;
 
304
     int length;
 
305
{
 
306
  obstack_blank_fast (obstack, length);
 
307
}
 
308
 
 
309
POINTER (obstack_finish) (obstack)
 
310
     struct obstack *obstack;
 
311
{
 
312
  return obstack_finish (obstack);
 
313
}
 
314
 
 
315
POINTER (obstack_alloc) (obstack, length)
 
316
     struct obstack *obstack;
 
317
     int length;
 
318
{
 
319
  return obstack_alloc (obstack, length);
 
320
}
 
321
 
 
322
POINTER (obstack_copy) (obstack, pointer, length)
 
323
     struct obstack *obstack;
 
324
     POINTER pointer;
 
325
     int length;
 
326
{
 
327
  return obstack_copy (obstack, pointer, length);
 
328
}
 
329
 
 
330
POINTER (obstack_copy0) (obstack, pointer, length)
 
331
     struct obstack *obstack;
 
332
     POINTER pointer;
 
333
     int length;
 
334
{
 
335
  return obstack_copy0 (obstack, pointer, length);
 
336
}
 
337
 
 
338
#endif /* HAVE_STDC */
 
339
 
 
340
#endif /* 0 */