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