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