Mercurial > hg > octave-kai > gnulib-hg
annotate lib/obstack.c @ 17160:72f4bab621be
fts: introduce FTS_VERBATIM
This gives clients the option to disable stripping of trailing slashes
from input path names during fts_open initialization.
The recent change v0.0-7611-g3a9002d that made fts_open strip trailing
slashes from input path names had a negative impact on findutils that
relies on the old fts_open behavior to implement POSIX requirement that
each path operand of the find utility shall be evaluated unaltered as it
was provided, including all trailing slash characters.
* lib/fts_.h (FTS_VERBATIM): New bit flag.
(FTS_OPTIONMASK, FTS_NAMEONLY, FTS_STOP): Adjust.
* lib/fts.c (fts_open): Honor it.
author | Dmitry V. Levin <ldv@altlinux.org> |
---|---|
date | Sun, 18 Nov 2012 04:40:18 +0400 |
parents | 18a38c9615f0 |
children | e542fd46ad6f |
rev | line source |
---|---|
334 | 1 /* obstack.c - subroutines used implicitly by object stack macros |
4667 | 2 |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
16077
diff
changeset
|
3 Copyright (C) 1988-1994, 1996-2006, 2009-2012 Free Software Foundation, Inc. |
4667 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7550
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
4020 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7550
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7550
diff
changeset
|
8 (at your option) any later version. |
334 | 9 |
4020 | 10 This program is distributed in the hope that it will be useful, |
881 | 11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4020 | 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 GNU General Public License for more details. | |
334 | 14 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7550
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7550
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
334 | 17 |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
18 #ifdef _LIBC |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
19 # include <obstack.h> |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
20 # 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
|
21 #else |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7162
diff
changeset
|
22 # include <config.h> |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
23 # 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
|
24 #endif |
334 | 25 |
779 | 26 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be |
27 incremented whenever callers compiled using an old obstack.h can no | |
28 longer properly call the functions in this obstack.c. */ | |
29 #define OBSTACK_INTERFACE_VERSION 1 | |
334 | 30 |
31 /* Comment out all this code if we are using the GNU C Library, and are not | |
779 | 32 actually compiling the library itself, and the installed library |
33 supports the same library interface we do. This code is part of the GNU | |
34 C Library, but also included in many other GNU distributions. Compiling | |
334 | 35 and linking in this code is a waste when using the GNU C library |
36 (especially if it is a shared library). Rather than having every GNU | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
37 program understand 'configure --with-gnu-libc' and omit the object |
779 | 38 files, it is simpler to just do this in the source for each such file. */ |
334 | 39 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
40 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */ |
3362 | 41 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 |
2475 | 42 # include <gnu-versions.h> |
43 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION | |
44 # define ELIDE_CODE | |
45 # endif | |
779 | 46 #endif |
47 | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
48 #include <stddef.h> |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
49 |
779 | 50 #ifndef ELIDE_CODE |
334 | 51 |
7162
19c2e5121b2f
Add and change modules to make it easier for coreutils to use
Paul Eggert <eggert@cs.ucla.edu>
parents:
6376
diff
changeset
|
52 # include <stdint.h> |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
53 |
334 | 54 /* Determine default alignment. */ |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
55 union fooround |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
56 { |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
57 uintmax_t i; |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
58 long double d; |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
59 void *p; |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
60 }; |
5587
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
61 struct fooalign |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
62 { |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
63 char c; |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
64 union fooround u; |
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
65 }; |
334 | 66 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. |
67 But in fact it might be less smart and round addresses to as much as | |
68 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
|
69 enum |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
70 { |
5587
5cef5f4311a1
Merge changes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5178
diff
changeset
|
71 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u), |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
72 DEFAULT_ROUNDING = sizeof (union fooround) |
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
73 }; |
334 | 74 |
75 /* When we copy a long block of data, this is the unit to do it with. | |
76 On some machines, copying successive ints does not work; | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
77 in such a case, redefine COPYING_UNIT to 'long' (if that works) |
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
78 or 'char' as a last resort. */ |
2475 | 79 # ifndef COPYING_UNIT |
80 # define COPYING_UNIT int | |
81 # endif | |
334 | 82 |
881 | 83 |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
84 /* The functions allocating more room by calling 'obstack_chunk_alloc' |
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
85 jump to the handler pointed to by 'obstack_alloc_failed_handler'. |
2475 | 86 This can be set to a user defined function which should either |
87 abort gracefully or use longjump - but shouldn't return. This | |
88 variable by default points to the internal function | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
89 'print_and_abort'. */ |
16077
5168b3ab1d02
obstack: Fix compilation error on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
15400
diff
changeset
|
90 static _Noreturn void print_and_abort (void); |
881 | 91 void (*obstack_alloc_failed_handler) (void) = print_and_abort; |
92 | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
93 /* Exit value used when 'print_and_abort' is used. */ |
4667 | 94 # include <stdlib.h> |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
95 # ifdef _LIBC |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
96 int obstack_exit_failure = EXIT_FAILURE; |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
97 # else |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
98 # include "exitfail.h" |
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
99 # define obstack_exit_failure exit_failure |
2475 | 100 # endif |
881 | 101 |
5071
d525afe1916c
* malloc/obstack.c (_obstack) [defined _LIBC]: Bring back this var.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5070
diff
changeset
|
102 # ifdef _LIBC |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
103 # 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
|
104 /* 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
|
105 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
|
106 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
|
107 struct obstack *_obstack_compat; |
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
108 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
|
109 # endif |
5071
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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
142 int size, int alignment, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
143 void *(*chunkfun) (long), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
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. | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
154 Use the values for range checking, because if range checking is off, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
155 the extra bytes won't be missed terribly, but if range checking is on |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
156 and we used a larger request, a whole extra 4096 bytes would be |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
157 allocated. |
334 | 158 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
159 These number are irrelevant to the new GNU malloc. I suspect it is |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
160 less sensitive to the size of the request. */ |
334 | 161 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
162 + 4 + DEFAULT_ROUNDING - 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
163 & ~(DEFAULT_ROUNDING - 1)); |
334 | 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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
189 void *(*chunkfun) (void *, long), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
190 void (*freefun) (void *, void *), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
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. | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
201 Use the values for range checking, because if range checking is off, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
202 the extra bytes won't be missed terribly, but if range checking is on |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
203 and we used a larger request, a whole extra 4096 bytes would be |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
204 allocated. |
334 | 205 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
206 These number are irrelevant to the new GNU malloc. I suspect it is |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
207 less sensitive to the size of the request. */ |
334 | 208 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
209 + 4 + DEFAULT_ROUNDING - 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
210 & ~(DEFAULT_ROUNDING - 1)); |
334 | 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, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
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; | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
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; | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
275 i >= 0; i--) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
276 ((COPYING_UNIT *)object_base)[i] |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
277 = ((COPYING_UNIT *)h->object_base)[i]; |
334 | 278 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
279 but that can cross a page boundary on a machine |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
280 which does not do strict alignment for COPYING_UNITS. */ |
334 | 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 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
294 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
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 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
321 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
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 | |
7550
0d3d27f21643
Make it possible to rename obstack_free.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
342 __obstack_free (struct obstack *h, void *obj) |
334 | 343 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
344 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
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 | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
357 chunk contains an empty object, so assume that it may. */ |
334 | 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 |
15400 | 404 static _Noreturn void |
4667 | 405 print_and_abort (void) |
881 | 406 { |
3361 | 407 /* Don't change any of these strings. Yes, it would be possible to add |
408 the newline to the string and use fputs or so. But this must not | |
409 happen because the "memory exhausted" message appears in other places | |
410 like this and the translation should be reused instead of creating | |
411 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
|
412 # 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
|
413 (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
|
414 # else |
56829cd68e67
* config/srclist.txt: Add glibc bug 321 for obstack.c, obstack.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
415 fprintf (stderr, "%s\n", _("memory exhausted")); |
3361 | 416 # endif |
881 | 417 exit (obstack_exit_failure); |
418 } | |
334 | 419 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
420 #endif /* !ELIDE_CODE */ |