1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/* Copyright (C) 1992, 1995 Aladdin Enterprises. All rights reserved.
This file is part of GNU Ghostscript.
GNU Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU Ghostscript General Public License for full details.
*/
/* istack.c */
/* Ghostscript expandable stack manager */
#include "memory_.h"
#include "ghost.h"
#include "gsstruct.h"
#include "gsutil.h"
#include "errors.h"
#include "ialloc.h"
#include "istack.h"
#include "istruct.h" /* for gs_reloc_refs */
#include "iutil.h"
#include "ivmspace.h" /* for local/global test */
#include "store.h"
/* Forward references */
private void init_block(P3(ref_stack *, ref *, uint));
int ref_stack_push_block(P3(ref_stack *, uint, uint));
/* GC procedures */
#define sptr ((ref_stack *)vptr)
private CLEAR_MARKS_PROC(ref_stack_clear_marks) {
r_clear_attrs(&sptr->current, l_mark);
}
private ENUM_PTRS_BEGIN(ref_stack_enum_ptrs) return 0;
case 0:
*pep = &sptr->current;
return ptr_ref_type;
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(ref_stack_reloc_ptrs) {
#if stacks_are_segmented
/* In a segmented environment, the top block can't move, */
/* so we don't need to relocate pointers to it. */
#else
/* Note that the relocation must be a multiple of sizeof(ref_packed) */
/* * align_packed_per_ref, but it need not be a multiple of */
/* sizeof(ref). Therefore, we must do the adjustments using */
/* ref_packed pointers rather than ref pointers. */
ref_packed *bot = (ref_packed *)sptr->current.value.refs;
long reloc;
gs_reloc_refs((ref_packed *)&sptr->current,
(ref_packed *)(&sptr->current + 1),
gcst);
r_clear_attrs(&sptr->current, l_mark);
reloc = bot - (ref_packed *)sptr->current.value.refs;
#define reloc_p(p)\
sptr->p = (ref *)((ref_packed *)sptr->p - reloc);
reloc_p(p);
reloc_p(bot);
reloc_p(top);
#undef reloc_p
#endif
} RELOC_PTRS_END
/* Structure type for a ref_stack. */
public_st_ref_stack();
/* Initialize a stack. */
void
ref_stack_init(register ref_stack *pstack, ref *psb,
uint bot_guard, uint top_guard, ref *pguard, gs_ref_memory_t *mem)
{ uint size = r_size(psb);
uint avail = size - (stack_block_refs + bot_guard + top_guard);
ref_stack_block *pblock = (ref_stack_block *)psb->value.refs;
s_ptr body = (s_ptr)(pblock + 1);
pstack->bot = body + bot_guard;
pstack->p = pstack->bot - 1;
pstack->top = pstack->p + avail;
pstack->current = *psb;
pstack->extension_size = 0;
pstack->extension_used = 0;
make_int(&pstack->max_stack, avail);
pstack->requested = 0;
pstack->bot_guard = bot_guard;
pstack->top_guard = top_guard;
pstack->block_size = size - segmented_guard(bot_guard + top_guard);
pstack->body_size = avail;
if ( pguard != 0 )
pstack->guard_value = *pguard;
else
make_tav(&pstack->guard_value, t__invalid, 0, intval, 0);
pstack->underflow_error = -1; /* bogus, caller must set */
pstack->overflow_error = -1; /* bogus, caller must set */
pstack->allow_expansion = true; /* default, caller may reset */
pstack->memory = mem;
init_block(pstack, psb, 0);
refset_null(pstack->bot, avail);
make_empty_array(&pblock->next, 0);
}
/* Set the maximum number of elements allowed on a stack. */
int
ref_stack_set_max_count(ref_stack *pstack, long nmax)
{ uint nmin = pstack->extension_size + (pstack->top - pstack->bot + 1);
if ( nmax < nmin )
nmax = nmin;
if ( nmax > max_uint / sizeof(ref) )
nmax = max_uint / sizeof(ref);
if ( !pstack->allow_expansion )
{ uint ncur = pstack->body_size;
if ( nmax > ncur )
nmax = ncur;
}
pstack->max_stack.value.intval = nmax;
return 0;
}
/* Return the number of elements on a stack. */
uint
ref_stack_count(const ref_stack *pstack)
{ return pstack->extension_used + (pstack->p - pstack->bot + 1);
}
/* Retrieve a given element from the stack, counting from */
/* 0 as the top element. */
ref *
ref_stack_index(const ref_stack *pstack, long idx)
{ ref_stack_block *pblock;
uint used = pstack->p + 1 - pstack->bot;
if ( idx < 0 )
return NULL;
if ( idx < used ) /* common case */
return pstack->p - (uint)idx;
pblock = (ref_stack_block *)pstack->current.value.refs;
do
{ pblock = (ref_stack_block *)pblock->next.value.refs;
if ( pblock == 0 )
return NULL;
idx -= used;
used = r_size(&pblock->used);
}
while ( idx >= used );
return pblock->used.value.refs + (used - 1 - (uint)idx);
}
/* Count the number of elements down to and including the first mark. */
/* If no mark is found, return 0. */
uint
ref_stack_counttomark(const ref_stack *pstack)
{ uint scanned = 0;
STACK_LOOP_BEGIN(pstack, p, used)
{ uint count = used;
p += used - 1;
for ( ; count; count--, p-- )
if ( r_has_type(p, t_mark) )
return scanned + (used - count + 1);
scanned += used;
}
STACK_LOOP_END(p, used)
return 0;
}
/* Store the top elements of a stack into an array, */
/* with or without store/undo checking. */
/* May return e_rangecheck or e_invalidaccess. */
int
ref_stack_store(const ref_stack *pstack, ref *parray, uint count, uint skip,
int age, bool check, client_name_t cname)
{ uint left, pass;
ref *to;
uint space = r_space(parray);
if ( count > ref_stack_count(pstack) || count > r_size(parray) )
return_error(e_rangecheck);
if ( check && space != avm_local )
{ /* Pre-check the elements being stored. */
left = count, pass = skip;
STACK_LOOP_BEGIN(pstack, ptr, size)
if ( size <= pass )
pass -= size;
else
{ int code;
if ( pass != 0 )
size -= pass, pass = 0;
ptr += size;
if ( size > left )
size = left;
left -= size;
code = refs_check_space(ptr - size, size, space);
if ( code < 0 )
return code;
if ( left == 0 )
break;
}
STACK_LOOP_END(ptr, size)
}
to = parray->value.refs + count;
left = count, pass = skip;
STACK_LOOP_BEGIN(pstack, from, size)
if ( size <= pass )
pass -= size;
else
{ if ( pass != 0 )
size -= pass, pass = 0;
from += size;
if ( size > left )
size = left;
left -= size;
switch ( age )
{
case -1: /* not an array */
while ( size-- )
{ from--, to--;
ref_assign(to, from);
}
break;
case 0: /* old array */
while ( size-- )
{ from--, to--;
ref_assign_old(parray, to, from, cname);
}
break;
case 1: /* new array */
while ( size-- )
{ from--, to--;
ref_assign_new(to, from);
}
break;
}
if ( left == 0 )
break;
}
STACK_LOOP_END(from, size)
r_set_size(parray, count);
return 0;
}
/* Pop a given number of elements off a stack. */
/* The number must not exceed the number of elements in use. */
void
ref_stack_pop(register ref_stack *pstack, uint count)
{ uint used;
while ( (used = pstack->p + 1 - pstack->bot) < count )
{ count -= used;
pstack->p = pstack->bot - 1;
ref_stack_pop_block(pstack);
}
pstack->p -= count;
}
/* Pop the top block off a stack. */
int
ref_stack_pop_block(register ref_stack *pstack)
{ s_ptr bot = pstack->bot;
uint count = pstack->p + 1 - bot;
ref_stack_block *pcur =
(ref_stack_block *)pstack->current.value.refs;
register ref_stack_block *pnext =
(ref_stack_block *)pcur->next.value.refs;
uint used;
ref *body;
ref next;
if ( pnext == 0 )
return_error(pstack->underflow_error);
used = r_size(&pnext->used);
body = (ref *)(pnext + 1) + flat_guard(pstack->bot_guard);
next = pcur->next;
/*
* If we're on a segmented system, the top block does not move,
* so we move up the used part of the top block, copy the contents
* of the next block under it, and free the next block.
* We also do this on non-segmented systems if the contents of the
* two blocks won't fit in a single block; in this case we copy up
* as much as will fit. On non-segmented systems where the contents
* of both blocks fit in a single block, we copy the used part
* of the top block to the top of the next block, and free
* the top block.
*/
if ( used + count > pstack->body_size )
{ /* Move as much into the top block as will fit. */
uint moved = pstack->body_size - count;
uint left;
if ( moved == 0 )
return_error(e_Fatal);
memmove(bot + moved, bot, count * sizeof(ref));
left = used - moved;
memcpy(bot, body + left, moved * sizeof(ref));
refset_null(body + left, moved);
r_dec_size(&pnext->used, moved);
pstack->p = pstack->top;
pstack->extension_used -= moved;
}
else
{
#if stacks_are_segmented
/* We know there are no guard elements in the next block. */
memmove(bot + used, bot, count * sizeof(ref));
memcpy(bot, body, used * sizeof(ref));
pcur->next = pnext->next;
gs_free_ref_array(pstack->memory, &next, "ref_stack_pop_block");
#else
memcpy(body + used, bot, count * sizeof(ref));
pstack->bot = bot = body;
pstack->top = bot + pstack->body_size - 1;
gs_free_ref_array(pstack->memory, &pstack->current,
"ref_stack_pop_block");
pstack->current = next;
#endif
pstack->p = bot + (used + count - 1);
pstack->extension_size -= pstack->body_size;
pstack->extension_used -= used;
}
return 0;
}
/* Extend a stack to recover from an overflow condition. */
/* May return overflow_error or e_VMerror. */
int
ref_stack_extend(ref_stack *pstack, uint request)
{ uint keep = (pstack->top - pstack->bot + 1) / 3;
if ( pstack->p < pstack->bot )
{ /* Adding another block can't help things. */
return_error(pstack->overflow_error);
}
if ( keep + request > pstack->body_size )
keep = pstack->body_size - request;
return ref_stack_push_block(pstack, keep, request);
}
/* Push N empty slots onto a stack. These slots are not initialized; */
/* the caller must fill them immediately. May return overflow_error */
/* (if max_stack would be exceeded, or the stack has no allocator) */
/* or e_VMerror. */
int
ref_stack_push(register ref_stack *pstack, uint count)
{ /* Don't bother to pre-check for overflow: we must be able to */
/* back out in the case of a VMerror anyway, and */
/* ref_stack_push_block will make the check itself. */
uint needed = count;
uint added;
for ( ; (added = pstack->top - pstack->p) < needed; needed -= added )
{ int code;
pstack->p = pstack->top;
code =
ref_stack_push_block(pstack,
(pstack->top - pstack->bot + 1) / 3,
count);
if ( code < 0 )
{ /* Back out. */
ref_stack_pop(pstack, count - needed);
pstack->requested = count;
return code;
}
}
pstack->p += needed;
return 0;
}
/* Push a block onto the stack, specifying how many elements of */
/* the current top block should remain in the top block and also */
/* how many elements we are trying to add. */
/* May return overflow_error or e_VMerror. */
/* Must have keep <= count. */
int
ref_stack_push_block(register ref_stack *pstack, uint keep, uint add)
{ uint count = pstack->p - pstack->bot + 1;
uint move = count - keep;
ref_stack_block *pcur = (ref_stack_block *)pstack->current.value.refs;
ref next;
ref_stack_block *pnext;
ref *body;
int code;
if ( keep > count )
return_error(e_Fatal);
/* Check for overflowing the maximum size, */
/* or expansion not allowed. */
if ( pstack->memory == 0 ||
pstack->extension_used + (pstack->top - pstack->bot) + add >=
pstack->max_stack.value.intval ||
!pstack->allow_expansion
)
return_error(pstack->overflow_error);
code = gs_alloc_ref_array(pstack->memory, &next, 0,
pstack->block_size, "ref_stack_push_block");
if ( code < 0 )
return code;
pnext = (ref_stack_block *)next.value.refs;
body = (ref *)(pnext + 1);
#if stacks_are_segmented
/* Copy all but the top keep elements into the new block, */
/* and move the top elements down. */
/* We know there are no guard elements in the new block. */
memcpy(body, pstack->bot, move * sizeof(ref));
/* Clear the elements above the top of the new block. */
refset_null(body + move, pstack->body_size - move);
if ( keep <= move )
{ /* No overlap, memcpy is safe. */
memcpy(pstack->bot, pstack->bot + move, keep * sizeof(ref));
}
else
{ uint i;
s_ptr bot = pstack->bot;
s_ptr up = bot + move;
for ( i = 0; i < keep; i++ )
bot[i] = up[i];
}
pnext->next = pcur->next;
pnext->used = next;
pcur->next = next;
pnext->used.value.refs = body;
r_set_size(&pnext->used, move);
#else
/* Copy the top keep elements into the new block, */
/* and make the new block the top block. */
init_block(pstack, &next, keep);
body += pstack->bot_guard;
memcpy(body, pstack->bot + move, keep * sizeof(ref));
/* Clear the elements above the top of the new block. */
refset_null(body + keep, pstack->body_size - keep);
/* Clear the elements above the top of the old block. */
refset_null(pstack->bot + move, keep);
pnext->next = pstack->current;
pcur->used.value.refs = pstack->bot;
r_set_size(&pcur->used, move);
pstack->current = next;
pstack->bot = body;
pstack->top = pstack->bot + pstack->body_size - 1;
#endif
pstack->p = pstack->bot + keep - 1;
pstack->extension_size += pstack->body_size;
pstack->extension_used += move;
return 0;
}
/* Clean up a stack for garbage collection. */
void
ref_stack_cleanup(ref_stack *pstack)
{ ref_stack_block *pblock =
(ref_stack_block *)pstack->current.value.refs;
refset_null(pstack->p + 1, pstack->top - pstack->p);
pblock->used = pstack->current; /* set attrs */
pblock->used.value.refs = pstack->bot;
r_set_size(&pblock->used, pstack->p + 1 - pstack->bot);
}
/* ------ Internal routines ------ */
/* Initialize the guards and body of a stack block. */
/* Note that this always initializes the guards, so it should not be used */
/* for extension blocks in a segmented environments. */
private void
init_block(ref_stack *pstack, ref *psb, uint used)
{ ref *brefs = psb->value.refs;
#define pblock ((ref_stack_block *)brefs)
register uint i;
register ref *p;
for ( i = pstack->bot_guard, p = brefs + stack_block_refs;
i != 0; i--, p++
)
ref_assign(p, &pstack->guard_value);
/* The top guard elements will never be read, */
/* but we need to initialize them for the sake of the GC. */
/* We can use refset_null for this, because even though it uses */
/* make_null_new and stack elements must not be marked new, */
/* these slots will never actually be read or written. */
if ( pstack->top_guard )
{ ref *top = brefs + r_size(psb);
int top_guard = pstack->top_guard;
refset_null(top - top_guard, top_guard);
}
pblock->used = *psb;
pblock->used.value.refs = brefs + stack_block_refs + pstack->bot_guard;
r_set_size(&pblock->used, 0);
}
|