home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZVMEM.C < prev   
C/C++ Source or Header  |  1992-07-17  |  6KB  |  190 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zvmem.c */
  21. /* "Virtual memory" operators for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "alloc.h"
  26. #include "estack.h"            /* for checking in restore */
  27. #include "dict.h"            /* ditto */
  28. #include "dstack.h"
  29. #include "save.h"
  30. #include "state.h"
  31. #include "store.h"
  32. #include "gsmatrix.h"            /* for gsstate.h */
  33. #include "gsstate.h"
  34.  
  35. /* Imported operators */
  36. extern int zgsave(P1(os_ptr));
  37. extern int zgrestore(P1(os_ptr));
  38.  
  39. /* Import the routine for setting the attributes of the identity matrix. */
  40. extern void init_identity_matrix(P0());
  41.  
  42. /* 'Save' structure */
  43. typedef struct vm_save_s vm_save;
  44. struct vm_save_s {
  45.     alloc_save *asave;        /* allocator save */
  46.     gs_state *gsave;        /* old graphics state */
  47. };
  48.  
  49. /* save */
  50. int
  51. zsave(register os_ptr op)
  52. {    vm_save *vmsave = (vm_save *)alloc(1, sizeof(vm_save), "zsave");
  53.     alloc_save *asave;
  54.     int code;
  55.     gs_state *prev, *prev2;
  56.     if ( vmsave == 0 ) return_error(e_VMerror);
  57.     asave = alloc_save_state();
  58.     if ( asave == 0 )
  59.        {    alloc_free((char *)vmsave, 1, sizeof(vm_save), "zsave");
  60.         return_error(e_VMerror);
  61.        }
  62.     vmsave->asave = asave;
  63.     code = zgsave(op);
  64.     if ( code < 0 ) return code;
  65.     /* Swap the contents of the old and new states, */
  66.     /* so the new state points to the newly allocated components. */
  67.     /* Cut the chains so we can't grestore past here. */
  68.     prev = gs_state_swap_saved(igs, (gs_state *)0);
  69.     prev2 = gs_state_swap_saved(prev, (gs_state *)0);
  70.     gs_state_swap_saved(igs, prev2);
  71.     gs_state_swap(igs, prev);
  72.     vmsave->gsave = igs;
  73.     igs = prev;
  74.     push(1);
  75.     make_tv(op, t_save, psave, vmsave);
  76.     init_identity_matrix();        /* update l_new attribute */
  77.     return zgsave(op);
  78. }
  79.  
  80. /* restore */
  81. private int restore_check_stack(P3(const ref *, const ref *, const alloc_save *));
  82. private void restore_fix_stack(P3(ref *, ref *, const alloc_save *));
  83. int
  84. zrestore(register os_ptr op)
  85. {    vm_save *vmsave;
  86.     alloc_save *asave;
  87.     check_type(*op, t_save);
  88.     vmsave = op->value.psave;
  89.     if ( vmsave == 0 )        /* invalidated save */
  90.         return_error(e_invalidrestore);
  91.     asave = vmsave->asave;
  92.     /* Check the contents of the stacks. */
  93.        {    int code;
  94.         if ( (code = restore_check_stack(osbot, op, asave)) < 0 ||
  95.              (code = restore_check_stack(esbot, esp + 1, asave)) < 0 ||
  96.              (code = restore_check_stack(dstack, dsp + 1, asave)) < 0
  97.            )
  98.             return code;
  99.        }
  100.     if ( alloc_restore_state_check(asave) < 0 )
  101.         return_error(e_invalidrestore);
  102.     /* Invalidate any other copies of this save object on the stacks, */
  103.     /* and reset l_new in all stack entries if the new restore level */
  104.     /* is zero. */
  105.     restore_fix_stack(osbot, op, asave);
  106.     restore_fix_stack(esbot, esp + 1, asave);
  107.     restore_fix_stack(dstack, dsp + 1, asave);
  108.     /* Now it's safe to restore the state of memory. */
  109.     alloc_restore_state(asave);
  110.     /* Restore the graphics state. */
  111.     igs = vmsave->gsave;
  112.     istate = *(int_gstate *)gs_state_client_data(igs);
  113.     alloc_free((char *)vmsave, 1, sizeof(vm_save), "zrestore");
  114.     pop(1);
  115.     init_identity_matrix();        /* update l_new attribute */
  116.     return 0;
  117. }
  118. /* Check a stack to make sure all its elements are older than a save. */
  119. private int
  120. restore_check_stack(const ref *bot, const ref *top, const alloc_save *asave)
  121. {    const ref *stkp;
  122.     for ( stkp = bot; stkp < top; stkp++ )
  123.        {    char *ptr;
  124.         switch ( r_type(stkp) )
  125.            {
  126.         case t_array: ptr = (char *)stkp->value.refs; break;
  127.         case t_condition: ptr = (char *)stkp->value.pcond; break;
  128.         case t_dictionary: ptr = (char *)stkp->value.pdict; break;
  129.         case t_fontID: ptr = (char *)stkp->value.pfont; break;
  130.         case t_gstate: ptr = (char *)stkp->value.pgstate; break;
  131.         /* case t_file: ****** WHAT? ****** */
  132.         case t_lock: ptr = (char *)stkp->value.plock; break;
  133.         case t_name:
  134.             /* Names are special because of how they are allocated. */
  135.             if ( alloc_name_is_since_save(stkp, asave) )
  136.                 return_error(e_invalidrestore);
  137.             continue;
  138.         case t_save: ptr = (char *)stkp->value.psave; break;
  139.         case t_string: ptr = (char *)stkp->value.bytes; break;
  140.         case t_mixedarray: case t_shortarray:
  141.             ptr = (char *)stkp->value.packed; break;
  142.         case t_device: ptr = (char *)stkp->value.pdevice; break;
  143.         default: continue;
  144.            }
  145.         if ( alloc_is_since_save(ptr, asave) )
  146.             return_error(e_invalidrestore);
  147.        }
  148.  
  149.     return 0;            /* OK */
  150. }
  151. /* Fix up the contents of a stack by invalidating */
  152. /* any save objects newer than the save being restored, */
  153. /* and, if the new save level is zero, clearing the l_new */
  154. /* bit in all the entries (since we can't tolerate values with */
  155. /* l_new set if the save level is zero). */
  156. private void
  157. restore_fix_stack(ref *bot, ref *top, const alloc_save *asave)
  158. {    ref *stkp;
  159.     for ( stkp = bot; stkp < top; stkp++ )
  160.        {    if ( r_type(stkp) == t_save &&
  161.              stkp->value.psave != 0 &&
  162.              (stkp->value.psave->asave == asave ||
  163.               alloc_is_since_save((const char *)stkp->value.psave, asave))
  164.            )
  165.             stkp->value.psave = 0;
  166.         r_clear_attrs(stkp, l_new);    /* always do it, no harm */
  167.        }
  168. }
  169.  
  170. /* vmstatus */
  171. int
  172. zvmstatus(register os_ptr op)
  173. {    long used, total;
  174.     alloc_status(&used, &total);
  175.     push(3);
  176.     make_int(op - 2, alloc_save_level());
  177.     make_int(op - 1, used);
  178.     make_int(op, total);
  179.     return 0;
  180. }
  181.  
  182. /* ------ Initialization procedure ------ */
  183.  
  184. op_def zvmem_op_defs[] = {
  185.     {"1restore", zrestore},
  186.     {"0save", zsave},
  187.     {"0vmstatus", zvmstatus},
  188.     op_def_end(0)
  189. };
  190.