Add obstack.[ch] from EGLIBC 2.10
Signed-off-by: Fredrik Kuivinen <frekui@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Fredrik Kuivinen committed
Aug 21, 2011 at 00:40 UTC
e831171d67469f3942c56433e3cd3c98fca15e72
3 files changed
+952
Makefile
+2
@@ -514,6 +514,7 @@ LIB_H += commit.h
514
LIB_H += compat/bswap.h
515
LIB_H += compat/cygwin.h
516
LIB_H += compat/mingw.h
517
+LIB_H += compat/obstack.h
518
LIB_H += compat/win32/pthread.h
519
LIB_H += compat/win32/syslog.h
520
LIB_H += compat/win32/sys/poll.h
@@ -593,6 +594,7 @@ LIB_OBJS += cache-tree.o
594
LIB_OBJS += color.o
595
LIB_OBJS += combine-diff.o
596
LIB_OBJS += commit.o
597
+LIB_OBJS += compat/obstack.o
598
LIB_OBJS += config.o
599
LIB_OBJS += connect.o
600
LIB_OBJS += convert.o
compat/obstack.c
new
+441
@@ -0,0 +1,441 @@
1
+/* obstack.c - subroutines used implicitly by object stack macros
2
+ Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
3
+ 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
+ This file is part of the GNU C Library.
5
+
6
+ The GNU C Library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ The GNU C Library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with the GNU C Library; if not, write to the Free
18
+ Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ Boston, MA 02110-1301, USA. */
20
+
21
+
22
+#ifdef HAVE_CONFIG_H
23
+# include <config.h>
24
+#endif
25
+
26
+#ifdef _LIBC
27
+# include <obstack.h>
28
+# include <shlib-compat.h>
29
+#else
30
+# include "obstack.h"
31
+#endif
32
+
33
+/* NOTE BEFORE MODIFYING THIS FILE: This version number must be
34
+ incremented whenever callers compiled using an old obstack.h can no
35
+ longer properly call the functions in this obstack.c. */
36
+#define OBSTACK_INTERFACE_VERSION 1
37
+
38
+/* Comment out all this code if we are using the GNU C Library, and are not
39
+ actually compiling the library itself, and the installed library
40
+ supports the same library interface we do. This code is part of the GNU
41
+ C Library, but also included in many other GNU distributions. Compiling
42
+ and linking in this code is a waste when using the GNU C library
43
+ (especially if it is a shared library). Rather than having every GNU
44
+ program understand `configure --with-gnu-libc' and omit the object
45
+ files, it is simpler to just do this in the source for each such file. */
46
+
47
+#include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
48
+#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
49
+# include <gnu-versions.h>
50
+# if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
51
+# define ELIDE_CODE
52
+# endif
53
+#endif
54
+
55
+#include <stddef.h>
56
+
57
+#ifndef ELIDE_CODE
58
+
59
+
60
+# if HAVE_INTTYPES_H
61
+# include <inttypes.h>
62
+# endif
63
+# if HAVE_STDINT_H || defined _LIBC
64
+# include <stdint.h>
65
+# endif
66
+
67
+/* Determine default alignment. */
68
+union fooround
69
+{
70
+ uintmax_t i;
71
+ long double d;
72
+ void *p;
73
+};
74
+struct fooalign
75
+{
76
+ char c;
77
+ union fooround u;
78
+};
79
+/* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
80
+ But in fact it might be less smart and round addresses to as much as
81
+ DEFAULT_ROUNDING. So we prepare for it to do that. */
82
+enum
83
+ {
84
+ DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
85
+ DEFAULT_ROUNDING = sizeof (union fooround)
86
+ };
87
+
88
+/* When we copy a long block of data, this is the unit to do it with.
89
+ On some machines, copying successive ints does not work;
90
+ in such a case, redefine COPYING_UNIT to `long' (if that works)
91
+ or `char' as a last resort. */
92
+# ifndef COPYING_UNIT
93
+# define COPYING_UNIT int
94
+# endif
95
+
96
+
97
+/* The functions allocating more room by calling `obstack_chunk_alloc'
98
+ jump to the handler pointed to by `obstack_alloc_failed_handler'.
99
+ This can be set to a user defined function which should either
100
+ abort gracefully or use longjump - but shouldn't return. This
101
+ variable by default points to the internal function
102
+ `print_and_abort'. */
103
+static void print_and_abort (void);
104
+void (*obstack_alloc_failed_handler) (void) = print_and_abort;
105
+
106
+/* Exit value used when `print_and_abort' is used. */
107
+# include <stdlib.h>
108
+# ifdef _LIBC
109
+int obstack_exit_failure = EXIT_FAILURE;
110
+# else
111
+# include "exitfail.h"
112
+# define obstack_exit_failure exit_failure
113
+# endif
114
+
115
+# ifdef _LIBC
116
+# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
117
+/* A looong time ago (before 1994, anyway; we're not sure) this global variable
118
+ was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
119
+ library still exports it because somebody might use it. */
120
+struct obstack *_obstack_compat;
121
+compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
122
+# endif
123
+# endif
124
+
125
+/* Define a macro that either calls functions with the traditional malloc/free
126
+ calling interface, or calls functions with the mmalloc/mfree interface
127
+ (that adds an extra first argument), based on the state of use_extra_arg.
128
+ For free, do not use ?:, since some compilers, like the MIPS compilers,
129
+ do not allow (expr) ? void : void. */
130
+
131
+# define CALL_CHUNKFUN(h, size) \
132
+ (((h) -> use_extra_arg) \
133
+ ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
134
+ : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
135
+
136
+# define CALL_FREEFUN(h, old_chunk) \
137
+ do { \
138
+ if ((h) -> use_extra_arg) \
139
+ (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
140
+ else \
141
+ (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
142
+ } while (0)
143
+
144
+
145
+/* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
146
+ Objects start on multiples of ALIGNMENT (0 means use default).
147
+ CHUNKFUN is the function to use to allocate chunks,
148
+ and FREEFUN the function to free them.
149
+
150
+ Return nonzero if successful, calls obstack_alloc_failed_handler if
151
+ allocation fails. */
152
+
153
+int
154
+_obstack_begin (struct obstack *h,
155
+ int size, int alignment,
156
+ void *(*chunkfun) (long),
157
+ void (*freefun) (void *))
158
+{
159
+ register struct _obstack_chunk *chunk; /* points to new chunk */
160
+
161
+ if (alignment == 0)
162
+ alignment = DEFAULT_ALIGNMENT;
163
+ if (size == 0)
164
+ /* Default size is what GNU malloc can fit in a 4096-byte block. */
165
+ {
166
+ /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
167
+ Use the values for range checking, because if range checking is off,
168
+ the extra bytes won't be missed terribly, but if range checking is on
169
+ and we used a larger request, a whole extra 4096 bytes would be
170
+ allocated.
171
+
172
+ These number are irrelevant to the new GNU malloc. I suspect it is
173
+ less sensitive to the size of the request. */
174
+ int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
175
+ + 4 + DEFAULT_ROUNDING - 1)
176
+ & ~(DEFAULT_ROUNDING - 1));
177
+ size = 4096 - extra;
178
+ }
179
+
180
+ h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
181
+ h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
182
+ h->chunk_size = size;
183
+ h->alignment_mask = alignment - 1;
184
+ h->use_extra_arg = 0;
185
+
186
+ chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
187
+ if (!chunk)
188
+ (*obstack_alloc_failed_handler) ();
189
+ h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
190
+ alignment - 1);
191
+ h->chunk_limit = chunk->limit
192
+ = (char *) chunk + h->chunk_size;
193
+ chunk->prev = 0;
194
+ /* The initial chunk now contains no empty object. */
195
+ h->maybe_empty_object = 0;
196
+ h->alloc_failed = 0;
197
+ return 1;
198
+}
199
+
200
+int
201
+_obstack_begin_1 (struct obstack *h, int size, int alignment,
202
+ void *(*chunkfun) (void *, long),
203
+ void (*freefun) (void *, void *),
204
+ void *arg)
205
+{
206
+ register struct _obstack_chunk *chunk; /* points to new chunk */
207
+
208
+ if (alignment == 0)
209
+ alignment = DEFAULT_ALIGNMENT;
210
+ if (size == 0)
211
+ /* Default size is what GNU malloc can fit in a 4096-byte block. */
212
+ {
213
+ /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
214
+ Use the values for range checking, because if range checking is off,
215
+ the extra bytes won't be missed terribly, but if range checking is on
216
+ and we used a larger request, a whole extra 4096 bytes would be
217
+ allocated.
218
+
219
+ These number are irrelevant to the new GNU malloc. I suspect it is
220
+ less sensitive to the size of the request. */
221
+ int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
222
+ + 4 + DEFAULT_ROUNDING - 1)
223
+ & ~(DEFAULT_ROUNDING - 1));
224
+ size = 4096 - extra;
225
+ }
226
+
227
+ h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
228
+ h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
229
+ h->chunk_size = size;
230
+ h->alignment_mask = alignment - 1;
231
+ h->extra_arg = arg;
232
+ h->use_extra_arg = 1;
233
+
234
+ chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
235
+ if (!chunk)
236
+ (*obstack_alloc_failed_handler) ();
237
+ h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
238
+ alignment - 1);
239
+ h->chunk_limit = chunk->limit
240
+ = (char *) chunk + h->chunk_size;
241
+ chunk->prev = 0;
242
+ /* The initial chunk now contains no empty object. */
243
+ h->maybe_empty_object = 0;
244
+ h->alloc_failed = 0;
245
+ return 1;
246
+}
247
+
248
+/* Allocate a new current chunk for the obstack *H
249
+ on the assumption that LENGTH bytes need to be added
250
+ to the current object, or a new object of length LENGTH allocated.
251
+ Copies any partial object from the end of the old chunk
252
+ to the beginning of the new one. */
253
+
254
+void
255
+_obstack_newchunk (struct obstack *h, int length)
256
+{
257
+ register struct _obstack_chunk *old_chunk = h->chunk;
258
+ register struct _obstack_chunk *new_chunk;
259
+ register long new_size;
260
+ register long obj_size = h->next_free - h->object_base;
261
+ register long i;
262
+ long already;
263
+ char *object_base;
264
+
265
+ /* Compute size for new chunk. */
266
+ new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
267
+ if (new_size < h->chunk_size)
268
+ new_size = h->chunk_size;
269
+
270
+ /* Allocate and initialize the new chunk. */
271
+ new_chunk = CALL_CHUNKFUN (h, new_size);
272
+ if (!new_chunk)
273
+ (*obstack_alloc_failed_handler) ();
274
+ h->chunk = new_chunk;
275
+ new_chunk->prev = old_chunk;
276
+ new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
277
+
278
+ /* Compute an aligned object_base in the new chunk */
279
+ object_base =
280
+ __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
281
+
282
+ /* Move the existing object to the new chunk.
283
+ Word at a time is fast and is safe if the object
284
+ is sufficiently aligned. */
285
+ if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
286
+ {
287
+ for (i = obj_size / sizeof (COPYING_UNIT) - 1;
288
+ i >= 0; i--)
289
+ ((COPYING_UNIT *)object_base)[i]
290
+ = ((COPYING_UNIT *)h->object_base)[i];
291
+ /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
292
+ but that can cross a page boundary on a machine
293
+ which does not do strict alignment for COPYING_UNITS. */
294
+ already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
295
+ }
296
+ else
297
+ already = 0;
298
+ /* Copy remaining bytes one by one. */
299
+ for (i = already; i < obj_size; i++)
300
+ object_base[i] = h->object_base[i];
301
+
302
+ /* If the object just copied was the only data in OLD_CHUNK,
303
+ free that chunk and remove it from the chain.
304
+ But not if that chunk might contain an empty object. */
305
+ if (! h->maybe_empty_object
306
+ && (h->object_base
307
+ == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
308
+ h->alignment_mask)))
309
+ {
310
+ new_chunk->prev = old_chunk->prev;
311
+ CALL_FREEFUN (h, old_chunk);
312
+ }
313
+
314
+ h->object_base = object_base;
315
+ h->next_free = h->object_base + obj_size;
316
+ /* The new chunk certainly contains no empty object yet. */
317
+ h->maybe_empty_object = 0;
318
+}
319
+# ifdef _LIBC
320
+libc_hidden_def (_obstack_newchunk)
321
+# endif
322
+
323
+/* Return nonzero if object OBJ has been allocated from obstack H.
324
+ This is here for debugging.
325
+ If you use it in a program, you are probably losing. */
326
+
327
+/* Suppress -Wmissing-prototypes warning. We don't want to declare this in
328
+ obstack.h because it is just for debugging. */
329
+int _obstack_allocated_p (struct obstack *h, void *obj);
330
+
331
+int
332
+_obstack_allocated_p (struct obstack *h, void *obj)
333
+{
334
+ register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
335
+ register struct _obstack_chunk *plp; /* point to previous chunk if any */
336
+
337
+ lp = (h)->chunk;
338
+ /* We use >= rather than > since the object cannot be exactly at
339
+ the beginning of the chunk but might be an empty object exactly
340
+ at the end of an adjacent chunk. */
341
+ while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
342
+ {
343
+ plp = lp->prev;
344
+ lp = plp;
345
+ }
346
+ return lp != 0;
347
+}
348
+
349
+/* Free objects in obstack H, including OBJ and everything allocate
350
+ more recently than OBJ. If OBJ is zero, free everything in H. */
351
+
352
+# undef obstack_free
353
+
354
+void
355
+obstack_free (struct obstack *h, void *obj)
356
+{
357
+ register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
358
+ register struct _obstack_chunk *plp; /* point to previous chunk if any */
359
+
360
+ lp = h->chunk;
361
+ /* We use >= because there cannot be an object at the beginning of a chunk.
362
+ But there can be an empty object at that address
363
+ at the end of another chunk. */
364
+ while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
365
+ {
366
+ plp = lp->prev;
367
+ CALL_FREEFUN (h, lp);
368
+ lp = plp;
369
+ /* If we switch chunks, we can't tell whether the new current
370
+ chunk contains an empty object, so assume that it may. */
371
+ h->maybe_empty_object = 1;
372
+ }
373
+ if (lp)
374
+ {
375
+ h->object_base = h->next_free = (char *) (obj);
376
+ h->chunk_limit = lp->limit;
377
+ h->chunk = lp;
378
+ }
379
+ else if (obj != 0)
380
+ /* obj is not in any of the chunks! */
381
+ abort ();
382
+}
383
+
384
+# ifdef _LIBC
385
+/* Older versions of libc used a function _obstack_free intended to be
386
+ called by non-GCC compilers. */
387
+strong_alias (obstack_free, _obstack_free)
388
+# endif
389
+
390
+int
391
+_obstack_memory_used (struct obstack *h)
392
+{
393
+ register struct _obstack_chunk* lp;
394
+ register int nbytes = 0;
395
+
396
+ for (lp = h->chunk; lp != 0; lp = lp->prev)
397
+ {
398
+ nbytes += lp->limit - (char *) lp;
399
+ }
400
+ return nbytes;
401
+}
402
+
403
+/* Define the error handler. */
404
+# ifdef _LIBC
405
+# include <libintl.h>
406
+# else
407
+# include "gettext.h"
408
+# endif
409
+# ifndef _
410
+# define _(msgid) gettext (msgid)
411
+# endif
412
+
413
+# ifdef _LIBC
414
+# include <libio/iolibio.h>
415
+# endif
416
+
417
+# ifndef __attribute__
418
+/* This feature is available in gcc versions 2.5 and later. */
419
+# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
420
+# define __attribute__(Spec) /* empty */
421
+# endif
422
+# endif
423
+
424
+static void
425
+__attribute__ ((noreturn))
426
+print_and_abort (void)
427
+{
428
+ /* Don't change any of these strings. Yes, it would be possible to add
429
+ the newline to the string and use fputs or so. But this must not
430
+ happen because the "memory exhausted" message appears in other places
431
+ like this and the translation should be reused instead of creating
432
+ a very similar string which requires a separate translation. */
433
+# ifdef _LIBC
434
+ (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
435
+# else
436
+ fprintf (stderr, "%s\n", _("memory exhausted"));
437
+# endif
438
+ exit (obstack_exit_failure);
439
+}
440
+
441
+#endif /* !ELIDE_CODE */
compat/obstack.h
new
+509
@@ -0,0 +1,509 @@
1
+/* obstack.h - object stack macros
2
+ Copyright (C) 1988-1994,1996-1999,2003,2004,2005,2009
3
+ Free Software Foundation, Inc.
4
+ This file is part of the GNU C Library.
5
+
6
+ The GNU C Library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ The GNU C Library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with the GNU C Library; if not, write to the Free
18
+ Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ Boston, MA 02110-1301, USA. */
20
+
21
+/* Summary:
22
+
23
+All the apparent functions defined here are macros. The idea
24
+is that you would use these pre-tested macros to solve a
25
+very specific set of problems, and they would run fast.
26
+Caution: no side-effects in arguments please!! They may be
27
+evaluated MANY times!!
28
+
29
+These macros operate a stack of objects. Each object starts life
30
+small, and may grow to maturity. (Consider building a word syllable
31
+by syllable.) An object can move while it is growing. Once it has
32
+been "finished" it never changes address again. So the "top of the
33
+stack" is typically an immature growing object, while the rest of the
34
+stack is of mature, fixed size and fixed address objects.
35
+
36
+These routines grab large chunks of memory, using a function you
37
+supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
38
+by calling `obstack_chunk_free'. You must define them and declare
39
+them before using any obstack macros.
40
+
41
+Each independent stack is represented by a `struct obstack'.
42
+Each of the obstack macros expects a pointer to such a structure
43
+as the first argument.
44
+
45
+One motivation for this package is the problem of growing char strings
46
+in symbol tables. Unless you are "fascist pig with a read-only mind"
47
+--Gosper's immortal quote from HAKMEM item 154, out of context--you
48
+would not like to put any arbitrary upper limit on the length of your
49
+symbols.
50
+
51
+In practice this often means you will build many short symbols and a
52
+few long symbols. At the time you are reading a symbol you don't know
53
+how long it is. One traditional method is to read a symbol into a
54
+buffer, realloc()ating the buffer every time you try to read a symbol
55
+that is longer than the buffer. This is beaut, but you still will
56
+want to copy the symbol from the buffer to a more permanent
57
+symbol-table entry say about half the time.
58
+
59
+With obstacks, you can work differently. Use one obstack for all symbol
60
+names. As you read a symbol, grow the name in the obstack gradually.
61
+When the name is complete, finalize it. Then, if the symbol exists already,
62
+free the newly read name.
63
+
64
+The way we do this is to take a large chunk, allocating memory from
65
+low addresses. When you want to build a symbol in the chunk you just
66
+add chars above the current "high water mark" in the chunk. When you
67
+have finished adding chars, because you got to the end of the symbol,
68
+you know how long the chars are, and you can create a new object.
69
+Mostly the chars will not burst over the highest address of the chunk,
70
+because you would typically expect a chunk to be (say) 100 times as
71
+long as an average object.
72
+
73
+In case that isn't clear, when we have enough chars to make up
74
+the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
75
+so we just point to it where it lies. No moving of chars is
76
+needed and this is the second win: potentially long strings need
77
+never be explicitly shuffled. Once an object is formed, it does not
78
+change its address during its lifetime.
79
+
80
+When the chars burst over a chunk boundary, we allocate a larger
81
+chunk, and then copy the partly formed object from the end of the old
82
+chunk to the beginning of the new larger chunk. We then carry on
83
+accreting characters to the end of the object as we normally would.
84
+
85
+A special macro is provided to add a single char at a time to a
86
+growing object. This allows the use of register variables, which
87
+break the ordinary 'growth' macro.
88
+
89
+Summary:
90
+ We allocate large chunks.
91
+ We carve out one object at a time from the current chunk.
92
+ Once carved, an object never moves.
93
+ We are free to append data of any size to the currently
94
+ growing object.
95
+ Exactly one object is growing in an obstack at any one time.
96
+ You can run one obstack per control block.
97
+ You may have as many control blocks as you dare.
98
+ Because of the way we do it, you can `unwind' an obstack
99
+ back to a previous state. (You may remove objects much
100
+ as you would with a stack.)
101
+*/
102
+
103
+
104
+/* Don't do the contents of this file more than once. */
105
+
106
+#ifndef _OBSTACK_H
107
+#define _OBSTACK_H 1
108
+
109
+#ifdef __cplusplus
110
+extern "C" {
111
+#endif
112
+
113
+/* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
114
+ defined, as with GNU C, use that; that way we don't pollute the
115
+ namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
116
+ and use ptrdiff_t. */
117
+
118
+#ifdef __PTRDIFF_TYPE__
119
+# define PTR_INT_TYPE __PTRDIFF_TYPE__
120
+#else
121
+# include <stddef.h>
122
+# define PTR_INT_TYPE ptrdiff_t
123
+#endif
124
+
125
+/* If B is the base of an object addressed by P, return the result of
126
+ aligning P to the next multiple of A + 1. B and P must be of type
127
+ char *. A + 1 must be a power of 2. */
128
+
129
+#define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
130
+
131
+/* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
132
+ where pointers can be converted to integers, aligned as integers,
133
+ and converted back again. If PTR_INT_TYPE is narrower than a
134
+ pointer (e.g., the AS/400), play it safe and compute the alignment
135
+ relative to B. Otherwise, use the faster strategy of computing the
136
+ alignment relative to 0. */
137
+
138
+#define __PTR_ALIGN(B, P, A) \
139
+ __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
140
+ P, A)
141
+
142
+#include <string.h>
143
+
144
+struct _obstack_chunk /* Lives at front of each chunk. */
145
+{
146
+ char *limit; /* 1 past end of this chunk */
147
+ struct _obstack_chunk *prev; /* address of prior chunk or NULL */
148
+ char contents[4]; /* objects begin here */
149
+};
150
+
151
+struct obstack /* control current object in current chunk */
152
+{
153
+ long chunk_size; /* preferred size to allocate chunks in */
154
+ struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
155
+ char *object_base; /* address of object we are building */
156
+ char *next_free; /* where to add next char to current object */
157
+ char *chunk_limit; /* address of char after current chunk */
158
+ union
159
+ {
160
+ PTR_INT_TYPE tempint;
161
+ void *tempptr;
162
+ } temp; /* Temporary for some macros. */
163
+ int alignment_mask; /* Mask of alignment for each object. */
164
+ /* These prototypes vary based on `use_extra_arg', and we use
165
+ casts to the prototypeless function type in all assignments,
166
+ but having prototypes here quiets -Wstrict-prototypes. */
167
+ struct _obstack_chunk *(*chunkfun) (void *, long);
168
+ void (*freefun) (void *, struct _obstack_chunk *);
169
+ void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
170
+ unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
171
+ unsigned maybe_empty_object:1;/* There is a possibility that the current
172
+ chunk contains a zero-length object. This
173
+ prevents freeing the chunk if we allocate
174
+ a bigger chunk to replace it. */
175
+ unsigned alloc_failed:1; /* No longer used, as we now call the failed
176
+ handler on error, but retained for binary
177
+ compatibility. */
178
+};
179
+
180
+/* Declare the external functions we use; they are in obstack.c. */
181
+
182
+extern void _obstack_newchunk (struct obstack *, int);
183
+extern int _obstack_begin (struct obstack *, int, int,
184
+ void *(*) (long), void (*) (void *));
185
+extern int _obstack_begin_1 (struct obstack *, int, int,
186
+ void *(*) (void *, long),
187
+ void (*) (void *, void *), void *);
188
+extern int _obstack_memory_used (struct obstack *);
189
+
190
+void obstack_free (struct obstack *__obstack, void *__block);
191
+
192
+
193
+/* Error handler called when `obstack_chunk_alloc' failed to allocate
194
+ more memory. This can be set to a user defined function which
195
+ should either abort gracefully or use longjump - but shouldn't
196
+ return. The default action is to print a message and abort. */
197
+extern void (*obstack_alloc_failed_handler) (void);
198
+
199
+/* Exit value used when `print_and_abort' is used. */
200
+extern int obstack_exit_failure;
201
+
202
+/* Pointer to beginning of object being allocated or to be allocated next.
203
+ Note that this might not be the final address of the object
204
+ because a new chunk might be needed to hold the final size. */
205
+
206
+#define obstack_base(h) ((void *) (h)->object_base)
207
+
208
+/* Size for allocating ordinary chunks. */
209
+
210
+#define obstack_chunk_size(h) ((h)->chunk_size)
211
+
212
+/* Pointer to next byte not yet allocated in current chunk. */
213
+
214
+#define obstack_next_free(h) ((h)->next_free)
215
+
216
+/* Mask specifying low bits that should be clear in address of an object. */
217
+
218
+#define obstack_alignment_mask(h) ((h)->alignment_mask)
219
+
220
+/* To prevent prototype warnings provide complete argument list. */
221
+#define obstack_init(h) \
222
+ _obstack_begin ((h), 0, 0, \
223
+ (void *(*) (long)) obstack_chunk_alloc, \
224
+ (void (*) (void *)) obstack_chunk_free)
225
+
226
+#define obstack_begin(h, size) \
227
+ _obstack_begin ((h), (size), 0, \
228
+ (void *(*) (long)) obstack_chunk_alloc, \
229
+ (void (*) (void *)) obstack_chunk_free)
230
+
231
+#define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
232
+ _obstack_begin ((h), (size), (alignment), \
233
+ (void *(*) (long)) (chunkfun), \
234
+ (void (*) (void *)) (freefun))
235
+
236
+#define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
237
+ _obstack_begin_1 ((h), (size), (alignment), \
238
+ (void *(*) (void *, long)) (chunkfun), \
239
+ (void (*) (void *, void *)) (freefun), (arg))
240
+
241
+#define obstack_chunkfun(h, newchunkfun) \
242
+ ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
243
+
244
+#define obstack_freefun(h, newfreefun) \
245
+ ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
246
+
247
+#define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
248
+
249
+#define obstack_blank_fast(h,n) ((h)->next_free += (n))
250
+
251
+#define obstack_memory_used(h) _obstack_memory_used (h)
252
+
253
+#if defined __GNUC__ && defined __STDC__ && __STDC__
254
+/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
255
+ does not implement __extension__. But that compiler doesn't define
256
+ __GNUC_MINOR__. */
257
+# if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
258
+# define __extension__
259
+# endif
260
+
261
+/* For GNU C, if not -traditional,
262
+ we can define these macros to compute all args only once
263
+ without using a global variable.
264
+ Also, we can avoid using the `temp' slot, to make faster code. */
265
+
266
+# define obstack_object_size(OBSTACK) \
267
+ __extension__ \
268
+ ({ struct obstack const *__o = (OBSTACK); \
269
+ (unsigned) (__o->next_free - __o->object_base); })
270
+
271
+# define obstack_room(OBSTACK) \
272
+ __extension__ \
273
+ ({ struct obstack const *__o = (OBSTACK); \
274
+ (unsigned) (__o->chunk_limit - __o->next_free); })
275
+
276
+# define obstack_make_room(OBSTACK,length) \
277
+__extension__ \
278
+({ struct obstack *__o = (OBSTACK); \
279
+ int __len = (length); \
280
+ if (__o->chunk_limit - __o->next_free < __len) \
281
+ _obstack_newchunk (__o, __len); \
282
+ (void) 0; })
283
+
284
+# define obstack_empty_p(OBSTACK) \
285
+ __extension__ \
286
+ ({ struct obstack const *__o = (OBSTACK); \
287
+ (__o->chunk->prev == 0 \
288
+ && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
289
+ __o->chunk->contents, \
290
+ __o->alignment_mask)); })
291
+
292
+# define obstack_grow(OBSTACK,where,length) \
293
+__extension__ \
294
+({ struct obstack *__o = (OBSTACK); \
295
+ int __len = (length); \
296
+ if (__o->next_free + __len > __o->chunk_limit) \
297
+ _obstack_newchunk (__o, __len); \
298
+ memcpy (__o->next_free, where, __len); \
299
+ __o->next_free += __len; \
300
+ (void) 0; })
301
+
302
+# define obstack_grow0(OBSTACK,where,length) \
303
+__extension__ \
304
+({ struct obstack *__o = (OBSTACK); \
305
+ int __len = (length); \
306
+ if (__o->next_free + __len + 1 > __o->chunk_limit) \
307
+ _obstack_newchunk (__o, __len + 1); \
308
+ memcpy (__o->next_free, where, __len); \
309
+ __o->next_free += __len; \
310
+ *(__o->next_free)++ = 0; \
311
+ (void) 0; })
312
+
313
+# define obstack_1grow(OBSTACK,datum) \
314
+__extension__ \
315
+({ struct obstack *__o = (OBSTACK); \
316
+ if (__o->next_free + 1 > __o->chunk_limit) \
317
+ _obstack_newchunk (__o, 1); \
318
+ obstack_1grow_fast (__o, datum); \
319
+ (void) 0; })
320
+
321
+/* These assume that the obstack alignment is good enough for pointers
322
+ or ints, and that the data added so far to the current object
323
+ shares that much alignment. */
324
+
325
+# define obstack_ptr_grow(OBSTACK,datum) \
326
+__extension__ \
327
+({ struct obstack *__o = (OBSTACK); \
328
+ if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
329
+ _obstack_newchunk (__o, sizeof (void *)); \
330
+ obstack_ptr_grow_fast (__o, datum); }) \
331
+
332
+# define obstack_int_grow(OBSTACK,datum) \
333
+__extension__ \
334
+({ struct obstack *__o = (OBSTACK); \
335
+ if (__o->next_free + sizeof (int) > __o->chunk_limit) \
336
+ _obstack_newchunk (__o, sizeof (int)); \
337
+ obstack_int_grow_fast (__o, datum); })
338
+
339
+# define obstack_ptr_grow_fast(OBSTACK,aptr) \
340
+__extension__ \
341
+({ struct obstack *__o1 = (OBSTACK); \
342
+ *(const void **) __o1->next_free = (aptr); \
343
+ __o1->next_free += sizeof (const void *); \
344
+ (void) 0; })
345
+
346
+# define obstack_int_grow_fast(OBSTACK,aint) \
347
+__extension__ \
348
+({ struct obstack *__o1 = (OBSTACK); \
349
+ *(int *) __o1->next_free = (aint); \
350
+ __o1->next_free += sizeof (int); \
351
+ (void) 0; })
352
+
353
+# define obstack_blank(OBSTACK,length) \
354
+__extension__ \
355
+({ struct obstack *__o = (OBSTACK); \
356
+ int __len = (length); \
357
+ if (__o->chunk_limit - __o->next_free < __len) \
358
+ _obstack_newchunk (__o, __len); \
359
+ obstack_blank_fast (__o, __len); \
360
+ (void) 0; })
361
+
362
+# define obstack_alloc(OBSTACK,length) \
363
+__extension__ \
364
+({ struct obstack *__h = (OBSTACK); \
365
+ obstack_blank (__h, (length)); \
366
+ obstack_finish (__h); })
367
+
368
+# define obstack_copy(OBSTACK,where,length) \
369
+__extension__ \
370
+({ struct obstack *__h = (OBSTACK); \
371
+ obstack_grow (__h, (where), (length)); \
372
+ obstack_finish (__h); })
373
+
374
+# define obstack_copy0(OBSTACK,where,length) \
375
+__extension__ \
376
+({ struct obstack *__h = (OBSTACK); \
377
+ obstack_grow0 (__h, (where), (length)); \
378
+ obstack_finish (__h); })
379
+
380
+/* The local variable is named __o1 to avoid a name conflict
381
+ when obstack_blank is called. */
382
+# define obstack_finish(OBSTACK) \
383
+__extension__ \
384
+({ struct obstack *__o1 = (OBSTACK); \
385
+ void *__value = (void *) __o1->object_base; \
386
+ if (__o1->next_free == __value) \
387
+ __o1->maybe_empty_object = 1; \
388
+ __o1->next_free \
389
+ = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
390
+ __o1->alignment_mask); \
391
+ if (__o1->next_free - (char *)__o1->chunk \
392
+ > __o1->chunk_limit - (char *)__o1->chunk) \
393
+ __o1->next_free = __o1->chunk_limit; \
394
+ __o1->object_base = __o1->next_free; \
395
+ __value; })
396
+
397
+# define obstack_free(OBSTACK, OBJ) \
398
+__extension__ \
399
+({ struct obstack *__o = (OBSTACK); \
400
+ void *__obj = (OBJ); \
401
+ if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
402
+ __o->next_free = __o->object_base = (char *)__obj; \
403
+ else (obstack_free) (__o, __obj); })
404
+
405
+#else /* not __GNUC__ or not __STDC__ */
406
+
407
+# define obstack_object_size(h) \
408
+ (unsigned) ((h)->next_free - (h)->object_base)
409
+
410
+# define obstack_room(h) \
411
+ (unsigned) ((h)->chunk_limit - (h)->next_free)
412
+
413
+# define obstack_empty_p(h) \
414
+ ((h)->chunk->prev == 0 \
415
+ && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
416
+ (h)->chunk->contents, \
417
+ (h)->alignment_mask))
418
+
419
+/* Note that the call to _obstack_newchunk is enclosed in (..., 0)
420
+ so that we can avoid having void expressions
421
+ in the arms of the conditional expression.
422
+ Casting the third operand to void was tried before,
423
+ but some compilers won't accept it. */
424
+
425
+# define obstack_make_room(h,length) \
426
+( (h)->temp.tempint = (length), \
427
+ (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
428
+ ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
429
+
430
+# define obstack_grow(h,where,length) \
431
+( (h)->temp.tempint = (length), \
432
+ (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
433
+ ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
434
+ memcpy ((h)->next_free, where, (h)->temp.tempint), \
435
+ (h)->next_free += (h)->temp.tempint)
436
+
437
+# define obstack_grow0(h,where,length) \
438
+( (h)->temp.tempint = (length), \
439
+ (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
440
+ ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
441
+ memcpy ((h)->next_free, where, (h)->temp.tempint), \
442
+ (h)->next_free += (h)->temp.tempint, \
443
+ *((h)->next_free)++ = 0)
444
+
445
+# define obstack_1grow(h,datum) \
446
+( (((h)->next_free + 1 > (h)->chunk_limit) \
447
+ ? (_obstack_newchunk ((h), 1), 0) : 0), \
448
+ obstack_1grow_fast (h, datum))
449
+
450
+# define obstack_ptr_grow(h,datum) \
451
+( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
452
+ ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
453
+ obstack_ptr_grow_fast (h, datum))
454
+
455
+# define obstack_int_grow(h,datum) \
456
+( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
457
+ ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
458
+ obstack_int_grow_fast (h, datum))
459
+
460
+# define obstack_ptr_grow_fast(h,aptr) \
461
+ (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
462
+
463
+# define obstack_int_grow_fast(h,aint) \
464
+ (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
465
+
466
+# define obstack_blank(h,length) \
467
+( (h)->temp.tempint = (length), \
468
+ (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
469
+ ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
470
+ obstack_blank_fast (h, (h)->temp.tempint))
471
+
472
+# define obstack_alloc(h,length) \
473
+ (obstack_blank ((h), (length)), obstack_finish ((h)))
474
+
475
+# define obstack_copy(h,where,length) \
476
+ (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
477
+
478
+# define obstack_copy0(h,where,length) \
479
+ (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
480
+
481
+# define obstack_finish(h) \
482
+( ((h)->next_free == (h)->object_base \
483
+ ? (((h)->maybe_empty_object = 1), 0) \
484
+ : 0), \
485
+ (h)->temp.tempptr = (h)->object_base, \
486
+ (h)->next_free \
487
+ = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
488
+ (h)->alignment_mask), \
489
+ (((h)->next_free - (char *) (h)->chunk \
490
+ > (h)->chunk_limit - (char *) (h)->chunk) \
491
+ ? ((h)->next_free = (h)->chunk_limit) : 0), \
492
+ (h)->object_base = (h)->next_free, \
493
+ (h)->temp.tempptr)
494
+
495
+# define obstack_free(h,obj) \
496
+( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
497
+ ((((h)->temp.tempint > 0 \
498
+ && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
499
+ ? (int) ((h)->next_free = (h)->object_base \
500
+ = (h)->temp.tempint + (char *) (h)->chunk) \
501
+ : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
502
+
503
+#endif /* not __GNUC__ or not __STDC__ */
504
+
505
+#ifdef __cplusplus
506
+} /* C++ */
507
+#endif
508
+
509
+#endif /* obstack.h */