annotate libiberty/obstack.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* obstack.c - subroutines used implicitly by object stack macros
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 1988-2020 Free Software Foundation, Inc.
111
kono
parents: 0
diff changeset
3 This file is part of the GNU C Library.
kono
parents: 0
diff changeset
4
kono
parents: 0
diff changeset
5 The GNU C Library is free software; you can redistribute it and/or
kono
parents: 0
diff changeset
6 modify it under the terms of the GNU Lesser General Public
kono
parents: 0
diff changeset
7 License as published by the Free Software Foundation; either
kono
parents: 0
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
kono
parents: 0
diff changeset
9
kono
parents: 0
diff changeset
10 The GNU C Library is distributed in the hope that it will be useful,
kono
parents: 0
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents: 0
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kono
parents: 0
diff changeset
13 Lesser General Public License for more details.
kono
parents: 0
diff changeset
14
kono
parents: 0
diff changeset
15 You should have received a copy of the GNU Lesser General Public
kono
parents: 0
diff changeset
16 License along with the GNU C Library; if not, see
kono
parents: 0
diff changeset
17 <http://www.gnu.org/licenses/>. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
111
kono
parents: 0
diff changeset
20 #ifdef _LIBC
kono
parents: 0
diff changeset
21 # include <obstack.h>
kono
parents: 0
diff changeset
22 #else
kono
parents: 0
diff changeset
23 # include <config.h>
kono
parents: 0
diff changeset
24 # include "obstack.h"
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #endif
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
111
kono
parents: 0
diff changeset
27 /* NOTE BEFORE MODIFYING THIS FILE: _OBSTACK_INTERFACE_VERSION in
kono
parents: 0
diff changeset
28 obstack.h must be incremented whenever callers compiled using an old
kono
parents: 0
diff changeset
29 obstack.h can no longer properly call the functions in this file. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 /* Comment out all this code if we are using the GNU C Library, and are not
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 actually compiling the library itself, and the installed library
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 supports the same library interface we do. This code is part of the GNU
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 C Library, but also included in many other GNU distributions. Compiling
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 and linking in this code is a waste when using the GNU C library
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 (especially if it is a shared library). Rather than having every GNU
111
kono
parents: 0
diff changeset
37 program understand 'configure --with-gnu-libc' and omit the object
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 files, it is simpler to just do this in the source for each such file. */
111
kono
parents: 0
diff changeset
39 #if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1
kono
parents: 0
diff changeset
40 # include <gnu-versions.h>
kono
parents: 0
diff changeset
41 # if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \
kono
parents: 0
diff changeset
42 || (_GNU_OBSTACK_INTERFACE_VERSION == 1 \
kono
parents: 0
diff changeset
43 && _OBSTACK_INTERFACE_VERSION == 2 \
kono
parents: 0
diff changeset
44 && defined SIZEOF_INT && defined SIZEOF_SIZE_T \
kono
parents: 0
diff changeset
45 && SIZEOF_INT == SIZEOF_SIZE_T))
kono
parents: 0
diff changeset
46 # define _OBSTACK_ELIDE_CODE
kono
parents: 0
diff changeset
47 # endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 #endif
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
111
kono
parents: 0
diff changeset
50 #ifndef _OBSTACK_ELIDE_CODE
kono
parents: 0
diff changeset
51 /* If GCC, or if an oddball (testing?) host that #defines __alignof__,
kono
parents: 0
diff changeset
52 use the already-supplied __alignof__. Otherwise, this must be Gnulib
kono
parents: 0
diff changeset
53 (as glibc assumes GCC); defer to Gnulib's alignof_type. */
kono
parents: 0
diff changeset
54 # if !defined __GNUC__ && !defined __IBM__ALIGNOF__ && !defined __alignof__
kono
parents: 0
diff changeset
55 # if defined __cplusplus
kono
parents: 0
diff changeset
56 template <class type> struct alignof_helper { char __slot1; type __slot2; };
kono
parents: 0
diff changeset
57 # define __alignof__(type) offsetof (alignof_helper<type>, __slot2)
kono
parents: 0
diff changeset
58 # else
kono
parents: 0
diff changeset
59 # define __alignof__(type) \
kono
parents: 0
diff changeset
60 offsetof (struct { char __slot1; type __slot2; }, __slot2)
kono
parents: 0
diff changeset
61 # endif
kono
parents: 0
diff changeset
62 # endif
kono
parents: 0
diff changeset
63 # include <stdlib.h>
kono
parents: 0
diff changeset
64 # include <stdint.h>
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
111
kono
parents: 0
diff changeset
66 # ifndef MAX
kono
parents: 0
diff changeset
67 # define MAX(a,b) ((a) > (b) ? (a) : (b))
kono
parents: 0
diff changeset
68 # endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 /* Determine default alignment. */
111
kono
parents: 0
diff changeset
71
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 But in fact it might be less smart and round addresses to as much as
111
kono
parents: 0
diff changeset
74 DEFAULT_ROUNDING. So we prepare for it to do that.
kono
parents: 0
diff changeset
75
kono
parents: 0
diff changeset
76 DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */
kono
parents: 0
diff changeset
77 #define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \
kono
parents: 0
diff changeset
78 MAX (__alignof__ (uintmax_t), \
kono
parents: 0
diff changeset
79 __alignof__ (void *)))
kono
parents: 0
diff changeset
80 #define DEFAULT_ROUNDING MAX (sizeof (long double), \
kono
parents: 0
diff changeset
81 MAX (sizeof (uintmax_t), \
kono
parents: 0
diff changeset
82 sizeof (void *)))
kono
parents: 0
diff changeset
83
kono
parents: 0
diff changeset
84 /* Call functions with either the traditional malloc/free calling
kono
parents: 0
diff changeset
85 interface, or the mmalloc/mfree interface (that adds an extra first
kono
parents: 0
diff changeset
86 argument), based on the value of use_extra_arg. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
111
kono
parents: 0
diff changeset
88 static void *
kono
parents: 0
diff changeset
89 call_chunkfun (struct obstack *h, size_t size)
kono
parents: 0
diff changeset
90 {
kono
parents: 0
diff changeset
91 if (h->use_extra_arg)
kono
parents: 0
diff changeset
92 return h->chunkfun.extra (h->extra_arg, size);
kono
parents: 0
diff changeset
93 else
kono
parents: 0
diff changeset
94 return h->chunkfun.plain (size);
kono
parents: 0
diff changeset
95 }
kono
parents: 0
diff changeset
96
kono
parents: 0
diff changeset
97 static void
kono
parents: 0
diff changeset
98 call_freefun (struct obstack *h, void *old_chunk)
kono
parents: 0
diff changeset
99 {
kono
parents: 0
diff changeset
100 if (h->use_extra_arg)
kono
parents: 0
diff changeset
101 h->freefun.extra (h->extra_arg, old_chunk);
kono
parents: 0
diff changeset
102 else
kono
parents: 0
diff changeset
103 h->freefun.plain (old_chunk);
kono
parents: 0
diff changeset
104 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 Objects start on multiples of ALIGNMENT (0 means use default).
111
kono
parents: 0
diff changeset
109
kono
parents: 0
diff changeset
110 Return nonzero if successful, calls obstack_alloc_failed_handler if
kono
parents: 0
diff changeset
111 allocation fails. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
111
kono
parents: 0
diff changeset
113 static int
kono
parents: 0
diff changeset
114 _obstack_begin_worker (struct obstack *h,
kono
parents: 0
diff changeset
115 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 {
111
kono
parents: 0
diff changeset
117 struct _obstack_chunk *chunk; /* points to new chunk */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 if (alignment == 0)
111
kono
parents: 0
diff changeset
120 alignment = DEFAULT_ALIGNMENT;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 if (size == 0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 /* Default size is what GNU malloc can fit in a 4096-byte block. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
111
kono
parents: 0
diff changeset
125 Use the values for range checking, because if range checking is off,
kono
parents: 0
diff changeset
126 the extra bytes won't be missed terribly, but if range checking is on
kono
parents: 0
diff changeset
127 and we used a larger request, a whole extra 4096 bytes would be
kono
parents: 0
diff changeset
128 allocated.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
111
kono
parents: 0
diff changeset
130 These number are irrelevant to the new GNU malloc. I suspect it is
kono
parents: 0
diff changeset
131 less sensitive to the size of the request. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
111
kono
parents: 0
diff changeset
133 + 4 + DEFAULT_ROUNDING - 1)
kono
parents: 0
diff changeset
134 & ~(DEFAULT_ROUNDING - 1));
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 size = 4096 - extra;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 h->chunk_size = size;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 h->alignment_mask = alignment - 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140
111
kono
parents: 0
diff changeset
141 chunk = (struct _obstack_chunk *) call_chunkfun (h, h->chunk_size);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 if (!chunk)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 (*obstack_alloc_failed_handler) ();
111
kono
parents: 0
diff changeset
144 h->chunk = chunk;
kono
parents: 0
diff changeset
145 h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents,
kono
parents: 0
diff changeset
146 alignment - 1);
kono
parents: 0
diff changeset
147 h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 chunk->prev = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 /* The initial chunk now contains no empty object. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 h->maybe_empty_object = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 h->alloc_failed = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 return 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 int
111
kono
parents: 0
diff changeset
156 _obstack_begin (struct obstack *h,
kono
parents: 0
diff changeset
157 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
kono
parents: 0
diff changeset
158 void *(*chunkfun) (size_t),
kono
parents: 0
diff changeset
159 void (*freefun) (void *))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 {
111
kono
parents: 0
diff changeset
161 h->chunkfun.plain = chunkfun;
kono
parents: 0
diff changeset
162 h->freefun.plain = freefun;
kono
parents: 0
diff changeset
163 h->use_extra_arg = 0;
kono
parents: 0
diff changeset
164 return _obstack_begin_worker (h, size, alignment);
kono
parents: 0
diff changeset
165 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166
111
kono
parents: 0
diff changeset
167 int
kono
parents: 0
diff changeset
168 _obstack_begin_1 (struct obstack *h,
kono
parents: 0
diff changeset
169 _OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment,
kono
parents: 0
diff changeset
170 void *(*chunkfun) (void *, size_t),
kono
parents: 0
diff changeset
171 void (*freefun) (void *, void *),
kono
parents: 0
diff changeset
172 void *arg)
kono
parents: 0
diff changeset
173 {
kono
parents: 0
diff changeset
174 h->chunkfun.extra = chunkfun;
kono
parents: 0
diff changeset
175 h->freefun.extra = freefun;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 h->extra_arg = arg;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 h->use_extra_arg = 1;
111
kono
parents: 0
diff changeset
178 return _obstack_begin_worker (h, size, alignment);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 /* Allocate a new current chunk for the obstack *H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 on the assumption that LENGTH bytes need to be added
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 to the current object, or a new object of length LENGTH allocated.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 Copies any partial object from the end of the old chunk
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 to the beginning of the new one. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 void
111
kono
parents: 0
diff changeset
188 _obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 {
111
kono
parents: 0
diff changeset
190 struct _obstack_chunk *old_chunk = h->chunk;
kono
parents: 0
diff changeset
191 struct _obstack_chunk *new_chunk = 0;
kono
parents: 0
diff changeset
192 size_t obj_size = h->next_free - h->object_base;
kono
parents: 0
diff changeset
193 char *object_base;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 /* Compute size for new chunk. */
111
kono
parents: 0
diff changeset
196 size_t sum1 = obj_size + length;
kono
parents: 0
diff changeset
197 size_t sum2 = sum1 + h->alignment_mask;
kono
parents: 0
diff changeset
198 size_t new_size = sum2 + (obj_size >> 3) + 100;
kono
parents: 0
diff changeset
199 if (new_size < sum2)
kono
parents: 0
diff changeset
200 new_size = sum2;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 if (new_size < h->chunk_size)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 new_size = h->chunk_size;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 /* Allocate and initialize the new chunk. */
111
kono
parents: 0
diff changeset
205 if (obj_size <= sum1 && sum1 <= sum2)
kono
parents: 0
diff changeset
206 new_chunk = (struct _obstack_chunk *) call_chunkfun (h, new_size);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 if (!new_chunk)
111
kono
parents: 0
diff changeset
208 (*obstack_alloc_failed_handler)();
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 h->chunk = new_chunk;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 new_chunk->prev = old_chunk;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212
111
kono
parents: 0
diff changeset
213 /* Compute an aligned object_base in the new chunk */
kono
parents: 0
diff changeset
214 object_base =
kono
parents: 0
diff changeset
215 __PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask);
kono
parents: 0
diff changeset
216
kono
parents: 0
diff changeset
217 /* Move the existing object to the new chunk. */
kono
parents: 0
diff changeset
218 memcpy (object_base, h->object_base, obj_size);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 /* If the object just copied was the only data in OLD_CHUNK,
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 free that chunk and remove it from the chain.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 But not if that chunk might contain an empty object. */
111
kono
parents: 0
diff changeset
223 if (!h->maybe_empty_object
kono
parents: 0
diff changeset
224 && (h->object_base
kono
parents: 0
diff changeset
225 == __PTR_ALIGN ((char *) old_chunk, old_chunk->contents,
kono
parents: 0
diff changeset
226 h->alignment_mask)))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 new_chunk->prev = old_chunk->prev;
111
kono
parents: 0
diff changeset
229 call_freefun (h, old_chunk);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231
111
kono
parents: 0
diff changeset
232 h->object_base = object_base;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 h->next_free = h->object_base + obj_size;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 /* The new chunk certainly contains no empty object yet. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 h->maybe_empty_object = 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 /* Return nonzero if object OBJ has been allocated from obstack H.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 This is here for debugging.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 If you use it in a program, you are probably losing. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 /* Suppress -Wmissing-prototypes warning. We don't want to declare this in
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 obstack.h because it is just for debugging. */
111
kono
parents: 0
diff changeset
244 int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
245
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 int
111
kono
parents: 0
diff changeset
247 _obstack_allocated_p (struct obstack *h, void *obj)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 {
111
kono
parents: 0
diff changeset
249 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
kono
parents: 0
diff changeset
250 struct _obstack_chunk *plp; /* point to previous chunk if any */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 lp = (h)->chunk;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 /* We use >= rather than > since the object cannot be exactly at
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 the beginning of the chunk but might be an empty object exactly
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 at the end of an adjacent chunk. */
111
kono
parents: 0
diff changeset
256 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 plp = lp->prev;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 lp = plp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 return lp != 0;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 }
111
kono
parents: 0
diff changeset
263
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 /* Free objects in obstack H, including OBJ and everything allocate
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 more recently than OBJ. If OBJ is zero, free everything in H. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 void
111
kono
parents: 0
diff changeset
268 _obstack_free (struct obstack *h, void *obj)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 {
111
kono
parents: 0
diff changeset
270 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
kono
parents: 0
diff changeset
271 struct _obstack_chunk *plp; /* point to previous chunk if any */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 lp = h->chunk;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 /* We use >= because there cannot be an object at the beginning of a chunk.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 But there can be an empty object at that address
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 at the end of another chunk. */
111
kono
parents: 0
diff changeset
277 while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 plp = lp->prev;
111
kono
parents: 0
diff changeset
280 call_freefun (h, lp);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 lp = plp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 /* If we switch chunks, we can't tell whether the new current
111
kono
parents: 0
diff changeset
283 chunk contains an empty object, so assume that it may. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 h->maybe_empty_object = 1;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 if (lp)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 h->object_base = h->next_free = (char *) (obj);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 h->chunk_limit = lp->limit;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 h->chunk = lp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 else if (obj != 0)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 /* obj is not in any of the chunks! */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 abort ();
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296
111
kono
parents: 0
diff changeset
297 _OBSTACK_SIZE_T
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 _obstack_memory_used (struct obstack *h)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 {
111
kono
parents: 0
diff changeset
300 struct _obstack_chunk *lp;
kono
parents: 0
diff changeset
301 _OBSTACK_SIZE_T nbytes = 0;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 for (lp = h->chunk; lp != 0; lp = lp->prev)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 nbytes += lp->limit - (char *) lp;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 return nbytes;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 }
111
kono
parents: 0
diff changeset
309
kono
parents: 0
diff changeset
310 # ifndef _OBSTACK_NO_ERROR_HANDLER
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 /* Define the error handler. */
111
kono
parents: 0
diff changeset
312 # include <stdio.h>
kono
parents: 0
diff changeset
313
kono
parents: 0
diff changeset
314 /* Exit value used when 'print_and_abort' is used. */
kono
parents: 0
diff changeset
315 # ifdef _LIBC
kono
parents: 0
diff changeset
316 int obstack_exit_failure = EXIT_FAILURE;
kono
parents: 0
diff changeset
317 # else
kono
parents: 0
diff changeset
318 # ifndef EXIT_FAILURE
kono
parents: 0
diff changeset
319 # define EXIT_FAILURE 1
kono
parents: 0
diff changeset
320 # endif
kono
parents: 0
diff changeset
321 # define obstack_exit_failure EXIT_FAILURE
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 # endif
111
kono
parents: 0
diff changeset
323
kono
parents: 0
diff changeset
324 # if defined _LIBC || (HAVE_LIBINTL_H && ENABLE_NLS)
kono
parents: 0
diff changeset
325 # include <libintl.h>
kono
parents: 0
diff changeset
326 # ifndef _
kono
parents: 0
diff changeset
327 # define _(msgid) gettext (msgid)
kono
parents: 0
diff changeset
328 # endif
kono
parents: 0
diff changeset
329 # else
kono
parents: 0
diff changeset
330 # ifndef _
kono
parents: 0
diff changeset
331 # define _(msgid) (msgid)
kono
parents: 0
diff changeset
332 # endif
kono
parents: 0
diff changeset
333 # endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334
111
kono
parents: 0
diff changeset
335 # if !(defined _Noreturn \
kono
parents: 0
diff changeset
336 || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112))
kono
parents: 0
diff changeset
337 # if ((defined __GNUC__ \
kono
parents: 0
diff changeset
338 && (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))) \
kono
parents: 0
diff changeset
339 || (defined __SUNPRO_C && __SUNPRO_C >= 0x5110))
kono
parents: 0
diff changeset
340 # define _Noreturn __attribute__ ((__noreturn__))
kono
parents: 0
diff changeset
341 # elif defined _MSC_VER && _MSC_VER >= 1200
kono
parents: 0
diff changeset
342 # define _Noreturn __declspec (noreturn)
kono
parents: 0
diff changeset
343 # else
kono
parents: 0
diff changeset
344 # define _Noreturn
kono
parents: 0
diff changeset
345 # endif
kono
parents: 0
diff changeset
346 # endif
kono
parents: 0
diff changeset
347
kono
parents: 0
diff changeset
348 # ifdef _LIBC
kono
parents: 0
diff changeset
349 # include <libio/iolibio.h>
kono
parents: 0
diff changeset
350 # endif
kono
parents: 0
diff changeset
351
kono
parents: 0
diff changeset
352 static _Noreturn void
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 print_and_abort (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 {
111
kono
parents: 0
diff changeset
355 /* Don't change any of these strings. Yes, it would be possible to add
kono
parents: 0
diff changeset
356 the newline to the string and use fputs or so. But this must not
kono
parents: 0
diff changeset
357 happen because the "memory exhausted" message appears in other places
kono
parents: 0
diff changeset
358 like this and the translation should be reused instead of creating
kono
parents: 0
diff changeset
359 a very similar string which requires a separate translation. */
kono
parents: 0
diff changeset
360 # ifdef _LIBC
kono
parents: 0
diff changeset
361 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
kono
parents: 0
diff changeset
362 # else
kono
parents: 0
diff changeset
363 fprintf (stderr, "%s\n", _("memory exhausted"));
kono
parents: 0
diff changeset
364 # endif
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 exit (obstack_exit_failure);
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367
111
kono
parents: 0
diff changeset
368 /* The functions allocating more room by calling 'obstack_chunk_alloc'
kono
parents: 0
diff changeset
369 jump to the handler pointed to by 'obstack_alloc_failed_handler'.
kono
parents: 0
diff changeset
370 This can be set to a user defined function which should either
kono
parents: 0
diff changeset
371 abort gracefully or use longjump - but shouldn't return. This
kono
parents: 0
diff changeset
372 variable by default points to the internal function
kono
parents: 0
diff changeset
373 'print_and_abort'. */
kono
parents: 0
diff changeset
374 void (*obstack_alloc_failed_handler) (void) = print_and_abort;
kono
parents: 0
diff changeset
375 # endif /* !_OBSTACK_NO_ERROR_HANDLER */
kono
parents: 0
diff changeset
376 #endif /* !_OBSTACK_ELIDE_CODE */