Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/obstack.c @ 18065:fd4663ad7454
time_rz: port to Solaris etc.
Works around a tzname problem on platforms like Solaris that have
tzname but not tm_zone, by setting tzname at the appropriate time
and restoring it later.
* lib/time_rz.c (tzname_address, tzname_value) [HAVE_TZNAME]:
New static vars.
(save_abbr) [HAVE_TZNAME]: Set them.
(revert_tz) [HAVE_TZNAME]: Clear or use them.
(restore_tzname): New function.
(localtime_rz, mktime_z): Use it.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sat, 25 Jul 2015 15:20:10 -0700 |
parents | ab58d4870664 |
children |
rev | line source |
---|---|
334 | 1 /* obstack.c - subroutines used implicitly by object stack macros |
17848 | 2 Copyright (C) 1988-2015 Free Software Foundation, Inc. |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
3 This file is part of the GNU C Library. |
4667 | 4 |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
5 The GNU C Library is free software; you can redistribute it and/or |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
7 License as published by the Free Software Foundation; either |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
4667 | 9 |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
10 The GNU C Library is distributed in the hope that it will be useful, |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
13 Lesser General Public License for more details. |
334 | 14 |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
16 License along with the GNU C Library; if not, see |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
17 <http://www.gnu.org/licenses/>. */ |
334 | 18 |
19 | |
4805
8f430f14ff21
Merge changes from glibc obstack; minor cleanups to make it easier to
Paul Eggert <eggert@cs.ucla.edu>
parents:
4696
diff
changeset
|
20 #ifdef _LIBC |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
21 # 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
|
22 #else |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
7162
diff
changeset
|
23 # include <config.h> |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4805
diff
changeset
|
24 # 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
|
25 #endif |
334 | 26 |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
27 /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
28 obstack.h must be incremented whenever callers compiled using an old |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
29 obstack.h can no longer properly call the functions in this file. */ |
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. */ |
3362 | 39 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 |
2475 | 40 # include <gnu-versions.h> |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
41 # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
42 || (_GNU_OBSTACK_INTERFACE_VERSION == 1 \ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
43 && _OBSTACK_INTERFACE_VERSION == 2 \ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
44 && defined SIZEOF_INT && defined SIZEOF_SIZE_T \ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
45 && SIZEOF_INT == SIZEOF_SIZE_T)) |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
46 # define _OBSTACK_ELIDE_CODE |
2475 | 47 # endif |
779 | 48 #endif |
49 | |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
50 #ifndef _OBSTACK_ELIDE_CODE |
17789
47c2be5d17f0
obstack: port to platforms that #define __alignof__
Paul Eggert <eggert@cs.ucla.edu>
parents:
17782
diff
changeset
|
51 /* If GCC, or if an oddball (testing?) host that #defines __alignof__, |
47c2be5d17f0
obstack: port to platforms that #define __alignof__
Paul Eggert <eggert@cs.ucla.edu>
parents:
17782
diff
changeset
|
52 use the already-supplied __alignof__. Otherwise, this must be Gnulib |
47c2be5d17f0
obstack: port to platforms that #define __alignof__
Paul Eggert <eggert@cs.ucla.edu>
parents:
17782
diff
changeset
|
53 (as glibc assumes GCC); defer to Gnulib's alignof_type. */ |
47c2be5d17f0
obstack: port to platforms that #define __alignof__
Paul Eggert <eggert@cs.ucla.edu>
parents:
17782
diff
changeset
|
54 # if !defined __GNUC__ && !defined __alignof__ |
17782
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
55 # include <alignof.h> |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
56 # define __alignof__(type) alignof_type (type) |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
57 # endif |
17775 | 58 # include <stdlib.h> |
7162
19c2e5121b2f
Add and change modules to make it easier for coreutils to use
Paul Eggert <eggert@cs.ucla.edu>
parents:
6376
diff
changeset
|
59 # include <stdint.h> |
5178
0804f69d0572
Include <inttypes.h> and <stdint.h> if available.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5174
diff
changeset
|
60 |
17782
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
61 # ifndef MAX |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
62 # define MAX(a,b) ((a) > (b) ? (a) : (b)) |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
63 # endif |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
64 |
334 | 65 /* Determine default alignment. */ |
17782
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
66 |
334 | 67 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. |
68 But in fact it might be less smart and round addresses to as much as | |
17782
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
69 DEFAULT_ROUNDING. So we prepare for it to do that. |
334 | 70 |
17782
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
71 DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */ |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
72 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \ |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
73 MAX (__alignof__ (uintmax_t), \ |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
74 __alignof__ (void *))) |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
75 #define DEFAULT_ROUNDING MAX (sizeof (long double), \ |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
76 MAX (sizeof (uintmax_t), \ |
f6c039e09a3b
obstack: prefer __alignof__ to alignof
Paul Eggert <eggert@cs.ucla.edu>
parents:
17781
diff
changeset
|
77 sizeof (void *))) |
881 | 78 |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
79 /* Call functions with either the traditional malloc/free calling |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
80 interface, or the mmalloc/mfree interface (that adds an extra first |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
81 argument), based on the value of use_extra_arg. */ |
334 | 82 |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
83 static void * |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
84 call_chunkfun (struct obstack *h, size_t size) |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
85 { |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
86 if (h->use_extra_arg) |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
87 return h->chunkfun.extra (h->extra_arg, size); |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
88 else |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
89 return h->chunkfun.plain (size); |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
90 } |
881 | 91 |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
92 static void |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
93 call_freefun (struct obstack *h, void *old_chunk) |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
94 { |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
95 if (h->use_extra_arg) |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
96 h->freefun.extra (h->extra_arg, old_chunk); |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
97 else |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
98 h->freefun.plain (old_chunk); |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
99 } |
334 | 100 |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
101 |
334 | 102 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default). |
103 Objects start on multiples of ALIGNMENT (0 means use default). | |
104 | |
2475 | 105 Return nonzero if successful, calls obstack_alloc_failed_handler if |
106 allocation fails. */ | |
334 | 107 |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
108 static int |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
109 _obstack_begin_worker (struct obstack *h, |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
110 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment) |
334 | 111 { |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
112 struct _obstack_chunk *chunk; /* points to new chunk */ |
334 | 113 |
114 if (alignment == 0) | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
115 alignment = DEFAULT_ALIGNMENT; |
334 | 116 if (size == 0) |
117 /* Default size is what GNU malloc can fit in a 4096-byte block. */ | |
118 { | |
119 /* 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
|
120 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
|
121 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
|
122 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
|
123 allocated. |
334 | 124 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
125 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
|
126 less sensitive to the size of the request. */ |
334 | 127 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
|
128 + 4 + DEFAULT_ROUNDING - 1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
129 & ~(DEFAULT_ROUNDING - 1)); |
334 | 130 size = 4096 - extra; |
131 } | |
132 | |
133 h->chunk_size = size; | |
134 h->alignment_mask = alignment - 1; | |
135 | |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
136 chunk = h->chunk = call_chunkfun (h, h->chunk_size); |
334 | 137 if (!chunk) |
881 | 138 (*obstack_alloc_failed_handler) (); |
5174
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
139 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
|
140 alignment - 1); |
17776 | 141 h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size; |
334 | 142 chunk->prev = 0; |
143 /* The initial chunk now contains no empty object. */ | |
144 h->maybe_empty_object = 0; | |
881 | 145 h->alloc_failed = 0; |
334 | 146 return 1; |
147 } | |
148 | |
149 int | |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
150 _obstack_begin (struct obstack *h, |
17780
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
151 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment, |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
152 void *(*chunkfun) (size_t), |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
153 void (*freefun) (void *)) |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
154 { |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
155 h->chunkfun.plain = chunkfun; |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
156 h->freefun.plain = freefun; |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
157 h->use_extra_arg = 0; |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
158 return _obstack_begin_worker (h, size, alignment); |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
159 } |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
160 |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
161 int |
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
162 _obstack_begin_1 (struct obstack *h, |
17780
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
163 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment, |
17777
d05e1a039ad3
obstack: 64-bit obstack support, part 1
Alan Modra <amodra@gmail.com>
parents:
17776
diff
changeset
|
164 void *(*chunkfun) (void *, size_t), |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
165 void (*freefun) (void *, void *), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
166 void *arg) |
334 | 167 { |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
168 h->chunkfun.extra = chunkfun; |
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
169 h->freefun.extra = freefun; |
334 | 170 h->extra_arg = arg; |
171 h->use_extra_arg = 1; | |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
172 return _obstack_begin_worker (h, size, alignment); |
334 | 173 } |
174 | |
175 /* Allocate a new current chunk for the obstack *H | |
176 on the assumption that LENGTH bytes need to be added | |
177 to the current object, or a new object of length LENGTH allocated. | |
178 Copies any partial object from the end of the old chunk | |
179 to the beginning of the new one. */ | |
180 | |
181 void | |
17778
589ffca43f9b
obstack: 64-bit obstack support, part 2
Alan Modra <amodra@gmail.com>
parents:
17777
diff
changeset
|
182 _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length) |
334 | 183 { |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
184 struct _obstack_chunk *old_chunk = h->chunk; |
17780
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
185 struct _obstack_chunk *new_chunk = 0; |
17778
589ffca43f9b
obstack: 64-bit obstack support, part 2
Alan Modra <amodra@gmail.com>
parents:
17777
diff
changeset
|
186 size_t obj_size = h->next_free - h->object_base; |
2915 | 187 char *object_base; |
334 | 188 |
189 /* Compute size for new chunk. */ | |
17780
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
190 size_t sum1 = obj_size + length; |
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
191 size_t sum2 = sum1 + h->alignment_mask; |
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
192 size_t new_size = sum2 + (obj_size >> 3) + 100; |
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
193 if (new_size < sum2) |
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
194 new_size = sum2; |
334 | 195 if (new_size < h->chunk_size) |
196 new_size = h->chunk_size; | |
197 | |
198 /* Allocate and initialize the new chunk. */ | |
17780
4fbd46593c56
obstack: use size_t alignments and check for overflow
Paul Eggert <eggert@cs.ucla.edu>
parents:
17779
diff
changeset
|
199 if (obj_size <= sum1 && sum1 <= sum2) |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
200 new_chunk = call_chunkfun (h, new_size); |
334 | 201 if (!new_chunk) |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
202 (*obstack_alloc_failed_handler)(); |
334 | 203 h->chunk = new_chunk; |
204 new_chunk->prev = old_chunk; | |
205 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; | |
206 | |
2915 | 207 /* Compute an aligned object_base in the new chunk */ |
208 object_base = | |
5065
4ce8ff7e5f06
Port obstack to the AS/400.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5057
diff
changeset
|
209 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask); |
2915 | 210 |
17776 | 211 /* Move the existing object to the new chunk. */ |
212 memcpy (object_base, h->object_base, obj_size); | |
334 | 213 |
214 /* If the object just copied was the only data in OLD_CHUNK, | |
215 free that chunk and remove it from the chain. | |
216 But not if that chunk might contain an empty object. */ | |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
217 if (!h->maybe_empty_object |
5174
7beb82c9f4d7
(_obstack_begin, _obstack_begin_1, _obstack_newchunk):
Paul Eggert <eggert@cs.ucla.edu>
parents:
5071
diff
changeset
|
218 && (h->object_base |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
219 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
220 h->alignment_mask))) |
334 | 221 { |
222 new_chunk->prev = old_chunk->prev; | |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
223 call_freefun (h, old_chunk); |
334 | 224 } |
225 | |
2915 | 226 h->object_base = object_base; |
334 | 227 h->next_free = h->object_base + obj_size; |
228 /* The new chunk certainly contains no empty object yet. */ | |
229 h->maybe_empty_object = 0; | |
230 } | |
231 | |
232 /* Return nonzero if object OBJ has been allocated from obstack H. | |
233 This is here for debugging. | |
234 If you use it in a program, you are probably losing. */ | |
235 | |
236 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in | |
237 obstack.h because it is just for debugging. */ | |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
238 int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__; |
334 | 239 |
240 int | |
4667 | 241 _obstack_allocated_p (struct obstack *h, void *obj) |
334 | 242 { |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
243 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
244 struct _obstack_chunk *plp; /* point to previous chunk if any */ |
334 | 245 |
246 lp = (h)->chunk; | |
247 /* We use >= rather than > since the object cannot be exactly at | |
248 the beginning of the chunk but might be an empty object exactly | |
881 | 249 at the end of an adjacent chunk. */ |
4667 | 250 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
334 | 251 { |
252 plp = lp->prev; | |
253 lp = plp; | |
254 } | |
255 return lp != 0; | |
256 } | |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
257 |
334 | 258 /* Free objects in obstack H, including OBJ and everything allocate |
259 more recently than OBJ. If OBJ is zero, free everything in H. */ | |
260 | |
261 void | |
17776 | 262 _obstack_free (struct obstack *h, void *obj) |
334 | 263 { |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
264 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */ |
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
265 struct _obstack_chunk *plp; /* point to previous chunk if any */ |
334 | 266 |
267 lp = h->chunk; | |
268 /* We use >= because there cannot be an object at the beginning of a chunk. | |
269 But there can be an empty object at that address | |
270 at the end of another chunk. */ | |
4667 | 271 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
334 | 272 { |
273 plp = lp->prev; | |
17792
7497a1d1d497
obstack: avoid potentially-nonportable function casts
Paul Eggert <eggert@cs.ucla.edu>
parents:
17789
diff
changeset
|
274 call_freefun (h, lp); |
334 | 275 lp = plp; |
276 /* 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
|
277 chunk contains an empty object, so assume that it may. */ |
334 | 278 h->maybe_empty_object = 1; |
279 } | |
280 if (lp) | |
281 { | |
881 | 282 h->object_base = h->next_free = (char *) (obj); |
334 | 283 h->chunk_limit = lp->limit; |
284 h->chunk = lp; | |
285 } | |
286 else if (obj != 0) | |
287 /* obj is not in any of the chunks! */ | |
288 abort (); | |
289 } | |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
290 |
17778
589ffca43f9b
obstack: 64-bit obstack support, part 2
Alan Modra <amodra@gmail.com>
parents:
17777
diff
changeset
|
291 _OBSTACK_SIZE_T |
4667 | 292 _obstack_memory_used (struct obstack *h) |
881 | 293 { |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
294 struct _obstack_chunk *lp; |
17778
589ffca43f9b
obstack: 64-bit obstack support, part 2
Alan Modra <amodra@gmail.com>
parents:
17777
diff
changeset
|
295 _OBSTACK_SIZE_T nbytes = 0; |
881 | 296 |
297 for (lp = h->chunk; lp != 0; lp = lp->prev) | |
298 { | |
299 nbytes += lp->limit - (char *) lp; | |
300 } | |
301 return nbytes; | |
302 } | |
17647
ae63f4dd18f3
obstack: merge with glibc changes
Paul Eggert <eggert@cs.ucla.edu>
parents:
17587
diff
changeset
|
303 |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
304 # ifndef _OBSTACK_NO_ERROR_HANDLER |
881 | 305 /* Define the error handler. */ |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
306 # include <stdio.h> |
17775 | 307 |
308 /* Exit value used when 'print_and_abort' is used. */ | |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
309 # ifdef _LIBC |
17775 | 310 int obstack_exit_failure = EXIT_FAILURE; |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
311 # else |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
312 # include "exitfail.h" |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
313 # define obstack_exit_failure exit_failure |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
314 # endif |
17775 | 315 |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
316 # ifdef _LIBC |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
317 # include <libintl.h> |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
318 # else |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
319 # include "gettext.h" |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
320 # endif |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
321 # ifndef _ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
322 # define _(msgid) gettext (msgid) |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
323 # endif |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3552
diff
changeset
|
324 |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
325 # ifdef _LIBC |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
326 # include <libio/iolibio.h> |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
327 # endif |
881 | 328 |
15400 | 329 static _Noreturn void |
4667 | 330 print_and_abort (void) |
881 | 331 { |
3361 | 332 /* Don't change any of these strings. Yes, it would be possible to add |
333 the newline to the string and use fputs or so. But this must not | |
334 happen because the "memory exhausted" message appears in other places | |
335 like this and the translation should be reused instead of creating | |
336 a very similar string which requires a separate translation. */ | |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
337 # ifdef _LIBC |
6376
56829cd68e67
* config/srclist.txt: Add glibc bug 321 for obstack.c, obstack.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
338 (void) __fxprintf (NULL, "%s\n", _("memory exhausted")); |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
339 # else |
6376
56829cd68e67
* config/srclist.txt: Add glibc bug 321 for obstack.c, obstack.h.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
340 fprintf (stderr, "%s\n", _("memory exhausted")); |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
341 # endif |
881 | 342 exit (obstack_exit_failure); |
343 } | |
334 | 344 |
17775 | 345 /* The functions allocating more room by calling 'obstack_chunk_alloc' |
346 jump to the handler pointed to by 'obstack_alloc_failed_handler'. | |
347 This can be set to a user defined function which should either | |
348 abort gracefully or use longjump - but shouldn't return. This | |
349 variable by default points to the internal function | |
350 'print_and_abort'. */ | |
351 void (*obstack_alloc_failed_handler) (void) = print_and_abort; | |
17779
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
352 # endif /* !_OBSTACK_NO_ERROR_HANDLER */ |
8ba63d29429e
obstack: 64-bit obstack support, part 3
Alan Modra <amodra@gmail.com>
parents:
17778
diff
changeset
|
353 #endif /* !_OBSTACK_ELIDE_CODE */ |