home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ialloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.8 KB  |  358 lines

  1. /* Copyright (C) 1993, 1995, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ialloc.c,v 1.2 2000/09/19 19:00:41 lpd Exp $ */
  20. /* Memory allocator for Ghostscript interpreter */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "errors.h"
  24. #include "gsstruct.h"
  25. #include "iref.h"        /* must precede iastate.h */
  26. #include "iastate.h"
  27. #include "igc.h"        /* for gs_gc_reclaim */
  28. #include "ipacked.h"
  29. #include "iutil.h"
  30. #include "ivmspace.h"
  31. #include "store.h"
  32.  
  33. /*
  34.  * Define global and local instances.
  35.  */
  36. public_st_gs_dual_memory();
  37.  
  38. /* Initialize the allocator */
  39. int
  40. ialloc_init(gs_dual_memory_t *dmem, gs_raw_memory_t * rmem, uint chunk_size,
  41.         bool level2)
  42. {
  43.     gs_ref_memory_t *ilmem = ialloc_alloc_state(rmem, chunk_size);
  44.     gs_ref_memory_t *ilmem_stable = ialloc_alloc_state(rmem, chunk_size);
  45.     gs_ref_memory_t *igmem = 0;
  46.     gs_ref_memory_t *igmem_stable = 0;
  47.     gs_ref_memory_t *ismem = ialloc_alloc_state(rmem, chunk_size);
  48.     int i;
  49.  
  50.     if (ilmem == 0 || ilmem_stable == 0 || ismem == 0)
  51.     goto fail;
  52.     ilmem->stable_memory = (gs_memory_t *)ilmem_stable;
  53.     if (level2) {
  54.     igmem = ialloc_alloc_state(rmem, chunk_size);
  55.     igmem_stable = ialloc_alloc_state(rmem, chunk_size);
  56.     if (igmem == 0 || igmem_stable == 0)
  57.         goto fail;
  58.     igmem->stable_memory = (gs_memory_t *)igmem_stable;
  59.     } else
  60.     igmem = ilmem, igmem_stable = ilmem_stable;
  61.     for (i = 0; i < countof(dmem->spaces_indexed); i++)
  62.     dmem->spaces_indexed[i] = 0;
  63.     dmem->space_local = ilmem;
  64.     dmem->space_global = igmem;
  65.     dmem->space_system = ismem;
  66.     dmem->spaces.vm_reclaim = gs_gc_reclaim; /* real GC */
  67.     dmem->reclaim = 0;        /* no interpreter GC yet */
  68.     /* Level 1 systems have only local VM. */
  69.     igmem->space = avm_global;
  70.     igmem_stable->space = avm_global;
  71.     ilmem->space = avm_local;    /* overrides if ilmem == igmem */
  72.     ilmem_stable->space = avm_local; /* ditto */
  73.     ismem->space = avm_system;
  74.     ialloc_set_space(dmem, avm_global);
  75.     return 0;
  76.  fail:
  77.     gs_free_object(rmem, igmem_stable, "ialloc_init failure");
  78.     gs_free_object(rmem, igmem, "ialloc_init failure");
  79.     gs_free_object(rmem, ismem, "ialloc_init failure");
  80.     gs_free_object(rmem, ilmem_stable, "ialloc_init failure");
  81.     gs_free_object(rmem, ilmem, "ialloc_init failure");
  82.     return_error(e_VMerror);
  83. }
  84.  
  85. /* ================ Local/global VM ================ */
  86.  
  87. /* Get the space attribute of an allocator */
  88. uint
  89. imemory_space(const gs_ref_memory_t * iimem)
  90. {
  91.     return iimem->space;
  92. }
  93.  
  94. /* Select the allocation space. */
  95. void
  96. ialloc_set_space(gs_dual_memory_t * dmem, uint space)
  97. {
  98.     gs_ref_memory_t *mem = dmem->spaces_indexed[space >> r_space_shift];
  99.  
  100.     dmem->current = mem;
  101.     dmem->current_space = mem->space;
  102. }
  103.  
  104. /* Get the l_new attribute of a current allocator. */
  105. /* (A copy of the new_mask in the gs_dual_memory_t.) */
  106. uint
  107. imemory_new_mask(const gs_ref_memory_t *imem)
  108. {
  109.     return imem->new_mask;
  110. }
  111.  
  112. /* Get the save level of an allocator. */
  113. int
  114. imemory_save_level(const gs_ref_memory_t *imem)
  115. {
  116.     return imem->save_level;
  117. }
  118.  
  119. /* Reset the requests. */
  120. void
  121. ialloc_reset_requested(gs_dual_memory_t * dmem)
  122. {
  123.     dmem->space_system->gc_status.requested = 0;
  124.     dmem->space_global->gc_status.requested = 0;
  125.     dmem->space_local->gc_status.requested = 0;
  126. }
  127.  
  128. /* ================ Refs ================ */
  129.  
  130. #ifdef DEBUG
  131. private int
  132. ialloc_trace_space(const gs_ref_memory_t *imem)
  133. {
  134.     return imem->space + (imem->stable_memory == (gs_memory_t *)imem);
  135. }
  136. #endif
  137.  
  138. /* Register a ref root. */
  139. int
  140. gs_register_ref_root(gs_memory_t *mem, gs_gc_root_t *root,
  141.              void **pp, client_name_t cname)
  142. {
  143.     return gs_register_root(mem, root, ptr_ref_type, pp, cname);
  144. }
  145.  
  146. /*
  147.  * As noted in iastate.h, every run of refs has an extra ref at the end
  148.  * to hold relocation information for the garbage collector;
  149.  * since sizeof(ref) % obj_align_mod == 0, we never need to
  150.  * allocate any additional padding space at the end of the block.
  151.  */
  152.  
  153. /* Allocate an array of refs. */
  154. int
  155. gs_alloc_ref_array(gs_ref_memory_t * mem, ref * parr, uint attrs,
  156.            uint num_refs, client_name_t cname)
  157. {
  158.     ref *obj;
  159.  
  160.     /* If we're allocating a run of refs already, */
  161.     /* and we aren't about to overflow the maximum run length, use it. */
  162.     if (mem->cc.rtop == mem->cc.cbot &&
  163.     num_refs < (mem->cc.ctop - mem->cc.cbot) / sizeof(ref) &&
  164.     mem->cc.rtop - (byte *) mem->cc.rcur + num_refs * sizeof(ref) <
  165.     max_size_st_refs
  166.     ) {
  167.     ref *end;
  168.  
  169.     obj = (ref *) mem->cc.rtop - 1;        /* back up over last ref */
  170.     if_debug4('A', "[a%d:+$ ]%s(%u) = 0x%lx\n",
  171.           ialloc_trace_space(mem), client_name_string(cname),
  172.           num_refs, (ulong) obj);
  173.     mem->cc.rcur[-1].o_size += num_refs * sizeof(ref);
  174.     end = (ref *) (mem->cc.rtop = mem->cc.cbot +=
  175.                num_refs * sizeof(ref));
  176.     make_mark(end - 1);
  177.     } else {
  178.     /*
  179.      * Allocate a new run.  We have to distinguish 3 cases:
  180.      *      - Same chunk: pcc unchanged, end == cc.cbot.
  181.      *      - Large chunk: pcc unchanged, end != cc.cbot.
  182.      *      - New chunk: pcc changed.
  183.      */
  184.     chunk_t *pcc = mem->pcc;
  185.     ref *end;
  186.  
  187.     obj = gs_alloc_struct_array((gs_memory_t *) mem, num_refs + 1,
  188.                     ref, &st_refs, cname);
  189.     if (obj == 0)
  190.         return_error(e_VMerror);
  191.     /* Set the terminating ref now. */
  192.     end = (ref *) obj + num_refs;
  193.     make_mark(end);
  194.     /* Set has_refs in the chunk. */
  195.     if (mem->pcc != pcc || mem->cc.cbot == (byte *) (end + 1)) {
  196.         /* Ordinary chunk. */
  197.         mem->cc.rcur = (obj_header_t *) obj;
  198.         mem->cc.rtop = (byte *) (end + 1);
  199.         mem->cc.has_refs = true;
  200.     } else {
  201.         /* Large chunk. */
  202.         /* This happens only for very large arrays, */
  203.         /* so it doesn't need to be cheap. */
  204.         chunk_locator_t cl;
  205.  
  206.         cl.memory = mem;
  207.         cl.cp = mem->clast;
  208.         chunk_locate_ptr(obj, &cl);
  209.         cl.cp->has_refs = true;
  210.     }
  211.     }
  212.     make_array(parr, attrs | mem->space, num_refs, obj);
  213.     return 0;
  214. }
  215.  
  216. /* Resize an array of refs.  Currently this is only implemented */
  217. /* for shrinking, not for growing. */
  218. int
  219. gs_resize_ref_array(gs_ref_memory_t * mem, ref * parr,
  220.             uint new_num_refs, client_name_t cname)
  221. {
  222.     uint old_num_refs = r_size(parr);
  223.     uint diff;
  224.     ref *obj = parr->value.refs;
  225.  
  226.     if (new_num_refs > old_num_refs || !r_has_type(parr, t_array))
  227.     return_error(e_Fatal);
  228.     diff = old_num_refs - new_num_refs;
  229.     /* Check for LIFO.  See gs_free_ref_array for more details. */
  230.     if (mem->cc.rtop == mem->cc.cbot &&
  231.     (byte *) (obj + (old_num_refs + 1)) == mem->cc.rtop
  232.     ) {
  233.     /* Shorten the refs object. */
  234.     ref *end = (ref *) (mem->cc.cbot = mem->cc.rtop -=
  235.                 diff * sizeof(ref));
  236.  
  237.     if_debug4('A', "[a%d:<$ ]%s(%u) 0x%lx\n",
  238.           ialloc_trace_space(mem), client_name_string(cname), diff,
  239.           (ulong) obj);
  240.     mem->cc.rcur[-1].o_size -= diff * sizeof(ref);
  241.     make_mark(end - 1);
  242.     } else {
  243.     /* Punt. */
  244.     if_debug4('A', "[a%d:<$#]%s(%u) 0x%lx\n",
  245.           ialloc_trace_space(mem), client_name_string(cname), diff,
  246.           (ulong) obj);
  247.     mem->lost.refs += diff * sizeof(ref);
  248.     }
  249.     r_set_size(parr, new_num_refs);
  250.     return 0;
  251. }
  252.  
  253. /* Deallocate an array of refs.  Only do this if LIFO, or if */
  254. /* the array occupies an entire chunk by itself. */
  255. void
  256. gs_free_ref_array(gs_ref_memory_t * mem, ref * parr, client_name_t cname)
  257. {
  258.     uint num_refs = r_size(parr);
  259.     ref *obj = parr->value.refs;
  260.  
  261.     /*
  262.      * Compute the storage size of the array, and check for LIFO
  263.      * freeing or a separate chunk.  Note that the array might be packed;
  264.      * for the moment, if it's anything but a t_array, punt.
  265.      * The +1s are for the extra ref for the GC.
  266.      */
  267.     if (!r_has_type(parr, t_array))
  268.     DO_NOTHING;        /* don't look for special cases */
  269.     else if (mem->cc.rtop == mem->cc.cbot &&
  270.          (byte *) (obj + (num_refs + 1)) == mem->cc.rtop
  271.     ) {
  272.     if ((obj_header_t *) obj == mem->cc.rcur) {
  273.         /* Deallocate the entire refs object. */
  274.         gs_free_object((gs_memory_t *) mem, obj, cname);
  275.         mem->cc.rcur = 0;
  276.         mem->cc.rtop = 0;
  277.     } else {
  278.         /* Deallocate it at the end of the refs object. */
  279.         if_debug4('A', "[a%d:-$ ]%s(%u) 0x%lx\n",
  280.               ialloc_trace_space(mem), client_name_string(cname),
  281.               num_refs, (ulong) obj);
  282.         mem->cc.rcur[-1].o_size -= num_refs * sizeof(ref);
  283.         mem->cc.rtop = mem->cc.cbot = (byte *) (obj + 1);
  284.         make_mark(obj);
  285.     }
  286.     return;
  287.     } else if (num_refs >= (mem->large_size / arch_sizeof_ref - 1)) {
  288.     /* See if this array has a chunk all to itself. */
  289.     /* We only make this check when freeing very large objects, */
  290.     /* so it doesn't need to be cheap. */
  291.     chunk_locator_t cl;
  292.  
  293.     cl.memory = mem;
  294.     cl.cp = mem->clast;
  295.     if (chunk_locate_ptr(obj, &cl) &&
  296.         obj == (ref *) ((obj_header_t *) (cl.cp->cbase) + 1) &&
  297.         (byte *) (obj + (num_refs + 1)) == cl.cp->cend
  298.         ) {
  299.         /* Free the chunk. */
  300.         if_debug4('a', "[a%d:-$L]%s(%u) 0x%lx\n",
  301.               ialloc_trace_space(mem), client_name_string(cname),
  302.               num_refs, (ulong) obj);
  303.         alloc_free_chunk(cl.cp, mem);
  304.         return;
  305.     }
  306.     }
  307.     /* Punt, but fill the array with nulls so that there won't be */
  308.     /* dangling references to confuse the garbage collector. */
  309.     if_debug4('A', "[a%d:-$#]%s(%u) 0x%lx\n",
  310.           ialloc_trace_space(mem), client_name_string(cname), num_refs,
  311.           (ulong) obj);
  312.     {
  313.     uint size;
  314.  
  315.     switch (r_type(parr)) {
  316.         case t_shortarray:
  317.         size = num_refs * sizeof(ref_packed);
  318.         break;
  319.         case t_mixedarray:{
  320.         /* We have to parse the array to compute the storage size. */
  321.         uint i = 0;
  322.         const ref_packed *p = parr->value.packed;
  323.  
  324.         for (; i < num_refs; ++i)
  325.             p = packed_next(p);
  326.         size = (const byte *)p - (const byte *)parr->value.packed;
  327.         break;
  328.         }
  329.         case t_array:
  330.         size = num_refs * sizeof(ref);
  331.         break;
  332.         default:
  333.         lprintf3("Unknown type 0x%x in free_ref_array(%u,0x%lx)!",
  334.              r_type(parr), num_refs, (ulong) obj);
  335.         return;
  336.     }
  337.     /*
  338.      * If there are any leftover packed elements, we don't
  339.      * worry about them, since they can't be dangling references.
  340.      */
  341.     refset_null_new(obj, size / sizeof(ref), 0);
  342.     mem->lost.refs += size;
  343.     }
  344. }
  345.  
  346. /* Allocate a string ref. */
  347. int
  348. gs_alloc_string_ref(gs_ref_memory_t * mem, ref * psref,
  349.             uint attrs, uint nbytes, client_name_t cname)
  350. {
  351.     byte *str = gs_alloc_string((gs_memory_t *) mem, nbytes, cname);
  352.  
  353.     if (str == 0)
  354.     return_error(e_VMerror);
  355.     make_string(psref, attrs | mem->space, nbytes, str);
  356.     return 0;
  357. }
  358.