| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifdef _LIBC |
| 21 |
# include <obstack.h> |
| 22 |
#else |
| 23 |
# include <config.h> |
| 24 |
# include "obstack.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#if !defined _LIBC && defined __GNU_LIBRARY__ && __GNU_LIBRARY__ > 1 |
| 40 |
# include <gnu-versions.h> |
| 41 |
# if (_GNU_OBSTACK_INTERFACE_VERSION == _OBSTACK_INTERFACE_VERSION \ |
| 42 |
|| (_GNU_OBSTACK_INTERFACE_VERSION == 1 \ |
| 43 |
&& _OBSTACK_INTERFACE_VERSION == 2 \ |
| 44 |
&& defined SIZEOF_INT && defined SIZEOF_SIZE_T \ |
| 45 |
&& SIZEOF_INT == SIZEOF_SIZE_T)) |
| 46 |
# define _OBSTACK_ELIDE_CODE |
| 47 |
# endif |
| 48 |
#endif |
| 49 |
|
| 50 |
#ifndef _OBSTACK_ELIDE_CODE |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
# if !defined __GNUC__ && !defined __alignof__ |
| 55 |
# include <alignof.h> |
| 56 |
# define __alignof__(type) alignof_type (type) |
| 57 |
# endif |
| 58 |
# include <stdlib.h> |
| 59 |
# include <stdint.h> |
| 60 |
|
| 61 |
# ifndef MAX |
| 62 |
# define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 63 |
# endif |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \ |
| 73 |
MAX (__alignof__ (uintmax_t), \ |
| 74 |
__alignof__ (void *))) |
| 75 |
#define DEFAULT_ROUNDING MAX (sizeof (long double), \ |
| 76 |
MAX (sizeof (uintmax_t), \ |
| 77 |
sizeof (void *))) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static void * |
| 84 |
call_chunkfun (struct obstack *h, size_t size) |
| 85 |
{ |
| 86 |
if (h->use_extra_arg) |
| 87 |
return h->chunkfun.extra (h->extra_arg, size); |
| 88 |
else |
| 89 |
return h->chunkfun.plain (size); |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
call_freefun (struct obstack *h, void *old_chunk) |
| 94 |
{ |
| 95 |
if (h->use_extra_arg) |
| 96 |
h->freefun.extra (h->extra_arg, old_chunk); |
| 97 |
else |
| 98 |
h->freefun.plain (old_chunk); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static int |
| 109 |
_obstack_begin_worker (struct obstack *h, |
| 110 |
_OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment) |
| 111 |
{ |
| 112 |
struct _obstack_chunk *chunk; |
| 113 |
|
| 114 |
if (alignment == 0) |
| 115 |
alignment = DEFAULT_ALIGNMENT; |
| 116 |
if (size == 0) |
| 117 |
|
| 118 |
{ |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1)) |
| 128 |
+ 4 + DEFAULT_ROUNDING - 1) |
| 129 |
& ~(DEFAULT_ROUNDING - 1)); |
| 130 |
size = 4096 - extra; |
| 131 |
} |
| 132 |
|
| 133 |
h->chunk_size = size; |
| 134 |
h->alignment_mask = alignment - 1; |
| 135 |
|
| 136 |
chunk = h->chunk = call_chunkfun (h, h->chunk_size); |
| 137 |
if (!chunk) |
| 138 |
(*obstack_alloc_failed_handler) (); |
| 139 |
h->next_free = h->object_base = __PTR_ALIGN ((char *) chunk, chunk->contents, |
| 140 |
alignment - 1); |
| 141 |
h->chunk_limit = chunk->limit = (char *) chunk + h->chunk_size; |
| 142 |
chunk->prev = 0; |
| 143 |
|
| 144 |
h->maybe_empty_object = 0; |
| 145 |
h->alloc_failed = 0; |
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
int |
| 150 |
_obstack_begin (struct obstack *h, |
| 151 |
_OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment, |
| 152 |
void *(*chunkfun) (size_t), |
| 153 |
void (*freefun) (void *)) |
| 154 |
{ |
| 155 |
h->chunkfun.plain = chunkfun; |
| 156 |
h->freefun.plain = freefun; |
| 157 |
h->use_extra_arg = 0; |
| 158 |
return _obstack_begin_worker (h, size, alignment); |
| 159 |
} |
| 160 |
|
| 161 |
int |
| 162 |
_obstack_begin_1 (struct obstack *h, |
| 163 |
_OBSTACK_SIZE_T size, _OBSTACK_SIZE_T alignment, |
| 164 |
void *(*chunkfun) (void *, size_t), |
| 165 |
void (*freefun) (void *, void *), |
| 166 |
void *arg) |
| 167 |
{ |
| 168 |
h->chunkfun.extra = chunkfun; |
| 169 |
h->freefun.extra = freefun; |
| 170 |
h->extra_arg = arg; |
| 171 |
h->use_extra_arg = 1; |
| 172 |
return _obstack_begin_worker (h, size, alignment); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
void |
| 182 |
_obstack_newchunk (struct obstack *h, _OBSTACK_SIZE_T length) |
| 183 |
{ |
| 184 |
struct _obstack_chunk *old_chunk = h->chunk; |
| 185 |
struct _obstack_chunk *new_chunk = 0; |
| 186 |
size_t obj_size = h->next_free - h->object_base; |
| 187 |
char *object_base; |
| 188 |
|
| 189 |
|
| 190 |
size_t sum1 = obj_size + length; |
| 191 |
size_t sum2 = sum1 + h->alignment_mask; |
| 192 |
size_t new_size = sum2 + (obj_size >> 3) + 100; |
| 193 |
if (new_size < sum2) |
| 194 |
new_size = sum2; |
| 195 |
if (new_size < h->chunk_size) |
| 196 |
new_size = h->chunk_size; |
| 197 |
|
| 198 |
|
| 199 |
if (obj_size <= sum1 && sum1 <= sum2) |
| 200 |
new_chunk = call_chunkfun (h, new_size); |
| 201 |
if (!new_chunk) |
| 202 |
(*obstack_alloc_failed_handler)(); |
| 203 |
h->chunk = new_chunk; |
| 204 |
new_chunk->prev = old_chunk; |
| 205 |
new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size; |
| 206 |
|
| 207 |
|
| 208 |
object_base = |
| 209 |
__PTR_ALIGN ((char *) new_chunk, new_chunk->contents, h->alignment_mask); |
| 210 |
|
| 211 |
|
| 212 |
memcpy (object_base, h->object_base, obj_size); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if (!h->maybe_empty_object |
| 218 |
&& (h->object_base |
| 219 |
== __PTR_ALIGN ((char *) old_chunk, old_chunk->contents, |
| 220 |
h->alignment_mask))) |
| 221 |
{ |
| 222 |
new_chunk->prev = old_chunk->prev; |
| 223 |
call_freefun (h, old_chunk); |
| 224 |
} |
| 225 |
|
| 226 |
h->object_base = object_base; |
| 227 |
h->next_free = h->object_base + obj_size; |
| 228 |
|
| 229 |
h->maybe_empty_object = 0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int _obstack_allocated_p (struct obstack *h, void *obj) __attribute_pure__; |
| 239 |
|
| 240 |
int |
| 241 |
_obstack_allocated_p (struct obstack *h, void *obj) |
| 242 |
{ |
| 243 |
struct _obstack_chunk *lp; |
| 244 |
struct _obstack_chunk *plp; |
| 245 |
|
| 246 |
lp = (h)->chunk; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
| 251 |
{ |
| 252 |
plp = lp->prev; |
| 253 |
lp = plp; |
| 254 |
} |
| 255 |
return lp != 0; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
void |
| 262 |
_obstack_free (struct obstack *h, void *obj) |
| 263 |
{ |
| 264 |
struct _obstack_chunk *lp; |
| 265 |
struct _obstack_chunk *plp; |
| 266 |
|
| 267 |
lp = h->chunk; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj)) |
| 272 |
{ |
| 273 |
plp = lp->prev; |
| 274 |
call_freefun (h, lp); |
| 275 |
lp = plp; |
| 276 |
|
| 277 |
|
| 278 |
h->maybe_empty_object = 1; |
| 279 |
} |
| 280 |
if (lp) |
| 281 |
{ |
| 282 |
h->object_base = h->next_free = (char *) (obj); |
| 283 |
h->chunk_limit = lp->limit; |
| 284 |
h->chunk = lp; |
| 285 |
} |
| 286 |
else if (obj != 0) |
| 287 |
|
| 288 |
abort (); |
| 289 |
} |
| 290 |
|
| 291 |
_OBSTACK_SIZE_T |
| 292 |
_obstack_memory_used (struct obstack *h) |
| 293 |
{ |
| 294 |
struct _obstack_chunk *lp; |
| 295 |
_OBSTACK_SIZE_T nbytes = 0; |
| 296 |
|
| 297 |
for (lp = h->chunk; lp != 0; lp = lp->prev) |
| 298 |
{ |
| 299 |
nbytes += lp->limit - (char *) lp; |
| 300 |
} |
| 301 |
return nbytes; |
| 302 |
} |
| 303 |
|
| 304 |
# ifndef _OBSTACK_NO_ERROR_HANDLER |
| 305 |
|
| 306 |
# include <stdio.h> |
| 307 |
|
| 308 |
|
| 309 |
# ifdef _LIBC |
| 310 |
int obstack_exit_failure = EXIT_FAILURE; |
| 311 |
# else |
| 312 |
# include "exitfail.h" |
| 313 |
# define obstack_exit_failure exit_failure |
| 314 |
# endif |
| 315 |
|
| 316 |
# ifdef _LIBC |
| 317 |
# include <libintl.h> |
| 318 |
# else |
| 319 |
# include "gettext.h" |
| 320 |
# endif |
| 321 |
# ifndef _ |
| 322 |
# define _(msgid) gettext (msgid) |
| 323 |
# endif |
| 324 |
|
| 325 |
# ifdef _LIBC |
| 326 |
# include <libio/iolibio.h> |
| 327 |
# endif |
| 328 |
|
| 329 |
static _Noreturn void |
| 330 |
print_and_abort (void) |
| 331 |
{ |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
# ifdef _LIBC |
| 338 |
(void) __fxprintf (NULL, "%s\n", _("memory exhausted")); |
| 339 |
# else |
| 340 |
fprintf (stderr, "%s\n", _("memory exhausted")); |
| 341 |
# endif |
| 342 |
exit (obstack_exit_failure); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
__attribute_noreturn__ void (*obstack_alloc_failed_handler) (void) |
| 352 |
= print_and_abort; |
| 353 |
# endif |
| 354 |
#endif |