Mercurial > hg > octave-kai > gnulib-hg
annotate lib/obstack.c @ 5948:f410a15537e7
(make_dir_parents): Don't apply sizeof to a hard-coded type name.
Use the variable name instead.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Wed, 29 Jun 2005 21:28:00 +0000 |
parents | a48fb0e98c8c |
children | 56829cd68e67 |
rev | line source |
---|---|
334 | 1 /* obstack.c - subroutines used implicitly by object stack macros |
4667 | 2 |
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, | |
5587
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
5 Inc. |
4667 | 6 |
4020 | 7 This program is free software; you can redistribute it and/or modify |
8 it under the terms of the GNU General Public License as published by | |
9 the Free Software Foundation; either version 2, or (at your option) | |
10 any later version. | |
334 | 11 |
4020 | 12 This program is distributed in the hope that it will be useful, |
881 | 13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4020 | 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 GNU General Public License for more details. | |
334 | 16 |
4020 | 17 You should have received a copy of the GNU General Public License along |
18 with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5587
diff
changeset
|
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
334 | 20 |
2475 | 21 #ifdef HAVE_CONFIG_H |
22 # include <config.h> | |
23 #endif | |
24 | |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
25 #ifdef _LIBC |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
26 # include <obstack.h> |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
27 # include <shlib-compat.h> |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
28 #else |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
29 # include "obstack.h" |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
30 #endif |
334 | 31 |
779 | 32 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be |
33 incremented whenever callers compiled using an old obstack.h can no | |
34 longer properly call the functions in this obstack.c. */ | |
35 #define OBSTACK_INTERFACE_VERSION 1 | |
334 | 36 |
37 /* Comment out all this code if we are using the GNU C Library, and are not | |
779 | 38 actually compiling the library itself, and the installed library |
39 supports the same library interface we do. This code is part of the GNU | |
40 C Library, but also included in many other GNU distributions. Compiling | |
334 | 41 and linking in this code is a waste when using the GNU C library |
42 (especially if it is a shared library). Rather than having every GNU | |
779 | 43 program understand `configure --with-gnu-libc' and omit the object |
44 files, it is simpler to just do this in the source for each such file. */ | |
334 | 45 |
779 | 46 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */ |
3362 | 47 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 |
2475 | 48 # include <gnu-versions.h> |
49 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION | |
50 # define ELIDE_CODE | |
51 # endif | |
779 | 52 #endif |
53 | |
3361 | 54 #if defined _LIBC && defined USE_IN_LIBIO |
55 # include <wchar.h> | |
56 #endif | |
779 | 57 |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
58 #include <stddef.h> |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
59 |
779 | 60 #ifndef ELIDE_CODE |
334 | 61 |
62 | |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
63 # if HAVE_INTTYPES_H |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
64 # include <inttypes.h> |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
65 # endif |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
66 # if HAVE_STDINT_H || defined _LIBC |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
67 # include <stdint.h> |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
68 # endif |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
69 |
334 | 70 /* Determine default alignment. */ |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
71 union fooround |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
72 { |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
73 uintmax_t i; |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
74 long double d; |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
75 void *p; |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
76 }; |
5587
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
77 struct fooalign |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
78 { |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
79 char c; |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
80 union fooround u; |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
81 }; |
334 | 82 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. |
83 But in fact it might be less smart and round addresses to as much as | |
84 DEFAULT_ROUNDING. So we prepare for it to do that. */ | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
85 enum |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
86 { |
5587
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
87 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u), |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
88 DEFAULT_ROUNDING = sizeof (union fooround) |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
89 }; |
334 | 90 |
91 /* When we copy a long block of data, this is the unit to do it with. | |
92 On some machines, copying successive ints does not work; | |
93 in such a case, redefine COPYING_UNIT to `long' (if that works) | |
94 or `char' as a last resort. */ | |
2475 | 95 # ifndef COPYING_UNIT |
96 # define COPYING_UNIT int | |
97 # endif | |
334 | 98 |
881 | 99 |
100 /* The functions allocating more room by calling `obstack_chunk_alloc' | |
101 jump to the handler pointed to by `obstack_alloc_failed_handler'. | |
2475 | 102 This can be set to a user defined function which should either |
103 abort gracefully or use longjump - but shouldn't return. This | |
104 variable by default points to the internal function | |
881 | 105 `print_and_abort'. */ |
106 static void print_and_abort (void); | |
107 void (*obstack_alloc_failed_handler) (void) = print_and_abort; | |
108 | |
109 /* Exit value used when `print_and_abort' is used. */ | |
4667 | 110 # include <stdlib.h> |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
111 # ifdef _LIBC |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
112 int obstack_exit_failure = EXIT_FAILURE; |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
113 # else |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
114 # include "exitfail.h" |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
115 # define obstack_exit_failure exit_failure |
2475 | 116 # endif |
881 | 117 |
5071
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
118 # ifdef _LIBC |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
119 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4) |
5071
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
120 /* A looong time ago (before 1994, anyway; we're not sure) this global variable |
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
121 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C |
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
122 library still exports it because somebody might use it. */ |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
123 struct obstack *_obstack_compat; |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
124 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0); |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
125 # endif |
5071
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
126 # endif |
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
127 |
334 | 128 /* Define a macro that either calls functions with the traditional malloc/free |
129 calling interface, or calls functions with the mmalloc/mfree interface | |
130 (that adds an extra first argument), based on the state of use_extra_arg. | |
131 For free, do not use ?:, since some compilers, like the MIPS compilers, | |
132 do not allow (expr) ? void : void. */ | |
133 | |
4667 | 134 # define CALL_CHUNKFUN(h, size) \ |
881 | 135 (((h) -> use_extra_arg) \ |
136 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ | |
137 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size))) | |
138 | |
4667 | 139 # define CALL_FREEFUN(h, old_chunk) \ |
881 | 140 do { \ |
141 if ((h) -> use_extra_arg) \ | |
142 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ | |
143 else \ | |
144 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \ | |
145 } while (0) | |
334 | 146 |
147 | |
148 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default). | |
149 Objects start on multiples of ALIGNMENT (0 means use default). | |
150 CHUNKFUN is the function to use to allocate chunks, | |
151 and FREEFUN the function to free them. | |
152 | |
2475 | 153 Return nonzero if successful, calls obstack_alloc_failed_handler if |
154 allocation fails. */ | |
334 | 155 |
156 int | |
4667 | 157 _obstack_begin (struct obstack *h, |
158 int size, int alignment, | |
159 void *(*chunkfun) (long), | |
160 void (*freefun) (void *)) | |
334 | 161 { |
881 | 162 register struct _obstack_chunk *chunk; /* points to new chunk */ |
334 | 163 |
164 if (alignment == 0) | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
165 alignment = DEFAULT_ALIGNMENT; |
334 | 166 if (size == 0) |
167 /* Default size is what GNU malloc can fit in a 4096-byte block. */ | |
168 { | |
169 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. | |
170 Use the values for range checking, because if range checking is off, | |
171 the extra bytes won't be missed terribly, but if range checking is on | |
172 and we used a larger request, a whole extra 4096 bytes would be | |
173 allocated. | |
174 | |
175 These number are irrelevant to the new GNU malloc. I suspect it is | |
176 less sensitive to the size of the request. */ | |
177 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) | |
178 + 4 + DEFAULT_ROUNDING - 1) | |
179 & ~(DEFAULT_ROUNDING - 1)); | |
180 size = 4096 - extra; | |
181 } | |
182 | |
881 | 183 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun; |
184 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; | |
334 | 185 h->chunk_size = size; |
186 h->alignment_mask = alignment - 1; | |
187 h->use_extra_arg = 0; | |
188 | |
189 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); | |
190 if (!chunk) | |
881 | 191 (*obstack_alloc_failed_handler) (); |
5174
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
192 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents, |
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
193 alignment - 1); |
334 | 194 h->chunk_limit = chunk->limit |
195 = (char *) chunk + h->chunk_size; | |
196 chunk->prev = 0; | |
197 /* The initial chunk now contains no empty object. */ | |
198 h->maybe_empty_object = 0; | |
881 | 199 h->alloc_failed = 0; |
334 | 200 return 1; |
201 } | |
202 | |
203 int | |
4667 | 204 _obstack_begin_1 (struct obstack *h, int size, int alignment, |
205 void *(*chunkfun) (void *, long), | |
206 void (*freefun) (void *, void *), | |
207 void *arg) | |
334 | 208 { |
881 | 209 register struct _obstack_chunk *chunk; /* points to new chunk */ |
334 | 210 |
211 if (alignment == 0) | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
212 alignment = DEFAULT_ALIGNMENT; |
334 | 213 if (size == 0) |
214 /* Default size is what GNU malloc can fit in a 4096-byte block. */ | |
215 { | |
216 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc. | |
217 Use the values for range checking, because if range checking is off, | |
218 the extra bytes won't be missed terribly, but if range checking is on | |
219 and we used a larger request, a whole extra 4096 bytes would be | |
220 allocated. | |
221 | |
222 These number are irrelevant to the new GNU malloc. I suspect it is | |
223 less sensitive to the size of the request. */ | |
224 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) | |
225 + 4 + DEFAULT_ROUNDING - 1) | |
226 & ~(DEFAULT_ROUNDING - 1)); | |
227 size = 4096 - extra; | |
228 } | |
229 | |
881 | 230 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun; |
231 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun; | |
334 | 232 h->chunk_size = size; |
233 h->alignment_mask = alignment - 1; | |
234 h->extra_arg = arg; | |
235 h->use_extra_arg = 1; | |
236 | |
237 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); | |
238 if (!chunk) | |
881 | 239 (*obstack_alloc_failed_handler) (); |
5174
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
240 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents, |
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
241 alignment - 1); |
334 | 242 h->chunk_limit = chunk->limit |
243 = (char *) chunk + h->chunk_size; | |
244 chunk->prev = 0; | |
245 /* The initial chunk now contains no empty object. */ | |
246 h->maybe_empty_object = 0; | |
881 | 247 h->alloc_failed = 0; |
334 | 248 return 1; |
249 } | |
250 | |
251 /* Allocate a new current chunk for the obstack *H | |
252 on the assumption that LENGTH bytes need to be added | |
253 to the current object, or a new object of length LENGTH allocated. | |
254 Copies any partial object from the end of the old chunk | |
255 to the beginning of the new one. */ | |
256 | |
257 void | |
4667 | 258 _obstack_newchunk (struct obstack *h, int length) |
334 | 259 { |
881 | 260 register struct _obstack_chunk *old_chunk = h->chunk; |
261 register struct _obstack_chunk *new_chunk; | |
334 | 262 register long new_size; |
2475 | 263 register long obj_size = h->next_free - h->object_base; |
264 register long i; | |
265 long already; | |
2915 | 266 char *object_base; |
334 | 267 |
268 /* Compute size for new chunk. */ | |
2915 | 269 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100; |
334 | 270 if (new_size < h->chunk_size) |
271 new_size = h->chunk_size; | |
272 | |
273 /* Allocate and initialize the new chunk. */ | |
274 new_chunk = CALL_CHUNKFUN (h, new_size); | |
275 if (!new_chunk) | |
881 | 276 (*obstack_alloc_failed_handler) (); |
334 | 277 h->chunk = new_chunk; |
278 new_chunk->prev = old_chunk; | |
279 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; | |
280 | |
2915 | 281 /* Compute an aligned object_base in the new chunk */ |
282 object_base = | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
283 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask); |
2915 | 284 |
334 | 285 /* Move the existing object to the new chunk. |
286 Word at a time is fast and is safe if the object | |
287 is sufficiently aligned. */ | |
288 if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT) | |
289 { | |
290 for (i = obj_size / sizeof (COPYING_UNIT) - 1; | |
291 i >= 0; i--) | |
2915 | 292 ((COPYING_UNIT *)object_base)[i] |
334 | 293 = ((COPYING_UNIT *)h->object_base)[i]; |
294 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, | |
295 but that can cross a page boundary on a machine | |
296 which does not do strict alignment for COPYING_UNITS. */ | |
297 already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT); | |
298 } | |
299 else | |
300 already = 0; | |
301 /* Copy remaining bytes one by one. */ | |
302 for (i = already; i < obj_size; i++) | |
2915 | 303 object_base[i] = h->object_base[i]; |
334 | 304 |
305 /* If the object just copied was the only data in OLD_CHUNK, | |
306 free that chunk and remove it from the chain. | |
307 But not if that chunk might contain an empty object. */ | |
5174
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
308 if (! h->maybe_empty_object |
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
309 && (h->object_base |
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
310 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents, |
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
311 h->alignment_mask))) |
334 | 312 { |
313 new_chunk->prev = old_chunk->prev; | |
314 CALL_FREEFUN (h, old_chunk); | |
315 } | |
316 | |
2915 | 317 h->object_base = object_base; |
334 | 318 h->next_free = h->object_base + obj_size; |
319 /* The new chunk certainly contains no empty object yet. */ | |
320 h->maybe_empty_object = 0; | |
321 } | |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
322 # ifdef _LIBC |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
323 libc_hidden_def (_obstack_newchunk) |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
324 # endif |
334 | 325 |
326 /* Return nonzero if object OBJ has been allocated from obstack H. | |
327 This is here for debugging. | |
328 If you use it in a program, you are probably losing. */ | |
329 | |
330 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in | |
331 obstack.h because it is just for debugging. */ | |
4667 | 332 int _obstack_allocated_p (struct obstack *h, void *obj); |
334 | 333 |
334 int | |
4667 | 335 _obstack_allocated_p (struct obstack *h, void *obj) |
334 | 336 { |
881 | 337 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
338 register struct _obstack_chunk *plp; /* point to previous chunk if any */ | |
334 | 339 |
340 lp = (h)->chunk; | |
341 /* We use >= rather than > since the object cannot be exactly at | |
342 the beginning of the chunk but might be an empty object exactly | |
881 | 343 at the end of an adjacent chunk. */ |
4667 | 344 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
334 | 345 { |
346 plp = lp->prev; | |
347 lp = plp; | |
348 } | |
349 return lp != 0; | |
350 } | |
351 | |
352 /* Free objects in obstack H, including OBJ and everything allocate | |
353 more recently than OBJ. If OBJ is zero, free everything in H. */ | |
354 | |
2475 | 355 # undef obstack_free |
334 | 356 |
357 void | |
4667 | 358 obstack_free (struct obstack *h, void *obj) |
334 | 359 { |
881 | 360 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
361 register struct _obstack_chunk *plp; /* point to previous chunk if any */ | |
334 | 362 |
363 lp = h->chunk; | |
364 /* We use >= because there cannot be an object at the beginning of a chunk. | |
365 But there can be an empty object at that address | |
366 at the end of another chunk. */ | |
4667 | 367 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
334 | 368 { |
369 plp = lp->prev; | |
370 CALL_FREEFUN (h, lp); | |
371 lp = plp; | |
372 /* If we switch chunks, we can't tell whether the new current | |
373 chunk contains an empty object, so assume that it may. */ | |
374 h->maybe_empty_object = 1; | |
375 } | |
376 if (lp) | |
377 { | |
881 | 378 h->object_base = h->next_free = (char *) (obj); |
334 | 379 h->chunk_limit = lp->limit; |
380 h->chunk = lp; | |
381 } | |
382 else if (obj != 0) | |
383 /* obj is not in any of the chunks! */ | |
384 abort (); | |
385 } | |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
386 |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
387 # ifdef _LIBC |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
388 /* Older versions of libc used a function _obstack_free intended to be |
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
389 called by non-GCC compilers. */ |
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
390 strong_alias (obstack_free, _obstack_free) |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
391 # endif |
334 | 392 |
881 | 393 int |
4667 | 394 _obstack_memory_used (struct obstack *h) |
881 | 395 { |
396 register struct _obstack_chunk* lp; | |
397 register int nbytes = 0; | |
398 | |
399 for (lp = h->chunk; lp != 0; lp = lp->prev) | |
400 { | |
401 nbytes += lp->limit - (char *) lp; | |
402 } | |
403 return nbytes; | |
404 } | |
405 | |
406 /* Define the error handler. */ | |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
407 # ifdef _LIBC |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
408 # include <libintl.h> |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
409 # else |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
410 # include "gettext.h" |
881 | 411 # endif |
5057
f4b3a4229349
Import obstack changes from libc.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4934
diff
changeset
|
412 # ifndef _ |
f4b3a4229349
Import obstack changes from libc.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4934
diff
changeset
|
413 # define _(msgid) gettext (msgid) |
f4b3a4229349
Import obstack changes from libc.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4934
diff
changeset
|
414 # endif |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
415 |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
416 # ifdef _LIBC |
2475 | 417 # include <libio/iolibio.h> |
418 # endif | |
881 | 419 |
3361 | 420 # ifndef __attribute__ |
3293
a3e558e29878
(__attribute__): Define it to be empty for compilers
Jim Meyering <jim@meyering.net>
parents:
3280
diff
changeset
|
421 /* This feature is available in gcc versions 2.5 and later. */ |
3361 | 422 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) |
423 # define __attribute__(Spec) /* empty */ | |
424 # endif | |
3293
a3e558e29878
(__attribute__): Define it to be empty for compilers
Jim Meyering <jim@meyering.net>
parents:
3280
diff
changeset
|
425 # endif |
a3e558e29878
(__attribute__): Define it to be empty for compilers
Jim Meyering <jim@meyering.net>
parents:
3280
diff
changeset
|
426 |
881 | 427 static void |
3280 | 428 __attribute__ ((noreturn)) |
4667 | 429 print_and_abort (void) |
881 | 430 { |
3361 | 431 /* Don't change any of these strings. Yes, it would be possible to add |
432 the newline to the string and use fputs or so. But this must not | |
433 happen because the "memory exhausted" message appears in other places | |
434 like this and the translation should be reused instead of creating | |
435 a very similar string which requires a separate translation. */ | |
436 # if defined _LIBC && defined USE_IN_LIBIO | |
437 if (_IO_fwide (stderr, 0) > 0) | |
438 __fwprintf (stderr, L"%s\n", _("memory exhausted")); | |
439 else | |
440 # endif | |
441 fprintf (stderr, "%s\n", _("memory exhausted")); | |
881 | 442 exit (obstack_exit_failure); |
443 } | |
334 | 444 |
779 | 445 #endif /* !ELIDE_CODE */ |