Commit 48442c8f 48442c8f89b9de078fe91ac9d0a7495bcb7882f1 by Sergey Poznyakoff

Updated by gnulib-sync

1 parent 91cfb588
1 /* Declaration for error-reporting function 1 /* Declaration for error-reporting function
2 Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc. 2 Copyright (C) 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
3 4
4 5 This program is free software; you can redistribute it and/or modify
5 NOTE: The canonical source of this file is maintained with the GNU C Library. 6 it under the terms of the GNU General Public License as published by
6 Bugs can be reported to bug-glibc@prep.ai.mit.edu. 7 the Free Software Foundation; either version 2, or (at your option)
7 8 any later version.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12 9
13 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 13 GNU General Public License for more details.
17 14
18 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License along
19 along with this program; if not, write to the Free Software 16 with this program; if not, write to the Free Software Foundation,
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 USA. */
22 18
23 #ifndef _ERROR_H 19 #ifndef _ERROR_H
24 #define _ERROR_H 1 20 #define _ERROR_H 1
25 21
26 #ifndef __attribute__ 22 #ifndef __attribute__
27 /* This feature is available in gcc versions 2.5 and later. */ 23 /* This feature is available in gcc versions 2.5 and later. */
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ 24 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
29 # define __attribute__(Spec) /* empty */ 25 # define __attribute__(Spec) /* empty */
30 # endif 26 # endif
31 /* The __-protected variants of `format' and `printf' attributes 27 /* The __-protected variants of `format' and `printf' attributes
...@@ -40,17 +36,15 @@ ...@@ -40,17 +36,15 @@
40 extern "C" { 36 extern "C" {
41 #endif 37 #endif
42 38
43 #if defined (__STDC__) && __STDC__
44
45 /* Print a message with `fprintf (stderr, FORMAT, ...)'; 39 /* Print a message with `fprintf (stderr, FORMAT, ...)';
46 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM). 40 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
47 If STATUS is nonzero, terminate the program with `exit (STATUS)'. */ 41 If STATUS is nonzero, terminate the program with `exit (STATUS)'. */
48 42
49 extern void error (int status, int errnum, const char *format, ...) 43 extern void error (int __status, int __errnum, const char *__format, ...)
50 __attribute__ ((__format__ (__printf__, 3, 4))); 44 __attribute__ ((__format__ (__printf__, 3, 4)));
51 45
52 extern void error_at_line (int status, int errnum, const char *fname, 46 extern void error_at_line (int __status, int __errnum, const char *__fname,
53 unsigned int lineno, const char *format, ...) 47 unsigned int __lineno, const char *__format, ...)
54 __attribute__ ((__format__ (__printf__, 5, 6))); 48 __attribute__ ((__format__ (__printf__, 5, 6)));
55 49
56 /* If NULL, error will flush stdout, then print on stderr the program 50 /* If NULL, error will flush stdout, then print on stderr the program
...@@ -58,12 +52,6 @@ extern void error_at_line (int status, int errnum, const char *fname, ...@@ -58,12 +52,6 @@ extern void error_at_line (int status, int errnum, const char *fname,
58 function without parameters instead. */ 52 function without parameters instead. */
59 extern void (*error_print_progname) (void); 53 extern void (*error_print_progname) (void);
60 54
61 #else
62 void error ();
63 void error_at_line ();
64 extern void (*error_print_progname) ();
65 #endif
66
67 /* This variable is incremented each time `error' is called. */ 55 /* This variable is incremented each time `error' is called. */
68 extern unsigned int error_message_count; 56 extern unsigned int error_message_count;
69 57
......
1 /* Copyright 1991, 1992, 1993, 1996, 1997, 2000 Free Software Foundation, Inc. 1 /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2004
2 Free Software Foundation, Inc.
2 3
3 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -23,208 +24,363 @@ ...@@ -23,208 +24,363 @@
23 # define _GNU_SOURCE 1 24 # define _GNU_SOURCE 1
24 #endif 25 #endif
25 26
26 #include <errno.h> 27 #if ! defined __builtin_expect && __GNUC__ < 3
28 # define __builtin_expect(expr, expected) (expr)
29 #endif
30
27 #include <fnmatch.h> 31 #include <fnmatch.h>
28 #include <ctype.h>
29 32
30 #if defined STDC_HEADERS || !defined isascii 33 #include <alloca.h>
31 # define IN_CTYPE_DOMAIN(c) 1 34 #include <assert.h>
32 #else 35 #include <ctype.h>
33 # define IN_CTYPE_DOMAIN(c) isascii (c) 36 #include <errno.h>
37 #include <stddef.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define WIDE_CHAR_SUPPORT \
43 (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC \
44 && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
45
46 /* For platform which support the ISO C amendement 1 functionality we
47 support user defined character classes. */
48 #if defined _LIBC || WIDE_CHAR_SUPPORT
49 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
50 # include <wchar.h>
51 # include <wctype.h>
34 #endif 52 #endif
35 53
36 #define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c)) 54 /* We need some of the locale data (the collation sequence information)
37 55 but there is no interface to get this information in general. Therefore
56 we support a correct implementation only in glibc. */
57 #ifdef _LIBC
58 # include "../locale/localeinfo.h"
59 # include "../locale/elem-hash.h"
60 # include "../locale/coll-lookup.h"
61 # include <shlib-compat.h>
62
63 # define CONCAT(a,b) __CONCAT(a,b)
64 # define mbsrtowcs __mbsrtowcs
65 # define fnmatch __fnmatch
66 extern int fnmatch (const char *pattern, const char *string, int flags);
67 #endif
38 68
39 #ifndef errno 69 #ifndef SIZE_MAX
40 extern int errno; 70 # define SIZE_MAX ((size_t) -1)
41 #endif 71 #endif
42 72
43 /* Match STRING against the filename pattern PATTERN, returning zero if 73 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
44 it matches, nonzero if not. */ 74 #define NO_LEADING_PERIOD(flags) \
45 int 75 ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
46 fnmatch (const char *pattern, const char *string, int flags) 76
47 { 77 /* Comment out all this code if we are using the GNU C Library, and are not
48 register const char *p = pattern, *n = string; 78 actually compiling the library itself, and have not detected a bug
49 register char c; 79 in the library. This code is part of the GNU C
80 Library, but also included in many other GNU distributions. Compiling
81 and linking in this code is a waste when using the GNU C library
82 (especially if it is a shared library). Rather than having every GNU
83 program understand `configure --with-gnu-libc' and omit the object files,
84 it is simpler to just do this in the source for each such file. */
85
86 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
87
88
89 # if defined STDC_HEADERS || !defined isascii
90 # define ISASCII(c) 1
91 # else
92 # define ISASCII(c) isascii(c)
93 # endif
94
95 # ifdef isblank
96 # define ISBLANK(c) (ISASCII (c) && isblank (c))
97 # else
98 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
99 # endif
100 # ifdef isgraph
101 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
102 # else
103 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
104 # endif
105
106 # define ISPRINT(c) (ISASCII (c) && isprint (c))
107 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
108 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
109 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
110 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
111 # define ISLOWER(c) (ISASCII (c) && islower (c))
112 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
113 # define ISSPACE(c) (ISASCII (c) && isspace (c))
114 # define ISUPPER(c) (ISASCII (c) && isupper (c))
115 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
116
117 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
118
119 # if defined _LIBC || WIDE_CHAR_SUPPORT
120 /* The GNU C library provides support for user-defined character classes
121 and the functions from ISO C amendement 1. */
122 # ifdef CHARCLASS_NAME_MAX
123 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
124 # else
125 /* This shouldn't happen but some implementation might still have this
126 problem. Use a reasonable default value. */
127 # define CHAR_CLASS_MAX_LENGTH 256
128 # endif
129
130 # ifdef _LIBC
131 # define IS_CHAR_CLASS(string) __wctype (string)
132 # else
133 # define IS_CHAR_CLASS(string) wctype (string)
134 # endif
135
136 # ifdef _LIBC
137 # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
138 # else
139 # define ISWCTYPE(WC, WT) iswctype (WC, WT)
140 # endif
141
142 # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
143 /* In this case we are implementing the multibyte character handling. */
144 # define HANDLE_MULTIBYTE 1
145 # endif
146
147 # else
148 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
149
150 # define IS_CHAR_CLASS(string) \
151 (STREQ (string, "alpha") || STREQ (string, "upper") \
152 || STREQ (string, "lower") || STREQ (string, "digit") \
153 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
154 || STREQ (string, "space") || STREQ (string, "print") \
155 || STREQ (string, "punct") || STREQ (string, "graph") \
156 || STREQ (string, "cntrl") || STREQ (string, "blank"))
157 # endif
158
159 /* Avoid depending on library functions or files
160 whose names are inconsistent. */
161
162 /* Global variable. */
163 static int posixly_correct;
164
165 # ifndef internal_function
166 /* Inside GNU libc we mark some function in a special way. In other
167 environments simply ignore the marking. */
168 # define internal_function
169 # endif
50 170
51 /* Note that this evaluates C many times. */ 171 /* Note that this evaluates C many times. */
52 #define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER ((unsigned char) (c)) \ 172 # ifdef _LIBC
53 ? tolower ((unsigned char) (c)) \ 173 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
54 : (c)) 174 # else
175 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
176 # endif
177 # define CHAR char
178 # define UCHAR unsigned char
179 # define INT int
180 # define FCT internal_fnmatch
181 # define EXT ext_match
182 # define END end_pattern
183 # define L(CS) CS
184 # ifdef _LIBC
185 # define BTOWC(C) __btowc (C)
186 # else
187 # define BTOWC(C) btowc (C)
188 # endif
189 # define STRLEN(S) strlen (S)
190 # define STRCAT(D, S) strcat (D, S)
191 # ifdef _LIBC
192 # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
193 # else
194 # if HAVE_MEMPCPY
195 # define MEMPCPY(D, S, N) mempcpy (D, S, N)
196 # else
197 # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
198 # endif
199 # endif
200 # define MEMCHR(S, C, N) memchr (S, C, N)
201 # define STRCOLL(S1, S2) strcoll (S1, S2)
202 # include "fnmatch_loop.c"
203
204
205 # if HANDLE_MULTIBYTE
206 # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
207 # define CHAR wchar_t
208 # define UCHAR wint_t
209 # define INT wint_t
210 # define FCT internal_fnwmatch
211 # define EXT ext_wmatch
212 # define END end_wpattern
213 # define L(CS) L##CS
214 # define BTOWC(C) (C)
215 # ifdef _LIBC
216 # define STRLEN(S) __wcslen (S)
217 # define STRCAT(D, S) __wcscat (D, S)
218 # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
219 # else
220 # define STRLEN(S) wcslen (S)
221 # define STRCAT(D, S) wcscat (D, S)
222 # if HAVE_WMEMPCPY
223 # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
224 # else
225 # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
226 # endif
227 # endif
228 # define MEMCHR(S, C, N) wmemchr (S, C, N)
229 # define STRCOLL(S1, S2) wcscoll (S1, S2)
230 # define WIDE_CHAR_VERSION 1
231
232 # undef IS_CHAR_CLASS
233 /* We have to convert the wide character string in a multibyte string. But
234 we know that the character class names consist of alphanumeric characters
235 from the portable character set, and since the wide character encoding
236 for a member of the portable character set is the same code point as
237 its single-byte encoding, we can use a simplified method to convert the
238 string to a multibyte character string. */
239 static wctype_t
240 is_char_class (const wchar_t *wcs)
241 {
242 char s[CHAR_CLASS_MAX_LENGTH + 1];
243 char *cp = s;
55 244
56 while ((c = *p++) != '\0') 245 do
57 { 246 {
58 c = FOLD (c); 247 /* Test for a printable character from the portable character set. */
59 248 # ifdef _LIBC
60 switch (c) 249 if (*wcs < 0x20 || *wcs > 0x7e
250 || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
251 return (wctype_t) 0;
252 # else
253 switch (*wcs)
61 { 254 {
62 case '?': 255 case L' ': case L'!': case L'"': case L'#': case L'%':
63 if (*n == '\0') 256 case L'&': case L'\'': case L'(': case L')': case L'*':
64 return FNM_NOMATCH; 257 case L'+': case L',': case L'-': case L'.': case L'/':
65 else if ((flags & FNM_FILE_NAME) && *n == '/') 258 case L'0': case L'1': case L'2': case L'3': case L'4':
66 return FNM_NOMATCH; 259 case L'5': case L'6': case L'7': case L'8': case L'9':
67 else if ((flags & FNM_PERIOD) && *n == '.' && 260 case L':': case L';': case L'<': case L'=': case L'>':
68 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) 261 case L'?':
69 return FNM_NOMATCH; 262 case L'A': case L'B': case L'C': case L'D': case L'E':
263 case L'F': case L'G': case L'H': case L'I': case L'J':
264 case L'K': case L'L': case L'M': case L'N': case L'O':
265 case L'P': case L'Q': case L'R': case L'S': case L'T':
266 case L'U': case L'V': case L'W': case L'X': case L'Y':
267 case L'Z':
268 case L'[': case L'\\': case L']': case L'^': case L'_':
269 case L'a': case L'b': case L'c': case L'd': case L'e':
270 case L'f': case L'g': case L'h': case L'i': case L'j':
271 case L'k': case L'l': case L'm': case L'n': case L'o':
272 case L'p': case L'q': case L'r': case L's': case L't':
273 case L'u': case L'v': case L'w': case L'x': case L'y':
274 case L'z': case L'{': case L'|': case L'}': case L'~':
70 break; 275 break;
71 276 default:
72 case '\\': 277 return (wctype_t) 0;
73 if (!(flags & FNM_NOESCAPE))
74 {
75 c = *p++;
76 if (c == '\0')
77 /* Trailing \ loses. */
78 return FNM_NOMATCH;
79 c = FOLD (c);
80 }
81 if (FOLD (*n) != c)
82 return FNM_NOMATCH;
83 break;
84
85 case '*':
86 if ((flags & FNM_PERIOD) && *n == '.' &&
87 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
88 return FNM_NOMATCH;
89
90 for (c = *p++; c == '?' || c == '*'; c = *p++)
91 {
92 if (c == '?')
93 {
94 /* A ? needs to match one character. */
95 if (*n == '\0' || (*n == '/' && (flags & FNM_FILE_NAME)))
96 /* There isn't another character; no match. */
97 return FNM_NOMATCH;
98 else
99 /* One character of the string is consumed in matching
100 this ? wildcard, so *??? won't match if there are
101 less than three characters. */
102 ++n;
103 }
104 } 278 }
279 # endif
105 280
106 if (c == '\0') 281 /* Avoid overrunning the buffer. */
107 { 282 if (cp == s + CHAR_CLASS_MAX_LENGTH)
108 if ((flags & (FNM_FILE_NAME | FNM_LEADING_DIR)) == FNM_FILE_NAME) 283 return (wctype_t) 0;
109 for (; *n != '\0'; n++)
110 if (*n == '/')
111 return FNM_NOMATCH;
112 return 0;
113 }
114 284
115 { 285 *cp++ = (char) *wcs++;
116 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
117 c1 = FOLD (c1);
118 for (--p; *n != '\0'; ++n)
119 if ((c == '[' || FOLD (*n) == c1) &&
120 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
121 return 0;
122 else if (*n == '/' && (flags & FNM_FILE_NAME))
123 break;
124 return FNM_NOMATCH;
125 } 286 }
287 while (*wcs != L'\0');
126 288
127 case '[': 289 *cp = '\0';
128 {
129 /* Nonzero if the sense of the character class is inverted. */
130 register int not;
131 290
132 if (*n == '\0') 291 # ifdef _LIBC
133 return FNM_NOMATCH; 292 return __wctype (s);
134 293 # else
135 if ((flags & FNM_PERIOD) && *n == '.' && 294 return wctype (s);
136 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) 295 # endif
137 return FNM_NOMATCH; 296 }
297 # define IS_CHAR_CLASS(string) is_char_class (string)
138 298
139 not = (*p == '!' || *p == '^'); 299 # include "fnmatch_loop.c"
140 if (not) 300 # endif
141 ++p;
142 301
143 c = *p++;
144 for (;;)
145 {
146 register char cstart = c, cend = c;
147 302
148 if (!(flags & FNM_NOESCAPE) && c == '\\') 303 int
304 fnmatch (const char *pattern, const char *string, int flags)
305 {
306 # if HANDLE_MULTIBYTE
307 # define ALLOCA_LIMIT 2000
308 if (__builtin_expect (MB_CUR_MAX, 1) != 1)
149 { 309 {
150 if (*p == '\0') 310 mbstate_t ps;
151 return FNM_NOMATCH; 311 size_t patsize;
152 cstart = cend = *p++; 312 size_t strsize;
153 } 313 size_t totsize;
154 314 wchar_t *wpattern;
155 cstart = cend = FOLD (cstart); 315 wchar_t *wstring;
156 316 int res;
157 if (c == '\0') 317
158 /* [ (unterminated) loses. */ 318 /* Calculate the size needed to convert the strings to
159 return FNM_NOMATCH; 319 wide characters. */
160 320 memset (&ps, '\0', sizeof (ps));
161 c = *p++; 321 patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
162 c = FOLD (c); 322 if (__builtin_expect (patsize == 0, 0))
163 323 /* Something wrong.
164 if ((flags & FNM_FILE_NAME) && c == '/') 324 XXX Do we have to set `errno' to something which mbsrtows hasn't
165 /* [/] can never match. */ 325 already done? */
166 return FNM_NOMATCH; 326 return -1;
167 327 assert (mbsinit (&ps));
168 if (c == '-' && *p != ']') 328 strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
329 if (__builtin_expect (strsize == 0, 0))
330 /* Something wrong.
331 XXX Do we have to set `errno' to something which mbsrtows hasn't
332 already done? */
333 return -1;
334 assert (mbsinit (&ps));
335 totsize = patsize + strsize;
336 if (__builtin_expect (! (patsize <= totsize
337 && totsize <= SIZE_MAX / sizeof (wchar_t)),
338 0))
169 { 339 {
170 cend = *p++; 340 errno = ENOMEM;
171 if (!(flags & FNM_NOESCAPE) && cend == '\\') 341 return -1;
172 cend = *p++;
173 if (cend == '\0')
174 return FNM_NOMATCH;
175 cend = FOLD (cend);
176
177 c = *p++;
178 }
179
180 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
181 goto matched;
182
183 if (c == ']')
184 break;
185 } 342 }
186 if (!not)
187 return FNM_NOMATCH;
188 break;
189 343
190 matched:; 344 /* Allocate room for the wide characters. */
191 /* Skip the rest of the [...] that already matched. */ 345 if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
192 while (c != ']') 346 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
347 else
193 { 348 {
194 if (c == '\0') 349 wpattern = malloc (totsize * sizeof (wchar_t));
195 /* [... (unterminated) loses. */ 350 if (__builtin_expect (! wpattern, 0))
196 return FNM_NOMATCH;
197
198 c = *p++;
199 if (!(flags & FNM_NOESCAPE) && c == '\\')
200 { 351 {
201 if (*p == '\0') 352 errno = ENOMEM;
202 return FNM_NOMATCH; 353 return -1;
203 /* XXX 1003.2d11 is unclear if this is right. */
204 ++p;
205 }
206 }
207 if (not)
208 return FNM_NOMATCH;
209 } 354 }
210 break;
211
212 default:
213 if (c != FOLD (*n))
214 return FNM_NOMATCH;
215 } 355 }
356 wstring = wpattern + patsize;
216 357
217 ++n; 358 /* Convert the strings into wide characters. */
218 } 359 mbsrtowcs (wpattern, &pattern, patsize, &ps);
360 assert (mbsinit (&ps));
361 mbsrtowcs (wstring, &string, strsize, &ps);
219 362
220 if (*n == '\0') 363 res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
221 return 0; 364 flags & FNM_PERIOD, flags);
222 365
223 if ((flags & FNM_LEADING_DIR) && *n == '/') 366 if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
224 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ 367 free (wpattern);
225 return 0; 368 return res;
226 369 }
227 return FNM_NOMATCH; 370 # endif /* HANDLE_MULTIBYTE */
228 371
229 #undef FOLD 372 return internal_fnmatch (pattern, string, string + strlen (string),
373 flags & FNM_PERIOD, flags);
230 } 374 }
375
376 # ifdef _LIBC
377 # undef fnmatch
378 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
379 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
380 strong_alias (__fnmatch, __fnmatch_old)
381 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
382 # endif
383 libc_hidden_ver (__fnmatch, fnmatch)
384 # endif
385
386 #endif /* _LIBC or not __GNU_LIBRARY__. */
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* Copyright (C) 1992-2001, 2003, 2004 Free Software Foundation, Inc.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. 2 This file is part of the GNU C Library.
3 3
4 This library is free software; you can redistribute it and/or 4 This program is free software; you can redistribute it and/or modify
5 modify it under the terms of the GNU Lesser General Public 5 it under the terms of the GNU General Public License as published by
6 License as published by the Free Software Foundation; either 6 the Free Software Foundation; either version 2, or (at your option)
7 version 2 of the License, or (at your option) any later version. 7 any later version.
8 8
9 This library is distributed in the hope that it will be useful, 9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 Lesser General Public License for more details. 12 GNU General Public License for more details.
13 13
14 You should have received a copy of the GNU Lesser General Public 14 You should have received a copy of the GNU General Public License along
15 License along with this library; if not, write to the Free Software 15 with this program; if not, write to the Free Software Foundation,
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #if !_LIBC
23 # include "getpass.h"
24 #endif
25
26 #if _LIBC
27 # define HAVE_STDIO_EXT_H 1
28 #endif
29
30 #include <stdbool.h>
17 31
18 #include <stdio.h> 32 #include <stdio.h>
19 #include <string.h> 33 #if HAVE_STDIO_EXT_H
20 #include <stdlib.h> 34 # include <stdio_ext.h>
35 #else
36 # define __fsetlocking(stream, type) /* empty */
37 #endif
38 #if !_LIBC
39 # include "getline.h"
40 #endif
41
21 #include <termios.h> 42 #include <termios.h>
43 #include <unistd.h>
22 44
23 /* Alain: Parts originally from GNU Lib C. */ 45 #if _LIBC
46 # include <wchar.h>
47 #endif
24 48
25 static void 49 #if _LIBC
26 echo_off(int fd, struct termios *stored_settings) 50 # define NOTCANCEL_MODE "c"
27 { 51 #else
28 struct termios new_settings; 52 # define NOTCANCEL_MODE
29 tcgetattr (fd, stored_settings); 53 #endif
30 new_settings = *stored_settings; 54
31 new_settings.c_lflag &= (~ECHO); 55 #if _LIBC
32 tcsetattr (fd, TCSANOW, &new_settings); 56 # define flockfile(s) _IO_flockfile (s)
33 } 57 # define funlockfile(s) _IO_funlockfile (s)
58 #elif USE_UNLOCKED_IO
59 # include "unlocked-io.h"
60 #else
61 # if !HAVE_DECL_FFLUSH_UNLOCKED
62 # undef fflush_unlocked
63 # define fflush_unlocked(x) fflush (x)
64 # endif
65 # if !HAVE_DECL_FLOCKFILE
66 # undef flockfile
67 # define flockfile(x) ((void) 0)
68 # endif
69 # if !HAVE_DECL_FUNLOCKFILE
70 # undef funlockfile
71 # define funlockfile(x) ((void) 0)
72 # endif
73 # if !HAVE_DECL_FPUTS_UNLOCKED
74 # undef fputs_unlocked
75 # define fputs_unlocked(str,stream) fputs (str, stream)
76 # endif
77 # if !HAVE_DECL_PUTC_UNLOCKED
78 # undef putc_unlocked
79 # define putc_unlocked(c,stream) putc (c, stream)
80 # endif
81 #endif
82
83 #if _LIBC
84 # include <bits/libc-lock.h>
85 #else
86 # define __libc_cleanup_push(function, arg) /* empty */
87 # define __libc_cleanup_pop(execute) /* empty */
88 #endif
89
90 #if !_LIBC
91 # define __getline getline
92 # define __tcgetattr tcgetattr
93 #endif
94
95 /* It is desirable to use this bit on systems that have it.
96 The only bit of terminal state we want to twiddle is echoing, which is
97 done in software; there is no need to change the state of the terminal
98 hardware. */
99
100 #ifndef TCSASOFT
101 # define TCSASOFT 0
102 #endif
34 103
35 static void 104 static void
36 echo_on(int fd, struct termios *stored_settings) 105 call_fclose (void *arg)
37 { 106 {
38 tcsetattr (fd, TCSANOW, stored_settings); 107 if (arg != NULL)
108 fclose (arg);
39 } 109 }
40 110
41 char * 111 char *
42 getpass (const char * prompt) 112 getpass (const char *prompt)
43 { 113 {
114 FILE *tty;
44 FILE *in, *out; 115 FILE *in, *out;
45 struct termios stored_settings; 116 struct termios s, t;
117 bool tty_changed;
46 static char *buf; 118 static char *buf;
47 static size_t buf_size; 119 static size_t bufsize;
48 char *pbuf; 120 ssize_t nread;
121
122 /* Try to write to and read from the terminal if we can.
123 If we can't open the terminal, use stderr and stdin. */
49 124
50 /* First pass initialize the buffer. */ 125 tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
51 if (buf_size == 0) 126 if (tty == NULL)
52 { 127 {
53 buf_size = 256; 128 in = stdin;
54 buf = calloc (1, buf_size); 129 out = stderr;
55 if (buf == NULL)
56 return NULL;
57 } 130 }
58 else 131 else
59 memset (buf, '\0', buf_size); 132 {
133 /* We do the locking ourselves. */
134 __fsetlocking (tty, FSETLOCKING_BYCALLER);
135
136 out = in = tty;
137 }
138
139 /* Make sure the stream we opened is closed even if the thread is
140 canceled. */
141 __libc_cleanup_push (call_fclose, tty);
142
143 flockfile (out);
60 144
61 /* Turn echoing off if it is on now. */ 145 /* Turn echoing off if it is on now. */
62 echo_off (fileno (stdin), &stored_settings); 146
147 if (__tcgetattr (fileno (in), &t) == 0)
148 {
149 /* Save the old one. */
150 s = t;
151 /* Tricky, tricky. */
152 t.c_lflag &= ~(ECHO|ISIG);
153 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
154 }
155 else
156 tty_changed = false;
63 157
64 /* Write the prompt. */ 158 /* Write the prompt. */
65 fputs (prompt, stdout); 159 #ifdef USE_IN_LIBIO
66 fflush (stdout); 160 if (_IO_fwide (out, 0) > 0)
161 __fwprintf (out, L"%s", prompt);
162 else
163 #endif
164 fputs_unlocked (prompt, out);
165 fflush_unlocked (out);
67 166
68 /* Read the password. */ 167 /* Read the password. */
69 pbuf = fgets (buf, buf_size, stdin); 168 nread = __getline (&buf, &bufsize, in);
70 if (pbuf) 169
170 #if !_LIBC
171 /* As far as is known, glibc doesn't need this no-op fseek. */
172
173 /* According to the C standard, input may not be followed by output
174 on the same stream without an intervening call to a file
175 positioning function. Suppose in == out; then without this fseek
176 call, on Solaris, HP-UX, AIX, OSF/1, the previous input gets
177 echoed, whereas on IRIX, the following newline is not output as
178 it should be. POSIX imposes similar restrictions if fileno (in)
179 == fileno (out). The POSIX restrictions are tricky and change
180 from POSIX version to POSIX version, so play it safe and invoke
181 fseek even if in != out. */
182 fseek (out, 0, SEEK_CUR);
183 #endif
184
185 if (buf != NULL)
71 { 186 {
72 size_t nread = strlen (pbuf); 187 if (nread < 0)
73 if (nread && pbuf[nread - 1] == '\n') 188 buf[0] = '\0';
189 else if (buf[nread - 1] == '\n')
74 { 190 {
75 /* Remove the newline. */ 191 /* Remove the newline. */
76 pbuf[nread - 1] = '\0'; 192 buf[nread - 1] = '\0';
193 if (tty_changed)
194 {
77 /* Write the newline that was not echoed. */ 195 /* Write the newline that was not echoed. */
78 putc ('\n', stdout); 196 #ifdef USE_IN_LIBIO
197 if (_IO_fwide (out, 0) > 0)
198 putwc_unlocked (L'\n', out);
199 else
200 #endif
201 putc_unlocked ('\n', out);
202 }
79 } 203 }
80 } 204 }
81 205
82 /* Restore the original setting. */ 206 /* Restore the original setting. */
83 echo_on (fileno (stdin), &stored_settings); 207 if (tty_changed)
208 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
84 209
85 return pbuf; 210 funlockfile (out);
86 }
87 211
88 #ifdef _GETPASS_STANDALONE_TEST 212 __libc_cleanup_pop (0);
89 213
90 int 214 call_fclose (tty);
91 main () 215
92 { 216 return buf;
93 char *p;
94 p = getpass ("my prompt: ");
95 if (p)
96 printf ("Passwd: %s\n", p);
97 return 0;
98 } 217 }
99 #endif
......
1 /* rpl_malloc.c -- a replacement for malloc that don't accept 0 size 1 /* malloc() function that is glibc compatible.
2 Copyright (C) 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -15,12 +15,22 @@ ...@@ -15,12 +15,22 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 17
18 /* written by Jim Meyering */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #undef malloc
24
18 #include <stdlib.h> 25 #include <stdlib.h>
19 26
27 /* Allocate an N-byte block of memory from the heap.
28 If N is zero, allocate a 1-byte block. */
29
20 void * 30 void *
21 rpl_malloc(size_t size) 31 rpl_malloc (size_t n)
22 { 32 {
23 if (!size) 33 if (n == 0)
24 size++; 34 n = 1;
25 return malloc(size); 35 return malloc (n);
26 } 36 }
......
1 /* obstack.c - subroutines used implicitly by object stack macros 1 /* obstack.c - subroutines used implicitly by object stack macros
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
3 2
4 This program is free software; you can redistribute it and/or modify it 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
5 under the terms of the GNU General Public License as published by the 4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
6 Free Software Foundation; either version 2, or (at your option) any 5 Inc.
7 later version.
8 6
9 This program is distributed in the hope that it will be useful, 7 This program is free software; you can redistribute it and/or modify
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 8 it under the terms of the GNU General Public License as published by
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 the Free Software Foundation; either version 2, or (at your option)
12 GNU General Public License for more details. 10 any later version.
13 11
14 You should have received a copy of the GNU General Public License 12 This program is distributed in the hope that it will be useful,
15 along with this program; if not, write to the Free Software 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 16
18 #include "obstack.h" 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. */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #ifdef _LIBC
26 # include <obstack.h>
27 # include <shlib-compat.h>
28 #else
29 # include "obstack.h"
30 #endif
19 31
20 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be 32 /* NOTE BEFORE MODIFYING THIS FILE: This version number must be
21 incremented whenever callers compiled using an old obstack.h can no 33 incremented whenever callers compiled using an old obstack.h can no
...@@ -32,45 +44,86 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ ...@@ -32,45 +44,86 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 files, it is simpler to just do this in the source for each such file. */ 44 files, it is simpler to just do this in the source for each such file. */
33 45
34 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */ 46 #include <stdio.h> /* Random thing to get __GNU_LIBRARY__. */
35 #if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1 47 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
36 #include <gnu-versions.h> 48 # include <gnu-versions.h>
37 #if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION 49 # if _GNU_OBSTACK_INTERFACE_VERSION == OBSTACK_INTERFACE_VERSION
38 #define ELIDE_CODE 50 # define ELIDE_CODE
51 # endif
39 #endif 52 #endif
53
54 #if defined _LIBC && defined USE_IN_LIBIO
55 # include <wchar.h>
40 #endif 56 #endif
41 57
58 #include <stddef.h>
42 59
43 #ifndef ELIDE_CODE 60 #ifndef ELIDE_CODE
44 61
45 62
46 #if defined (__STDC__) && __STDC__ 63 # if HAVE_INTTYPES_H
47 #define POINTER void * 64 # include <inttypes.h>
48 #else 65 # endif
49 #define POINTER char * 66 # if HAVE_STDINT_H || defined _LIBC
50 #endif 67 # include <stdint.h>
68 # endif
51 69
52 /* Determine default alignment. */ 70 /* Determine default alignment. */
53 struct fooalign {char x; double d;}; 71 union fooround
54 #define DEFAULT_ALIGNMENT \ 72 {
55 ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0)) 73 uintmax_t i;
74 long double d;
75 void *p;
76 };
77 struct fooalign
78 {
79 char c;
80 union fooround u;
81 };
56 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. 82 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
57 But in fact it might be less smart and round addresses to as much as 83 But in fact it might be less smart and round addresses to as much as
58 DEFAULT_ROUNDING. So we prepare for it to do that. */ 84 DEFAULT_ROUNDING. So we prepare for it to do that. */
59 union fooround {long x; double d;}; 85 enum
60 #define DEFAULT_ROUNDING (sizeof (union fooround)) 86 {
87 DEFAULT_ALIGNMENT = offsetof (struct fooalign, u),
88 DEFAULT_ROUNDING = sizeof (union fooround)
89 };
61 90
62 /* When we copy a long block of data, this is the unit to do it with. 91 /* When we copy a long block of data, this is the unit to do it with.
63 On some machines, copying successive ints does not work; 92 On some machines, copying successive ints does not work;
64 in such a case, redefine COPYING_UNIT to `long' (if that works) 93 in such a case, redefine COPYING_UNIT to `long' (if that works)
65 or `char' as a last resort. */ 94 or `char' as a last resort. */
66 #ifndef COPYING_UNIT 95 # ifndef COPYING_UNIT
67 #define COPYING_UNIT int 96 # define COPYING_UNIT int
68 #endif 97 # endif
69 98
70 /* The non-GNU-C macros copy the obstack into this global variable 99
71 to avoid multiple evaluation. */ 100 /* The functions allocating more room by calling `obstack_chunk_alloc'
72 101 jump to the handler pointed to by `obstack_alloc_failed_handler'.
73 struct obstack *_obstack; 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
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. */
110 # include <stdlib.h>
111 # ifdef _LIBC
112 int obstack_exit_failure = EXIT_FAILURE;
113 # else
114 # include "exitfail.h"
115 # define obstack_exit_failure exit_failure
116 # endif
117
118 # ifdef _LIBC
119 # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
120 /* A looong time ago (before 1994, anyway; we're not sure) this global variable
121 was used by non-GNU-C macros to avoid multiple evaluation. The GNU C
122 library still exports it because somebody might use it. */
123 struct obstack *_obstack_compat;
124 compat_symbol (libc, _obstack_compat, _obstack, GLIBC_2_0);
125 # endif
126 # endif
74 127
75 /* Define a macro that either calls functions with the traditional malloc/free 128 /* Define a macro that either calls functions with the traditional malloc/free
76 calling interface, or calls functions with the mmalloc/mfree interface 129 calling interface, or calls functions with the mmalloc/mfree interface
...@@ -78,17 +131,17 @@ struct obstack *_obstack; ...@@ -78,17 +131,17 @@ struct obstack *_obstack;
78 For free, do not use ?:, since some compilers, like the MIPS compilers, 131 For free, do not use ?:, since some compilers, like the MIPS compilers,
79 do not allow (expr) ? void : void. */ 132 do not allow (expr) ? void : void. */
80 133
81 #define CALL_CHUNKFUN(h, size) \ 134 # define CALL_CHUNKFUN(h, size) \
82 (((h) -> use_extra_arg) \ 135 (((h) -> use_extra_arg) \
83 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \ 136 ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
84 : (*(h)->chunkfun) ((size))) 137 : (*(struct _obstack_chunk *(*) (long)) (h)->chunkfun) ((size)))
85 138
86 #define CALL_FREEFUN(h, old_chunk) \ 139 # define CALL_FREEFUN(h, old_chunk) \
87 do { \ 140 do { \
88 if ((h) -> use_extra_arg) \ 141 if ((h) -> use_extra_arg) \
89 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \ 142 (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
90 else \ 143 else \
91 (*(h)->freefun) ((old_chunk)); \ 144 (*(void (*) (void *)) (h)->freefun) ((old_chunk)); \
92 } while (0) 145 } while (0)
93 146
94 147
...@@ -97,19 +150,16 @@ struct obstack *_obstack; ...@@ -97,19 +150,16 @@ struct obstack *_obstack;
97 CHUNKFUN is the function to use to allocate chunks, 150 CHUNKFUN is the function to use to allocate chunks,
98 and FREEFUN the function to free them. 151 and FREEFUN the function to free them.
99 152
100 Return nonzero if successful, zero if out of memory. 153 Return nonzero if successful, calls obstack_alloc_failed_handler if
101 To recover from an out of memory error, 154 allocation fails. */
102 free up some memory, then call this again. */
103 155
104 int 156 int
105 _obstack_begin (h, size, alignment, chunkfun, freefun) 157 _obstack_begin (struct obstack *h,
106 struct obstack *h; 158 int size, int alignment,
107 int size; 159 void *(*chunkfun) (long),
108 int alignment; 160 void (*freefun) (void *))
109 POINTER (*chunkfun) ();
110 void (*freefun) ();
111 { 161 {
112 register struct _obstack_chunk* chunk; /* points to new chunk */ 162 register struct _obstack_chunk *chunk; /* points to new chunk */
113 163
114 if (alignment == 0) 164 if (alignment == 0)
115 alignment = DEFAULT_ALIGNMENT; 165 alignment = DEFAULT_ALIGNMENT;
...@@ -130,38 +180,33 @@ _obstack_begin (h, size, alignment, chunkfun, freefun) ...@@ -130,38 +180,33 @@ _obstack_begin (h, size, alignment, chunkfun, freefun)
130 size = 4096 - extra; 180 size = 4096 - extra;
131 } 181 }
132 182
133 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun; 183 h->chunkfun = (struct _obstack_chunk * (*)(void *, long)) chunkfun;
134 h->freefun = freefun; 184 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
135 h->chunk_size = size; 185 h->chunk_size = size;
136 h->alignment_mask = alignment - 1; 186 h->alignment_mask = alignment - 1;
137 h->use_extra_arg = 0; 187 h->use_extra_arg = 0;
138 188
139 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 189 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
140 if (!chunk) 190 if (!chunk)
141 { 191 (*obstack_alloc_failed_handler) ();
142 h->alloc_failed = 1; 192 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
143 return 0; 193 alignment - 1);
144 }
145 h->alloc_failed = 0;
146 h->next_free = h->object_base = chunk->contents;
147 h->chunk_limit = chunk->limit 194 h->chunk_limit = chunk->limit
148 = (char *) chunk + h->chunk_size; 195 = (char *) chunk + h->chunk_size;
149 chunk->prev = 0; 196 chunk->prev = 0;
150 /* The initial chunk now contains no empty object. */ 197 /* The initial chunk now contains no empty object. */
151 h->maybe_empty_object = 0; 198 h->maybe_empty_object = 0;
199 h->alloc_failed = 0;
152 return 1; 200 return 1;
153 } 201 }
154 202
155 int 203 int
156 _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg) 204 _obstack_begin_1 (struct obstack *h, int size, int alignment,
157 struct obstack *h; 205 void *(*chunkfun) (void *, long),
158 int size; 206 void (*freefun) (void *, void *),
159 int alignment; 207 void *arg)
160 POINTER (*chunkfun) ();
161 void (*freefun) ();
162 POINTER arg;
163 { 208 {
164 register struct _obstack_chunk* chunk; /* points to new chunk */ 209 register struct _obstack_chunk *chunk; /* points to new chunk */
165 210
166 if (alignment == 0) 211 if (alignment == 0)
167 alignment = DEFAULT_ALIGNMENT; 212 alignment = DEFAULT_ALIGNMENT;
...@@ -182,8 +227,8 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg) ...@@ -182,8 +227,8 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
182 size = 4096 - extra; 227 size = 4096 - extra;
183 } 228 }
184 229
185 h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun; 230 h->chunkfun = (struct _obstack_chunk * (*)(void *,long)) chunkfun;
186 h->freefun = freefun; 231 h->freefun = (void (*) (void *, struct _obstack_chunk *)) freefun;
187 h->chunk_size = size; 232 h->chunk_size = size;
188 h->alignment_mask = alignment - 1; 233 h->alignment_mask = alignment - 1;
189 h->extra_arg = arg; 234 h->extra_arg = arg;
...@@ -191,17 +236,15 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg) ...@@ -191,17 +236,15 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
191 236
192 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size); 237 chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
193 if (!chunk) 238 if (!chunk)
194 { 239 (*obstack_alloc_failed_handler) ();
195 h->alloc_failed = 1; 240 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
196 return 0; 241 alignment - 1);
197 }
198 h->alloc_failed = 0;
199 h->next_free = h->object_base = chunk->contents;
200 h->chunk_limit = chunk->limit 242 h->chunk_limit = chunk->limit
201 = (char *) chunk + h->chunk_size; 243 = (char *) chunk + h->chunk_size;
202 chunk->prev = 0; 244 chunk->prev = 0;
203 /* The initial chunk now contains no empty object. */ 245 /* The initial chunk now contains no empty object. */
204 h->maybe_empty_object = 0; 246 h->maybe_empty_object = 0;
247 h->alloc_failed = 0;
205 return 1; 248 return 1;
206 } 249 }
207 250
...@@ -212,34 +255,33 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg) ...@@ -212,34 +255,33 @@ _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
212 to the beginning of the new one. */ 255 to the beginning of the new one. */
213 256
214 void 257 void
215 _obstack_newchunk (h, length) 258 _obstack_newchunk (struct obstack *h, int length)
216 struct obstack *h;
217 int length;
218 { 259 {
219 register struct _obstack_chunk* old_chunk = h->chunk; 260 register struct _obstack_chunk *old_chunk = h->chunk;
220 register struct _obstack_chunk* new_chunk; 261 register struct _obstack_chunk *new_chunk;
221 register long new_size; 262 register long new_size;
222 register int obj_size = h->next_free - h->object_base; 263 register long obj_size = h->next_free - h->object_base;
223 register int i; 264 register long i;
224 int already; 265 long already;
266 char *object_base;
225 267
226 /* Compute size for new chunk. */ 268 /* Compute size for new chunk. */
227 new_size = (obj_size + length) + (obj_size >> 3) + 100; 269 new_size = (obj_size + length) + (obj_size >> 3) + h->alignment_mask + 100;
228 if (new_size < h->chunk_size) 270 if (new_size < h->chunk_size)
229 new_size = h->chunk_size; 271 new_size = h->chunk_size;
230 272
231 /* Allocate and initialize the new chunk. */ 273 /* Allocate and initialize the new chunk. */
232 new_chunk = CALL_CHUNKFUN (h, new_size); 274 new_chunk = CALL_CHUNKFUN (h, new_size);
233 if (!new_chunk) 275 if (!new_chunk)
234 { 276 (*obstack_alloc_failed_handler) ();
235 h->alloc_failed = 1;
236 return;
237 }
238 h->alloc_failed = 0;
239 h->chunk = new_chunk; 277 h->chunk = new_chunk;
240 new_chunk->prev = old_chunk; 278 new_chunk->prev = old_chunk;
241 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; 279 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
242 280
281 /* Compute an aligned object_base in the new chunk */
282 object_base =
283 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
284
243 /* Move the existing object to the new chunk. 285 /* Move the existing object to the new chunk.
244 Word at a time is fast and is safe if the object 286 Word at a time is fast and is safe if the object
245 is sufficiently aligned. */ 287 is sufficiently aligned. */
...@@ -247,7 +289,7 @@ _obstack_newchunk (h, length) ...@@ -247,7 +289,7 @@ _obstack_newchunk (h, length)
247 { 289 {
248 for (i = obj_size / sizeof (COPYING_UNIT) - 1; 290 for (i = obj_size / sizeof (COPYING_UNIT) - 1;
249 i >= 0; i--) 291 i >= 0; i--)
250 ((COPYING_UNIT *)new_chunk->contents)[i] 292 ((COPYING_UNIT *)object_base)[i]
251 = ((COPYING_UNIT *)h->object_base)[i]; 293 = ((COPYING_UNIT *)h->object_base)[i];
252 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT, 294 /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
253 but that can cross a page boundary on a machine 295 but that can cross a page boundary on a machine
...@@ -258,46 +300,48 @@ _obstack_newchunk (h, length) ...@@ -258,46 +300,48 @@ _obstack_newchunk (h, length)
258 already = 0; 300 already = 0;
259 /* Copy remaining bytes one by one. */ 301 /* Copy remaining bytes one by one. */
260 for (i = already; i < obj_size; i++) 302 for (i = already; i < obj_size; i++)
261 new_chunk->contents[i] = h->object_base[i]; 303 object_base[i] = h->object_base[i];
262 304
263 /* If the object just copied was the only data in OLD_CHUNK, 305 /* If the object just copied was the only data in OLD_CHUNK,
264 free that chunk and remove it from the chain. 306 free that chunk and remove it from the chain.
265 But not if that chunk might contain an empty object. */ 307 But not if that chunk might contain an empty object. */
266 if (h->object_base == old_chunk->contents && ! h->maybe_empty_object) 308 if (! h->maybe_empty_object
309 && (h->object_base
310 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
311 h->alignment_mask)))
267 { 312 {
268 new_chunk->prev = old_chunk->prev; 313 new_chunk->prev = old_chunk->prev;
269 CALL_FREEFUN (h, old_chunk); 314 CALL_FREEFUN (h, old_chunk);
270 } 315 }
271 316
272 h->object_base = new_chunk->contents; 317 h->object_base = object_base;
273 h->next_free = h->object_base + obj_size; 318 h->next_free = h->object_base + obj_size;
274 /* The new chunk certainly contains no empty object yet. */ 319 /* The new chunk certainly contains no empty object yet. */
275 h->maybe_empty_object = 0; 320 h->maybe_empty_object = 0;
276 } 321 }
322 # ifdef _LIBC
323 libc_hidden_def (_obstack_newchunk)
324 # endif
277 325
278 /* Return nonzero if object OBJ has been allocated from obstack H. 326 /* Return nonzero if object OBJ has been allocated from obstack H.
279 This is here for debugging. 327 This is here for debugging.
280 If you use it in a program, you are probably losing. */ 328 If you use it in a program, you are probably losing. */
281 329
282 #if defined (__STDC__) && __STDC__
283 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in 330 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
284 obstack.h because it is just for debugging. */ 331 obstack.h because it is just for debugging. */
285 int _obstack_allocated_p (struct obstack *h, POINTER obj); 332 int _obstack_allocated_p (struct obstack *h, void *obj);
286 #endif
287 333
288 int 334 int
289 _obstack_allocated_p (h, obj) 335 _obstack_allocated_p (struct obstack *h, void *obj)
290 struct obstack *h;
291 POINTER obj;
292 { 336 {
293 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */ 337 register struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
294 register struct _obstack_chunk* plp; /* point to previous chunk if any */ 338 register struct _obstack_chunk *plp; /* point to previous chunk if any */
295 339
296 lp = (h)->chunk; 340 lp = (h)->chunk;
297 /* We use >= rather than > since the object cannot be exactly at 341 /* We use >= rather than > since the object cannot be exactly at
298 the beginning of the chunk but might be an empty object exactly 342 the beginning of the chunk but might be an empty object exactly
299 at the end of an adjacent chunk. */ 343 at the end of an adjacent chunk. */
300 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj)) 344 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
301 { 345 {
302 plp = lp->prev; 346 plp = lp->prev;
303 lp = plp; 347 lp = plp;
...@@ -308,24 +352,19 @@ _obstack_allocated_p (h, obj) ...@@ -308,24 +352,19 @@ _obstack_allocated_p (h, obj)
308 /* Free objects in obstack H, including OBJ and everything allocate 352 /* Free objects in obstack H, including OBJ and everything allocate
309 more recently than OBJ. If OBJ is zero, free everything in H. */ 353 more recently than OBJ. If OBJ is zero, free everything in H. */
310 354
311 #undef obstack_free 355 # undef obstack_free
312
313 /* This function has two names with identical definitions.
314 This is the first one, called from non-ANSI code. */
315 356
316 void 357 void
317 _obstack_free (h, obj) 358 obstack_free (struct obstack *h, void *obj)
318 struct obstack *h;
319 POINTER obj;
320 { 359 {
321 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */ 360 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 */ 361 register struct _obstack_chunk *plp; /* point to previous chunk if any */
323 362
324 lp = h->chunk; 363 lp = h->chunk;
325 /* We use >= because there cannot be an object at the beginning of a chunk. 364 /* We use >= because there cannot be an object at the beginning of a chunk.
326 But there can be an empty object at that address 365 But there can be an empty object at that address
327 at the end of another chunk. */ 366 at the end of another chunk. */
328 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj)) 367 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
329 { 368 {
330 plp = lp->prev; 369 plp = lp->prev;
331 CALL_FREEFUN (h, lp); 370 CALL_FREEFUN (h, lp);
...@@ -336,7 +375,7 @@ _obstack_free (h, obj) ...@@ -336,7 +375,7 @@ _obstack_free (h, obj)
336 } 375 }
337 if (lp) 376 if (lp)
338 { 377 {
339 h->object_base = h->next_free = (char *)(obj); 378 h->object_base = h->next_free = (char *) (obj);
340 h->chunk_limit = lp->limit; 379 h->chunk_limit = lp->limit;
341 h->chunk = lp; 380 h->chunk = lp;
342 } 381 }
...@@ -345,153 +384,62 @@ _obstack_free (h, obj) ...@@ -345,153 +384,62 @@ _obstack_free (h, obj)
345 abort (); 384 abort ();
346 } 385 }
347 386
348 /* This function is used from ANSI code. */ 387 # ifdef _LIBC
388 /* Older versions of libc used a function _obstack_free intended to be
389 called by non-GCC compilers. */
390 strong_alias (obstack_free, _obstack_free)
391 # endif
349 392
350 void 393 int
351 obstack_free (h, obj) 394 _obstack_memory_used (struct obstack *h)
352 struct obstack *h;
353 POINTER obj;
354 { 395 {
355 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */ 396 register struct _obstack_chunk* lp;
356 register struct _obstack_chunk* plp; /* point to previous chunk if any */ 397 register int nbytes = 0;
357 398
358 lp = h->chunk; 399 for (lp = h->chunk; lp != 0; lp = lp->prev)
359 /* We use >= because there cannot be an object at the beginning of a chunk.
360 But there can be an empty object at that address
361 at the end of another chunk. */
362 while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
363 { 400 {
364 plp = lp->prev; 401 nbytes += lp->limit - (char *) lp;
365 CALL_FREEFUN (h, lp);
366 lp = plp;
367 /* If we switch chunks, we can't tell whether the new current
368 chunk contains an empty object, so assume that it may. */
369 h->maybe_empty_object = 1;
370 } 402 }
371 if (lp) 403 return nbytes;
372 {
373 h->object_base = h->next_free = (char *)(obj);
374 h->chunk_limit = lp->limit;
375 h->chunk = lp;
376 }
377 else if (obj != 0)
378 /* obj is not in any of the chunks! */
379 abort ();
380 }
381
382 #if 0
383 /* These are now turned off because the applications do not use it
384 and it uses bcopy via obstack_grow, which causes trouble on sysV. */
385
386 /* Now define the functional versions of the obstack macros.
387 Define them to simply use the corresponding macros to do the job. */
388
389 #if defined (__STDC__) && __STDC__
390 /* These function definitions do not work with non-ANSI preprocessors;
391 they won't pass through the macro names in parentheses. */
392
393 /* The function names appear in parentheses in order to prevent
394 the macro-definitions of the names from being expanded there. */
395
396 POINTER (obstack_base) (obstack)
397 struct obstack *obstack;
398 {
399 return obstack_base (obstack);
400 }
401
402 POINTER (obstack_next_free) (obstack)
403 struct obstack *obstack;
404 {
405 return obstack_next_free (obstack);
406 }
407
408 int (obstack_object_size) (obstack)
409 struct obstack *obstack;
410 {
411 return obstack_object_size (obstack);
412 }
413
414 int (obstack_room) (obstack)
415 struct obstack *obstack;
416 {
417 return obstack_room (obstack);
418 }
419
420 void (obstack_grow) (obstack, pointer, length)
421 struct obstack *obstack;
422 POINTER pointer;
423 int length;
424 {
425 obstack_grow (obstack, pointer, length);
426 }
427
428 void (obstack_grow0) (obstack, pointer, length)
429 struct obstack *obstack;
430 POINTER pointer;
431 int length;
432 {
433 obstack_grow0 (obstack, pointer, length);
434 } 404 }
435 405
436 void (obstack_1grow) (obstack, character) 406 /* Define the error handler. */
437 struct obstack *obstack; 407 # ifdef _LIBC
438 int character; 408 # include <libintl.h>
409 # else
410 # include "gettext.h"
411 # endif
412 # ifndef _
413 # define _(msgid) gettext (msgid)
414 # endif
415
416 # ifdef _LIBC
417 # include <libio/iolibio.h>
418 # endif
419
420 # ifndef __attribute__
421 /* This feature is available in gcc versions 2.5 and later. */
422 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
423 # define __attribute__(Spec) /* empty */
424 # endif
425 # endif
426
427 static void
428 __attribute__ ((noreturn))
429 print_and_abort (void)
439 { 430 {
440 obstack_1grow (obstack, character); 431 /* Don't change any of these strings. Yes, it would be possible to add
441 } 432 the newline to the string and use fputs or so. But this must not
442 433 happen because the "memory exhausted" message appears in other places
443 void (obstack_blank) (obstack, length) 434 like this and the translation should be reused instead of creating
444 struct obstack *obstack; 435 a very similar string which requires a separate translation. */
445 int length; 436 # if defined _LIBC && defined USE_IN_LIBIO
446 { 437 if (_IO_fwide (stderr, 0) > 0)
447 obstack_blank (obstack, length); 438 __fwprintf (stderr, L"%s\n", _("memory exhausted"));
448 } 439 else
449 440 # endif
450 void (obstack_1grow_fast) (obstack, character) 441 fprintf (stderr, "%s\n", _("memory exhausted"));
451 struct obstack *obstack; 442 exit (obstack_exit_failure);
452 int character;
453 {
454 obstack_1grow_fast (obstack, character);
455 }
456
457 void (obstack_blank_fast) (obstack, length)
458 struct obstack *obstack;
459 int length;
460 {
461 obstack_blank_fast (obstack, length);
462 }
463
464 POINTER (obstack_finish) (obstack)
465 struct obstack *obstack;
466 {
467 return obstack_finish (obstack);
468 }
469
470 POINTER (obstack_alloc) (obstack, length)
471 struct obstack *obstack;
472 int length;
473 {
474 return obstack_alloc (obstack, length);
475 }
476
477 POINTER (obstack_copy) (obstack, pointer, length)
478 struct obstack *obstack;
479 POINTER pointer;
480 int length;
481 {
482 return obstack_copy (obstack, pointer, length);
483 }
484
485 POINTER (obstack_copy0) (obstack, pointer, length)
486 struct obstack *obstack;
487 POINTER pointer;
488 int length;
489 {
490 return obstack_copy0 (obstack, pointer, length);
491 } 443 }
492 444
493 #endif /* __STDC__ */
494
495 #endif /* 0 */
496
497 #endif /* !ELIDE_CODE */ 445 #endif /* !ELIDE_CODE */
......
1 /* obstack.h - object stack macros 1 /* obstack.h - object stack macros
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc. 2 Copyright (C) 1988-1994,1996-1999,2003,2004 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify it 4 This file is part of the GNU C Library. Its master source is NOT part of
5 under the terms of the GNU Library General Public License as published by the 5 the C library, however. The master source lives in /gd/gnu/lib.
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8 6
9 This program is distributed in the hope that it will be useful, 7 NOTE: The canonical source of this file is maintained with the GNU C Library.
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 8 Bugs can be reported to bug-glibc@gnu.org.
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13 9
14 You should have received a copy of the GNU Library General Public License 10 This program is free software; you can redistribute it and/or modify
15 along with this program; if not, write to the Free Software 11 it under the terms of the GNU General Public License as published by
16 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 23
18 /* Summary: 24 /* Summary:
19 25
...@@ -100,43 +106,43 @@ Summary: ...@@ -100,43 +106,43 @@ Summary:
100 106
101 /* Don't do the contents of this file more than once. */ 107 /* Don't do the contents of this file more than once. */
102 108
103 #ifndef __OBSTACK_H__ 109 #ifndef _OBSTACK_H
104 #define __OBSTACK_H__ 110 #define _OBSTACK_H 1
105 111
106 /* We use subtraction of (char *)0 instead of casting to int 112 #ifdef __cplusplus
107 because on word-addressable machines a simple cast to int 113 extern "C" {
108 may ignore the byte-within-word field of the pointer. */
109
110 #ifndef __PTR_TO_INT
111 #define __PTR_TO_INT(P) ((P) - (char *)0)
112 #endif 114 #endif
113 115
114 #ifndef __INT_TO_PTR 116 /* We need the type of a pointer subtraction. If __PTRDIFF_TYPE__ is
115 #define __INT_TO_PTR(P) ((P) + (char *)0) 117 defined, as with GNU C, use that; that way we don't pollute the
116 #endif 118 namespace with <stddef.h>'s symbols. Otherwise, include <stddef.h>
119 and use ptrdiff_t. */
117 120
118 /* We need the type of the resulting object. In ANSI C it is ptrdiff_t 121 #ifdef __PTRDIFF_TYPE__
119 but in traditional C it is usually long. If we are in ANSI C and 122 # define PTR_INT_TYPE __PTRDIFF_TYPE__
120 don't already have ptrdiff_t get it. */ 123 #else
121 124 # include <stddef.h>
122 #if defined (__STDC__) && ! defined (offsetof) 125 # define PTR_INT_TYPE ptrdiff_t
123 #if defined (__GNUC__) && defined (IN_GCC)
124 /* On Next machine, the system's stddef.h screws up if included
125 after we have defined just ptrdiff_t, so include all of stddef.h.
126 Otherwise, define just ptrdiff_t, which is all we need. */
127 #ifndef __NeXT__
128 #define __need_ptrdiff_t
129 #endif
130 #endif 126 #endif
131 127
132 #include <stddef.h> 128 /* If B is the base of an object addressed by P, return the result of
133 #endif 129 aligning P to the next multiple of A + 1. B and P must be of type
130 char *. A + 1 must be a power of 2. */
134 131
135 #ifdef __STDC__ 132 #define __BPTR_ALIGN(B, P, A) ((B) + (((P) - (B) + (A)) & ~(A)))
136 #define PTR_INT_TYPE ptrdiff_t 133
137 #else 134 /* Similiar to _BPTR_ALIGN (B, P, A), except optimize the common case
138 #define PTR_INT_TYPE long 135 where pointers can be converted to integers, aligned as integers,
139 #endif 136 and converted back again. If PTR_INT_TYPE is narrower than a
137 pointer (e.g., the AS/400), play it safe and compute the alignment
138 relative to B. Otherwise, use the faster strategy of computing the
139 alignment relative to 0. */
140
141 #define __PTR_ALIGN(B, P, A) \
142 __BPTR_ALIGN (sizeof (PTR_INT_TYPE) < sizeof (void *) ? (B) : (char *) 0, \
143 P, A)
144
145 #include <string.h>
140 146
141 struct _obstack_chunk /* Lives at front of each chunk. */ 147 struct _obstack_chunk /* Lives at front of each chunk. */
142 { 148 {
...@@ -148,90 +154,59 @@ struct _obstack_chunk /* Lives at front of each chunk. */ ...@@ -148,90 +154,59 @@ struct _obstack_chunk /* Lives at front of each chunk. */
148 struct obstack /* control current object in current chunk */ 154 struct obstack /* control current object in current chunk */
149 { 155 {
150 long chunk_size; /* preferred size to allocate chunks in */ 156 long chunk_size; /* preferred size to allocate chunks in */
151 struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */ 157 struct _obstack_chunk *chunk; /* address of current struct obstack_chunk */
152 char *object_base; /* address of object we are building */ 158 char *object_base; /* address of object we are building */
153 char *next_free; /* where to add next char to current object */ 159 char *next_free; /* where to add next char to current object */
154 char *chunk_limit; /* address of char after current chunk */ 160 char *chunk_limit; /* address of char after current chunk */
155 PTR_INT_TYPE temp; /* Temporary for some macros. */ 161 union
162 {
163 PTR_INT_TYPE tempint;
164 void *tempptr;
165 } temp; /* Temporary for some macros. */
156 int alignment_mask; /* Mask of alignment for each object. */ 166 int alignment_mask; /* Mask of alignment for each object. */
157 struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk. */ 167 /* These prototypes vary based on `use_extra_arg', and we use
158 void (*freefun) (); /* User's function to free a chunk. */ 168 casts to the prototypeless function type in all assignments,
159 char *extra_arg; /* first arg for chunk alloc/dealloc funcs */ 169 but having prototypes here quiets -Wstrict-prototypes. */
170 struct _obstack_chunk *(*chunkfun) (void *, long);
171 void (*freefun) (void *, struct _obstack_chunk *);
172 void *extra_arg; /* first arg for chunk alloc/dealloc funcs */
160 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */ 173 unsigned use_extra_arg:1; /* chunk alloc/dealloc funcs take extra arg */
161 unsigned maybe_empty_object:1;/* There is a possibility that the current 174 unsigned maybe_empty_object:1;/* There is a possibility that the current
162 chunk contains a zero-length object. This 175 chunk contains a zero-length object. This
163 prevents freeing the chunk if we allocate 176 prevents freeing the chunk if we allocate
164 a bigger chunk to replace it. */ 177 a bigger chunk to replace it. */
165 unsigned alloc_failed:1; /* chunk alloc func returned 0 */ 178 unsigned alloc_failed:1; /* No longer used, as we now call the failed
179 handler on error, but retained for binary
180 compatibility. */
166 }; 181 };
167 182
168 /* Declare the external functions we use; they are in obstack.c. */ 183 /* Declare the external functions we use; they are in obstack.c. */
169 184
170 #ifdef __STDC__
171 extern void _obstack_newchunk (struct obstack *, int); 185 extern void _obstack_newchunk (struct obstack *, int);
172 extern void _obstack_free (struct obstack *, void *);
173 extern int _obstack_begin (struct obstack *, int, int, 186 extern int _obstack_begin (struct obstack *, int, int,
174 void *(*) (), void (*) ()); 187 void *(*) (long), void (*) (void *));
175 extern int _obstack_begin_1 (struct obstack *, int, int, 188 extern int _obstack_begin_1 (struct obstack *, int, int,
176 void *(*) (), void (*) (), void *); 189 void *(*) (void *, long),
190 void (*) (void *, void *), void *);
177 extern int _obstack_memory_used (struct obstack *); 191 extern int _obstack_memory_used (struct obstack *);
178 #else
179 extern void _obstack_newchunk ();
180 extern void _obstack_free ();
181 extern int _obstack_begin ();
182 extern int _obstack_begin_1 ();
183 extern int _obstack_memory_used ();
184 #endif
185
186 #ifdef __STDC__
187
188 /* Do the function-declarations after the structs
189 but before defining the macros. */
190
191 void obstack_init (struct obstack *obstack);
192
193 void * obstack_alloc (struct obstack *obstack, int size);
194
195 void * obstack_copy (struct obstack *obstack, void *address, int size);
196 void * obstack_copy0 (struct obstack *obstack, void *address, int size);
197 192
198 void obstack_free (struct obstack *obstack, void *block); 193 void obstack_free (struct obstack *obstack, void *block);
199 194
200 void obstack_blank (struct obstack *obstack, int size);
201
202 void obstack_grow (struct obstack *obstack, void *data, int size);
203 void obstack_grow0 (struct obstack *obstack, void *data, int size);
204
205 void obstack_1grow (struct obstack *obstack, int data_char);
206 void obstack_ptr_grow (struct obstack *obstack, void *data);
207 void obstack_int_grow (struct obstack *obstack, int data);
208
209 void * obstack_finish (struct obstack *obstack);
210
211 int obstack_object_size (struct obstack *obstack);
212
213 int obstack_room (struct obstack *obstack);
214 void obstack_1grow_fast (struct obstack *obstack, int data_char);
215 void obstack_ptr_grow_fast (struct obstack *obstack, void *data);
216 void obstack_int_grow_fast (struct obstack *obstack, int data);
217 void obstack_blank_fast (struct obstack *obstack, int size);
218
219 void * obstack_base (struct obstack *obstack);
220 void * obstack_next_free (struct obstack *obstack);
221 int obstack_alignment_mask (struct obstack *obstack);
222 int obstack_chunk_size (struct obstack *obstack);
223 int obstack_memory_used (struct obstack *obstack);
224 195
225 #endif /* __STDC__ */ 196 /* Error handler called when `obstack_chunk_alloc' failed to allocate
197 more memory. This can be set to a user defined function which
198 should either abort gracefully or use longjump - but shouldn't
199 return. The default action is to print a message and abort. */
200 extern void (*obstack_alloc_failed_handler) (void);
226 201
227 /* Non-ANSI C cannot really support alternative functions for these macros, 202 /* Exit value used when `print_and_abort' is used. */
228 so we do not declare them. */ 203 extern int obstack_exit_failure;
229 204
230 /* Pointer to beginning of object being allocated or to be allocated next. 205 /* Pointer to beginning of object being allocated or to be allocated next.
231 Note that this might not be the final address of the object 206 Note that this might not be the final address of the object
232 because a new chunk might be needed to hold the final size. */ 207 because a new chunk might be needed to hold the final size. */
233 208
234 #define obstack_base(h) ((h)->alloc_failed ? 0 : (h)->object_base) 209 #define obstack_base(h) ((void *) (h)->object_base)
235 210
236 /* Size for allocating ordinary chunks. */ 211 /* Size for allocating ordinary chunks. */
237 212
...@@ -239,145 +214,167 @@ int obstack_memory_used (struct obstack *obstack); ...@@ -239,145 +214,167 @@ int obstack_memory_used (struct obstack *obstack);
239 214
240 /* Pointer to next byte not yet allocated in current chunk. */ 215 /* Pointer to next byte not yet allocated in current chunk. */
241 216
242 #define obstack_next_free(h) ((h)->alloc_failed ? 0 : (h)->next_free) 217 #define obstack_next_free(h) ((h)->next_free)
243 218
244 /* Mask specifying low bits that should be clear in address of an object. */ 219 /* Mask specifying low bits that should be clear in address of an object. */
245 220
246 #define obstack_alignment_mask(h) ((h)->alignment_mask) 221 #define obstack_alignment_mask(h) ((h)->alignment_mask)
247 222
223 /* To prevent prototype warnings provide complete argument list. */
248 #define obstack_init(h) \ 224 #define obstack_init(h) \
249 _obstack_begin ((h), 0, 0, \ 225 _obstack_begin ((h), 0, 0, \
250 (void *(*) ()) obstack_chunk_alloc, (void (*) ()) obstack_chunk_free) 226 (void *(*) (long)) obstack_chunk_alloc, \
227 (void (*) (void *)) obstack_chunk_free)
251 228
252 #define obstack_begin(h, size) \ 229 #define obstack_begin(h, size) \
253 _obstack_begin ((h), (size), 0, \ 230 _obstack_begin ((h), (size), 0, \
254 (void *(*) ()) obstack_chunk_alloc, (void (*) ()) obstack_chunk_free) 231 (void *(*) (long)) obstack_chunk_alloc, \
232 (void (*) (void *)) obstack_chunk_free)
255 233
256 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \ 234 #define obstack_specify_allocation(h, size, alignment, chunkfun, freefun) \
257 _obstack_begin ((h), (size), (alignment), \ 235 _obstack_begin ((h), (size), (alignment), \
258 (void *(*) ()) (chunkfun), (void (*) ()) (freefun)) 236 (void *(*) (long)) (chunkfun), \
237 (void (*) (void *)) (freefun))
259 238
260 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \ 239 #define obstack_specify_allocation_with_arg(h, size, alignment, chunkfun, freefun, arg) \
261 _obstack_begin_1 ((h), (size), (alignment), \ 240 _obstack_begin_1 ((h), (size), (alignment), \
262 (void *(*) ()) (chunkfun), (void (*) ()) (freefun), (arg)) 241 (void *(*) (void *, long)) (chunkfun), \
242 (void (*) (void *, void *)) (freefun), (arg))
263 243
264 #define obstack_chunkfun(h, newchunkfun) \ 244 #define obstack_chunkfun(h, newchunkfun) \
265 ((h) -> chunkfun = (struct _obstack_chunk *(*)()) (newchunkfun)) 245 ((h) -> chunkfun = (struct _obstack_chunk *(*)(void *, long)) (newchunkfun))
266 246
267 #define obstack_freefun(h, newfreefun) \ 247 #define obstack_freefun(h, newfreefun) \
268 ((h) -> freefun = (void (*)()) (newfreefun)) 248 ((h) -> freefun = (void (*)(void *, struct _obstack_chunk *)) (newfreefun))
269 249
270 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar) 250 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = (achar))
271 251
272 #define obstack_blank_fast(h,n) ((h)->next_free += (n)) 252 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
273 253
274 #define obstack_memory_used(h) _obstack_memory_used (h) 254 #define obstack_memory_used(h) _obstack_memory_used (h)
275 255
276 #if defined (__GNUC__) && defined (__STDC__) 256 #if defined __GNUC__ && defined __STDC__ && __STDC__
277 #if __GNUC__ < 2 257 /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
278 #define __extension__ 258 does not implement __extension__. But that compiler doesn't define
279 #endif 259 __GNUC_MINOR__. */
260 # if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
261 # define __extension__
262 # endif
280 263
281 /* For GNU C, if not -traditional, 264 /* For GNU C, if not -traditional,
282 we can define these macros to compute all args only once 265 we can define these macros to compute all args only once
283 without using a global variable. 266 without using a global variable.
284 Also, we can avoid using the `temp' slot, to make faster code. */ 267 Also, we can avoid using the `temp' slot, to make faster code. */
285 268
286 #define obstack_object_size(OBSTACK) \ 269 # define obstack_object_size(OBSTACK) \
287 __extension__ \ 270 __extension__ \
288 ({ struct obstack *__o = (OBSTACK); \ 271 ({ struct obstack const *__o = (OBSTACK); \
289 __o->alloc_failed ? 0 : \
290 (unsigned) (__o->next_free - __o->object_base); }) 272 (unsigned) (__o->next_free - __o->object_base); })
291 273
292 #define obstack_room(OBSTACK) \ 274 # define obstack_room(OBSTACK) \
293 __extension__ \ 275 __extension__ \
294 ({ struct obstack *__o = (OBSTACK); \ 276 ({ struct obstack const *__o = (OBSTACK); \
295 (unsigned) (__o->chunk_limit - __o->next_free); }) 277 (unsigned) (__o->chunk_limit - __o->next_free); })
296 278
297 #define obstack_grow(OBSTACK,where,length) \ 279 # define obstack_make_room(OBSTACK,length) \
280 __extension__ \
281 ({ struct obstack *__o = (OBSTACK); \
282 int __len = (length); \
283 if (__o->chunk_limit - __o->next_free < __len) \
284 _obstack_newchunk (__o, __len); \
285 (void) 0; })
286
287 # define obstack_empty_p(OBSTACK) \
288 __extension__ \
289 ({ struct obstack const *__o = (OBSTACK); \
290 (__o->chunk->prev == 0 \
291 && __o->next_free == __PTR_ALIGN ((char *) __o->chunk, \
292 __o->chunk->contents, \
293 __o->alignment_mask)); })
294
295 # define obstack_grow(OBSTACK,where,length) \
298 __extension__ \ 296 __extension__ \
299 ({ struct obstack *__o = (OBSTACK); \ 297 ({ struct obstack *__o = (OBSTACK); \
300 int __len = (length); \ 298 int __len = (length); \
301 if (__o->next_free + __len > __o->chunk_limit) \ 299 if (__o->next_free + __len > __o->chunk_limit) \
302 _obstack_newchunk (__o, __len); \ 300 _obstack_newchunk (__o, __len); \
303 if (!__o->alloc_failed) \ 301 memcpy (__o->next_free, where, __len); \
304 { \
305 bcopy (where, __o->next_free, __len); \
306 __o->next_free += __len; \ 302 __o->next_free += __len; \
307 } \
308 (void) 0; }) 303 (void) 0; })
309 304
310 #define obstack_grow0(OBSTACK,where,length) \ 305 # define obstack_grow0(OBSTACK,where,length) \
311 __extension__ \ 306 __extension__ \
312 ({ struct obstack *__o = (OBSTACK); \ 307 ({ struct obstack *__o = (OBSTACK); \
313 int __len = (length); \ 308 int __len = (length); \
314 if (__o->next_free + __len + 1 > __o->chunk_limit) \ 309 if (__o->next_free + __len + 1 > __o->chunk_limit) \
315 _obstack_newchunk (__o, __len + 1); \ 310 _obstack_newchunk (__o, __len + 1); \
316 if (!__o->alloc_failed) \ 311 memcpy (__o->next_free, where, __len); \
317 { \
318 bcopy (where, __o->next_free, __len); \
319 __o->next_free += __len; \ 312 __o->next_free += __len; \
320 *(__o->next_free)++ = 0; \ 313 *(__o->next_free)++ = 0; \
321 } \
322 (void) 0; }) 314 (void) 0; })
323 315
324 #define obstack_1grow(OBSTACK,datum) \ 316 # define obstack_1grow(OBSTACK,datum) \
325 __extension__ \ 317 __extension__ \
326 ({ struct obstack *__o = (OBSTACK); \ 318 ({ struct obstack *__o = (OBSTACK); \
327 if (__o->next_free + 1 > __o->chunk_limit) \ 319 if (__o->next_free + 1 > __o->chunk_limit) \
328 _obstack_newchunk (__o, 1); \ 320 _obstack_newchunk (__o, 1); \
329 if (!__o->alloc_failed) \ 321 obstack_1grow_fast (__o, datum); \
330 *(__o->next_free)++ = (datum); \
331 (void) 0; }) 322 (void) 0; })
332 323
333 /* These assume that the obstack alignment is good enough for pointers or ints, 324 /* These assume that the obstack alignment is good enough for pointers
334 and that the data added so far to the current object 325 or ints, and that the data added so far to the current object
335 shares that much alignment. */ 326 shares that much alignment. */
336 327
337 #define obstack_ptr_grow(OBSTACK,datum) \ 328 # define obstack_ptr_grow(OBSTACK,datum) \
338 __extension__ \ 329 __extension__ \
339 ({ struct obstack *__o = (OBSTACK); \ 330 ({ struct obstack *__o = (OBSTACK); \
340 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \ 331 if (__o->next_free + sizeof (void *) > __o->chunk_limit) \
341 _obstack_newchunk (__o, sizeof (void *)); \ 332 _obstack_newchunk (__o, sizeof (void *)); \
342 if (!__o->alloc_failed) \ 333 obstack_ptr_grow_fast (__o, datum); }) \
343 *((void **)__o->next_free)++ = ((void *)datum); \
344 (void) 0; })
345 334
346 #define obstack_int_grow(OBSTACK,datum) \ 335 # define obstack_int_grow(OBSTACK,datum) \
347 __extension__ \ 336 __extension__ \
348 ({ struct obstack *__o = (OBSTACK); \ 337 ({ struct obstack *__o = (OBSTACK); \
349 if (__o->next_free + sizeof (int) > __o->chunk_limit) \ 338 if (__o->next_free + sizeof (int) > __o->chunk_limit) \
350 _obstack_newchunk (__o, sizeof (int)); \ 339 _obstack_newchunk (__o, sizeof (int)); \
351 if (!__o->alloc_failed) \ 340 obstack_int_grow_fast (__o, datum); })
352 *((int *)__o->next_free)++ = ((int)datum); \ 341
342 # define obstack_ptr_grow_fast(OBSTACK,aptr) \
343 __extension__ \
344 ({ struct obstack *__o1 = (OBSTACK); \
345 *(const void **) __o1->next_free = (aptr); \
346 __o1->next_free += sizeof (const void *); \
353 (void) 0; }) 347 (void) 0; })
354 348
355 #define obstack_ptr_grow_fast(h,aptr) (*((void **)(h)->next_free)++ = (void *)aptr) 349 # define obstack_int_grow_fast(OBSTACK,aint) \
356 #define obstack_int_grow_fast(h,aint) (*((int *)(h)->next_free)++ = (int)aint) 350 __extension__ \
351 ({ struct obstack *__o1 = (OBSTACK); \
352 *(int *) __o1->next_free = (aint); \
353 __o1->next_free += sizeof (int); \
354 (void) 0; })
357 355
358 #define obstack_blank(OBSTACK,length) \ 356 # define obstack_blank(OBSTACK,length) \
359 __extension__ \ 357 __extension__ \
360 ({ struct obstack *__o = (OBSTACK); \ 358 ({ struct obstack *__o = (OBSTACK); \
361 int __len = (length); \ 359 int __len = (length); \
362 if (__o->chunk_limit - __o->next_free < __len) \ 360 if (__o->chunk_limit - __o->next_free < __len) \
363 _obstack_newchunk (__o, __len); \ 361 _obstack_newchunk (__o, __len); \
364 if (!__o->alloc_failed) \ 362 obstack_blank_fast (__o, __len); \
365 __o->next_free += __len; \
366 (void) 0; }) 363 (void) 0; })
367 364
368 #define obstack_alloc(OBSTACK,length) \ 365 # define obstack_alloc(OBSTACK,length) \
369 __extension__ \ 366 __extension__ \
370 ({ struct obstack *__h = (OBSTACK); \ 367 ({ struct obstack *__h = (OBSTACK); \
371 obstack_blank (__h, (length)); \ 368 obstack_blank (__h, (length)); \
372 obstack_finish (__h); }) 369 obstack_finish (__h); })
373 370
374 #define obstack_copy(OBSTACK,where,length) \ 371 # define obstack_copy(OBSTACK,where,length) \
375 __extension__ \ 372 __extension__ \
376 ({ struct obstack *__h = (OBSTACK); \ 373 ({ struct obstack *__h = (OBSTACK); \
377 obstack_grow (__h, (where), (length)); \ 374 obstack_grow (__h, (where), (length)); \
378 obstack_finish (__h); }) 375 obstack_finish (__h); })
379 376
380 #define obstack_copy0(OBSTACK,where,length) \ 377 # define obstack_copy0(OBSTACK,where,length) \
381 __extension__ \ 378 __extension__ \
382 ({ struct obstack *__h = (OBSTACK); \ 379 ({ struct obstack *__h = (OBSTACK); \
383 obstack_grow0 (__h, (where), (length)); \ 380 obstack_grow0 (__h, (where), (length)); \
...@@ -385,134 +382,131 @@ __extension__ \ ...@@ -385,134 +382,131 @@ __extension__ \
385 382
386 /* The local variable is named __o1 to avoid a name conflict 383 /* The local variable is named __o1 to avoid a name conflict
387 when obstack_blank is called. */ 384 when obstack_blank is called. */
388 #define obstack_finish(OBSTACK) \ 385 # define obstack_finish(OBSTACK) \
389 __extension__ \ 386 __extension__ \
390 ({ struct obstack *__o1 = (OBSTACK); \ 387 ({ struct obstack *__o1 = (OBSTACK); \
391 void *value; \ 388 void *__value = (void *) __o1->object_base; \
392 if (__o1->alloc_failed) \ 389 if (__o1->next_free == __value) \
393 value = 0; \
394 else \
395 { \
396 value = (void *) __o1->object_base; \
397 if (__o1->next_free == value) \
398 __o1->maybe_empty_object = 1; \ 390 __o1->maybe_empty_object = 1; \
399 __o1->next_free \ 391 __o1->next_free \
400 = __INT_TO_PTR ((__PTR_TO_INT (__o1->next_free)+__o1->alignment_mask)\ 392 = __PTR_ALIGN (__o1->object_base, __o1->next_free, \
401 & ~ (__o1->alignment_mask)); \ 393 __o1->alignment_mask); \
402 if (__o1->next_free - (char *)__o1->chunk \ 394 if (__o1->next_free - (char *)__o1->chunk \
403 > __o1->chunk_limit - (char *)__o1->chunk) \ 395 > __o1->chunk_limit - (char *)__o1->chunk) \
404 __o1->next_free = __o1->chunk_limit; \ 396 __o1->next_free = __o1->chunk_limit; \
405 __o1->object_base = __o1->next_free; \ 397 __o1->object_base = __o1->next_free; \
406 } \ 398 __value; })
407 value; })
408 399
409 #define obstack_free(OBSTACK, OBJ) \ 400 # define obstack_free(OBSTACK, OBJ) \
410 __extension__ \ 401 __extension__ \
411 ({ struct obstack *__o = (OBSTACK); \ 402 ({ struct obstack *__o = (OBSTACK); \
412 void *__obj = (OBJ); \ 403 void *__obj = (OBJ); \
413 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \ 404 if (__obj > (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
414 __o->next_free = __o->object_base = __obj; \ 405 __o->next_free = __o->object_base = (char *)__obj; \
415 else (obstack_free) (__o, __obj); }) 406 else (obstack_free) (__o, __obj); })
416 407
417 #else /* not __GNUC__ or not __STDC__ */ 408 #else /* not __GNUC__ or not __STDC__ */
418 409
419 #define obstack_object_size(h) \ 410 # define obstack_object_size(h) \
420 (unsigned) ((h)->alloc_failed ? 0 : (h)->next_free - (h)->object_base) 411 (unsigned) ((h)->next_free - (h)->object_base)
421 412
422 #define obstack_room(h) \ 413 # define obstack_room(h) \
423 (unsigned) ((h)->chunk_limit - (h)->next_free) 414 (unsigned) ((h)->chunk_limit - (h)->next_free)
424 415
416 # define obstack_empty_p(h) \
417 ((h)->chunk->prev == 0 \
418 && (h)->next_free == __PTR_ALIGN ((char *) (h)->chunk, \
419 (h)->chunk->contents, \
420 (h)->alignment_mask))
421
425 /* Note that the call to _obstack_newchunk is enclosed in (..., 0) 422 /* Note that the call to _obstack_newchunk is enclosed in (..., 0)
426 so that we can avoid having void expressions 423 so that we can avoid having void expressions
427 in the arms of the conditional expression. 424 in the arms of the conditional expression.
428 Casting the third operand to void was tried before, 425 Casting the third operand to void was tried before,
429 but some compilers won't accept it. */ 426 but some compilers won't accept it. */
430 427
431 #define obstack_grow(h,where,length) \ 428 # define obstack_make_room(h,length) \
432 ( (h)->temp = (length), \ 429 ( (h)->temp.tempint = (length), \
433 (((h)->next_free + (h)->temp > (h)->chunk_limit) \ 430 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
434 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \ 431 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0))
435 ((h)->alloc_failed ? 0 : \ 432
436 (bcopy (where, (h)->next_free, (h)->temp), \ 433 # define obstack_grow(h,where,length) \
437 (h)->next_free += (h)->temp))) 434 ( (h)->temp.tempint = (length), \
438 435 (((h)->next_free + (h)->temp.tempint > (h)->chunk_limit) \
439 #define obstack_grow0(h,where,length) \ 436 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
440 ( (h)->temp = (length), \ 437 memcpy ((h)->next_free, where, (h)->temp.tempint), \
441 (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \ 438 (h)->next_free += (h)->temp.tempint)
442 ? (_obstack_newchunk ((h), (h)->temp + 1), 0) : 0), \ 439
443 ((h)->alloc_failed ? 0 : \ 440 # define obstack_grow0(h,where,length) \
444 (bcopy (where, (h)->next_free, (h)->temp), \ 441 ( (h)->temp.tempint = (length), \
445 (h)->next_free += (h)->temp, \ 442 (((h)->next_free + (h)->temp.tempint + 1 > (h)->chunk_limit) \
446 *((h)->next_free)++ = 0))) 443 ? (_obstack_newchunk ((h), (h)->temp.tempint + 1), 0) : 0), \
447 444 memcpy ((h)->next_free, where, (h)->temp.tempint), \
448 #define obstack_1grow(h,datum) \ 445 (h)->next_free += (h)->temp.tempint, \
446 *((h)->next_free)++ = 0)
447
448 # define obstack_1grow(h,datum) \
449 ( (((h)->next_free + 1 > (h)->chunk_limit) \ 449 ( (((h)->next_free + 1 > (h)->chunk_limit) \
450 ? (_obstack_newchunk ((h), 1), 0) : 0), \ 450 ? (_obstack_newchunk ((h), 1), 0) : 0), \
451 ((h)->alloc_failed ? 0 : \ 451 obstack_1grow_fast (h, datum))
452 (*((h)->next_free)++ = (datum))))
453 452
454 #define obstack_ptr_grow(h,datum) \ 453 # define obstack_ptr_grow(h,datum) \
455 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \ 454 ( (((h)->next_free + sizeof (char *) > (h)->chunk_limit) \
456 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \ 455 ? (_obstack_newchunk ((h), sizeof (char *)), 0) : 0), \
457 ((h)->alloc_failed ? 0 : \ 456 obstack_ptr_grow_fast (h, datum))
458 (*((char **)(((h)->next_free+=sizeof(char *))-sizeof(char *))) = ((char *)datum))))
459 457
460 #define obstack_int_grow(h,datum) \ 458 # define obstack_int_grow(h,datum) \
461 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \ 459 ( (((h)->next_free + sizeof (int) > (h)->chunk_limit) \
462 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \ 460 ? (_obstack_newchunk ((h), sizeof (int)), 0) : 0), \
463 ((h)->alloc_failed ? 0 : \ 461 obstack_int_grow_fast (h, datum))
464 (*((int *)(((h)->next_free+=sizeof(int))-sizeof(int))) = ((int)datum))))
465 462
466 #define obstack_ptr_grow_fast(h,aptr) (*((char **)(h)->next_free)++ = (char *)aptr) 463 # define obstack_ptr_grow_fast(h,aptr) \
467 #define obstack_int_grow_fast(h,aint) (*((int *)(h)->next_free)++ = (int)aint) 464 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
468 465
469 #define obstack_blank(h,length) \ 466 # define obstack_int_grow_fast(h,aint) \
470 ( (h)->temp = (length), \ 467 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
471 (((h)->chunk_limit - (h)->next_free < (h)->temp) \
472 ? (_obstack_newchunk ((h), (h)->temp), 0) : 0), \
473 ((h)->alloc_failed ? 0 : \
474 ((h)->next_free += (h)->temp)))
475 468
476 #define obstack_alloc(h,length) \ 469 # define obstack_blank(h,length) \
470 ( (h)->temp.tempint = (length), \
471 (((h)->chunk_limit - (h)->next_free < (h)->temp.tempint) \
472 ? (_obstack_newchunk ((h), (h)->temp.tempint), 0) : 0), \
473 obstack_blank_fast (h, (h)->temp.tempint))
474
475 # define obstack_alloc(h,length) \
477 (obstack_blank ((h), (length)), obstack_finish ((h))) 476 (obstack_blank ((h), (length)), obstack_finish ((h)))
478 477
479 #define obstack_copy(h,where,length) \ 478 # define obstack_copy(h,where,length) \
480 (obstack_grow ((h), (where), (length)), obstack_finish ((h))) 479 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
481 480
482 #define obstack_copy0(h,where,length) \ 481 # define obstack_copy0(h,where,length) \
483 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h))) 482 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
484 483
485 #define obstack_finish(h) \ 484 # define obstack_finish(h) \
486 ( (h)->alloc_failed ? 0 : \ 485 ( ((h)->next_free == (h)->object_base \
487 (((h)->next_free == (h)->object_base \
488 ? (((h)->maybe_empty_object = 1), 0) \ 486 ? (((h)->maybe_empty_object = 1), 0) \
489 : 0), \ 487 : 0), \
490 (h)->temp = __PTR_TO_INT ((h)->object_base), \ 488 (h)->temp.tempptr = (h)->object_base, \
491 (h)->next_free \ 489 (h)->next_free \
492 = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \ 490 = __PTR_ALIGN ((h)->object_base, (h)->next_free, \
493 & ~ ((h)->alignment_mask)), \ 491 (h)->alignment_mask), \
494 (((h)->next_free - (char *)(h)->chunk \ 492 (((h)->next_free - (char *) (h)->chunk \
495 > (h)->chunk_limit - (char *)(h)->chunk) \ 493 > (h)->chunk_limit - (char *) (h)->chunk) \
496 ? ((h)->next_free = (h)->chunk_limit) : 0), \ 494 ? ((h)->next_free = (h)->chunk_limit) : 0), \
497 (h)->object_base = (h)->next_free, \ 495 (h)->object_base = (h)->next_free, \
498 __INT_TO_PTR ((h)->temp))) 496 (h)->temp.tempptr)
499 497
500 #ifdef __STDC__ 498 # define obstack_free(h,obj) \
501 #define obstack_free(h,obj) \ 499 ( (h)->temp.tempint = (char *) (obj) - (char *) (h)->chunk, \
502 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \ 500 ((((h)->temp.tempint > 0 \
503 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\ 501 && (h)->temp.tempint < (h)->chunk_limit - (char *) (h)->chunk)) \
504 ? (int) ((h)->next_free = (h)->object_base \
505 = (h)->temp + (char *) (h)->chunk) \
506 : (((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0), 0)))
507 #else
508 #define obstack_free(h,obj) \
509 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
510 (((h)->temp > 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
511 ? (int) ((h)->next_free = (h)->object_base \ 502 ? (int) ((h)->next_free = (h)->object_base \
512 = (h)->temp + (char *) (h)->chunk) \ 503 = (h)->temp.tempint + (char *) (h)->chunk) \
513 : (_obstack_free ((h), (h)->temp + (char *) (h)->chunk), 0))) 504 : (((obstack_free) ((h), (h)->temp.tempint + (char *) (h)->chunk), 0), 0)))
514 #endif
515 505
516 #endif /* not __GNUC__ or not __STDC__ */ 506 #endif /* not __GNUC__ or not __STDC__ */
517 507
518 #endif /* not __OBSTACK_H__ */ 508 #ifdef __cplusplus
509 } /* C++ */
510 #endif
511
512 #endif /* obstack.h */
......
1 /* rpl_realloc.c -- a replacement for broken realloc implementations 1 /* realloc() function that is glibc compatible.
2 Copyright (C) 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -15,18 +15,32 @@ ...@@ -15,18 +15,32 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 17
18 /* written by Jim Meyering */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #undef realloc
24
18 #include <stdlib.h> 25 #include <stdlib.h>
19 26
27 /* Change the size of an allocated block of memory P to N bytes,
28 with error checking. If N is zero, change it to 1. If P is NULL,
29 use malloc. */
30
20 void * 31 void *
21 rpl_realloc(void *ptr, size_t size) 32 rpl_realloc (void *p, size_t n)
22 { 33 {
23 if (!ptr) 34 if (n == 0)
24 return malloc(size);
25 if (!size)
26 { 35 {
27 if (ptr) 36 n = 1;
28 free(ptr); 37
29 return malloc(size); 38 /* In theory realloc might fail, so don't rely on it to free. */
39 free (p);
40 p = NULL;
30 } 41 }
31 return realloc(ptr, size); 42
43 if (p == NULL)
44 return malloc (n);
45 return realloc (p, n);
32 } 46 }
......