home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / IASTATE.H < prev    next >
C/C++ Source or Header  |  1994-07-27  |  11KB  |  288 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* iastate.h */
  20. /* Memory manager internal definitions for Ghostscript */
  21. /* Requires gsmemory.h, gsstruct.h */
  22. #include "istruct.h"
  23. #include "iastruct.h"
  24.  
  25. /* ================ Chunks ================ */
  26.  
  27. /*
  28.  * We obtain memory from the operating system in `chunks'.  A chunk
  29.  * may hold only a single large object (or string), or it may hold
  30.  * many objects (allocated from the bottom up, always aligned)
  31.  * and strings (allocated from the top down, not aligned).
  32.  */
  33.  
  34. /*
  35.  * Refs are allocated in the bottom-up section, along with struct objects.
  36.  * In order to keep the overhead for refs small, we make consecutive
  37.  * blocks of refs into a single allocator object of type st_refs.
  38.  * To do this, we remember the start of the current ref object (if any),
  39.  * and the end of the last block of allocated refs.
  40.  * As long as the latter is equal to the top of the allocated area,
  41.  * we can add more refs to the current object; otherwise, we have to
  42.  * close off the previous ref object (if any) and start a new one.
  43.  * We check and then assume that sizeof(ref) % obj_align_mod == 0;
  44.  * this means that if we ever have to pad a block of refs, we never add
  45.  * as much as one entire ref.
  46.  */
  47. #ifdef r_type            /* i.e., we know about refs */
  48. #  if (arch_sizeof_ref % obj_align_mod) != 0
  49. Error : arch_sizeof_ref % obj_align_mod != 0 :
  50. #  endif
  51. #endif
  52.  
  53. /*
  54.  * When we do a save, we create a new 'inner' chunk out of the remaining
  55.  * space in the currently active chunk.  Inner chunks must not be freed
  56.  * by a restore.
  57.  *
  58.  * The garbage collector implements relocation for refs by scanning
  59.  * forward to a free object.  Because of this, every ref object must end
  60.  * with a dummy ref that can hold the relocation for the last block.
  61.  */
  62.  
  63. /*
  64.  * Strings carry some additional overhead for use by the GC.
  65.  * At the top of the chunk is a table of relocation values for
  66.  * 32-character blocks of strings.  This table is aligned, by adding
  67.  * padding above it if necessary.  Just below it is a mark table for
  68.  * the strings, which need not be aligned.  The actual string data
  69.  * start below the mark table.  These tables are not needed for a chunk
  70.  * that holds a single large (non-string) object, but they are needed for
  71.  * all other chunks, including chunks created to hold a single large
  72.  * string.
  73.  */
  74.  
  75. /*
  76.  * To allow the garbage collector to combine chunks, we store in the
  77.  * head of each chunk the address to which its contents will be moved.
  78.  */
  79. /*typedef struct chunk_head_s chunk_head_t;*/    /* in iastruct.h */
  80.  
  81. /* Structure for a chunk. */
  82. typedef struct chunk_s chunk_t;
  83. struct chunk_s {
  84.     chunk_head_t *chead;        /* chunk head, bottom of chunk */
  85.     /* Note that allocation takes place both from the bottom up */
  86.     /* (aligned objects) and from the top down (strings). */
  87.     byte *cbase;            /* bottom of chunk data area */
  88.     byte *cbot;            /* bottom of free area */
  89.                     /* (top of aligned objects) */
  90.     obj_header_t *rcur;        /* current refs object, 0 if none */
  91.     byte *rtop;            /* top of rcur */
  92.     byte *ctop;            /* top of free area */
  93.                     /* (bottom of strings) */
  94.     byte *climit;            /* top of strings */
  95.     byte *cend;            /* top of chunk */
  96.     chunk_t *cprev;            /* chain chunks together, */
  97.     chunk_t *cnext;            /*   sorted by address */
  98.     chunk_t *outer;            /* the chunk of which this is */
  99.                     /*   an inner chunk, if any */
  100.     uint inner_count;        /* number of chunks of which this is */
  101.                     /*   the outer chunk, if any */
  102.     bool has_refs;            /* true if any refs in chunk */
  103.         /* The remaining members are for the GC. */
  104.     byte *odest;            /* destination for objects */
  105.     byte *smark;            /* mark bits for strings */
  106.     uint smark_size;
  107.     byte *sbase;            /* base for computing smark offsets */
  108.     ushort *sreloc;            /* relocation for string blocks */
  109.     byte *sdest;            /* destination for (top of) strings */
  110. };
  111. /* The chunk descriptor is exported only for isave.c. */
  112. extern_st(st_chunk);
  113. #define public_st_chunk()    /* in ialloc.c */\
  114.   gs_public_st_ptrs2(st_chunk, chunk_t, "chunk_t",\
  115.     chunk_enum_ptrs, chunk_reloc_ptrs, cprev, cnext)
  116.  
  117. /*
  118.  * Macros for scanning a chunk linearly, with the following schema:
  119.  *    SCAN_CHUNK_OBJECTS(cp)            << declares pre, size >>
  120.  *        << code for all objects -- size not set yet >>
  121.  *    DO_LARGE
  122.  *        << code for large objects >>
  123.  *    DO_SMALL
  124.  *        << code for small objects >>
  125.  *    END_OBJECTS_SCAN
  126.  * If large and small objects are treated alike, one can use DO_ALL instead
  127.  * of DO_LARGE and DO_SMALL.
  128.  */
  129. #define SCAN_CHUNK_OBJECTS(cp)\
  130.     {    obj_header_t *pre = (obj_header_t *)((cp)->cbase);\
  131.         obj_header_t *end = (obj_header_t *)((cp)->cbot);\
  132.         ulong size;        /* long because of large objects */\
  133.         for ( ; pre < end;\
  134.             pre = (obj_header_t *)((char *)pre + obj_size_round(size))\
  135.             )\
  136.         {
  137. #define DO_LARGE\
  138.             if ( pre->o_large )\
  139.             {    size = pre_obj_large_size(pre);\
  140.                 {
  141. #define DO_SMALL\
  142.                 }\
  143.             } else\
  144.             {    size = pre_obj_small_size(pre);\
  145.                 {
  146. #define DO_ALL\
  147.             {    size = pre_obj_contents_size(pre);\
  148.                 {
  149. #ifdef DEBUG
  150. #  define END_OBJECTS_SCAN\
  151.                 }\
  152.             }\
  153.         }\
  154.         if ( pre != end )\
  155.         {    lprintf2("Chunk parsing error, 0x%lx != 0x%lx\n",\
  156.                  (ulong)pre, (ulong)end);\
  157.             exit(1);\
  158.         }\
  159.     }
  160. #else
  161. #  define END_OBJECTS_SCAN\
  162.                 }\
  163.             }\
  164.         }\
  165.     }
  166. #endif
  167.  
  168. /* Initialize a chunk. */
  169. /* This is exported for save/restore. */
  170. void alloc_init_chunk(P5(chunk_t *, byte *, byte *, bool, chunk_t *));
  171.  
  172. /* Find the chunk for a pointer. */
  173. /* Note that ptr_is_within_chunk returns true even if the pointer */
  174. /* is in an inner chunk of the chunk being tested. */
  175. #define ptr_is_within_chunk(ptr, cp)\
  176.   ptr_between((const byte *)(ptr), (cp)->cbase, (cp)->cend)
  177. #define ptr_is_in_inner_chunk(ptr, cp)\
  178.   ((cp)->inner_count != 0 &&\
  179.    ptr_between((const byte *)(ptr), (cp)->cbot, (cp)->ctop))
  180. #define ptr_is_in_chunk(ptr, cp)\
  181.   (ptr_is_within_chunk(ptr, cp) && !ptr_is_in_inner_chunk(ptr, cp))
  182. typedef struct chunk_locator_s {
  183.     const gs_ref_memory_t *memory;    /* for head & tail of chain */
  184.     chunk_t *cp;            /* one-element cache */
  185. } chunk_locator_t;
  186. bool chunk_locate_ptr(P2(const void *, chunk_locator_t *));
  187. #define chunk_locate(ptr, clp)\
  188.   (((clp)->cp != 0 && ptr_is_in_chunk(ptr, (clp)->cp)) ||\
  189.    chunk_locate_ptr(ptr, clp))
  190.  
  191. /* Close up the current chunk. */
  192. /* This is exported for save/restore and for the GC. */
  193. void alloc_close_chunk(P1(gs_ref_memory_t *mem));
  194.  
  195. /* Reopen the current chunk after a GC. */
  196. void alloc_open_chunk(P1(gs_ref_memory_t *mem));
  197.  
  198. /* Insert or remove a chunk in the address-ordered chain. */
  199. /* These are exported for the GC. */
  200. void alloc_link_chunk(P2(chunk_t *, gs_ref_memory_t *));
  201. void alloc_unlink_chunk(P2(chunk_t *, gs_ref_memory_t *));
  202.  
  203. /* Free a chunk.  This is exported for save/restore and for the GC. */
  204. void alloc_free_chunk(P2(chunk_t *, gs_ref_memory_t *));
  205.  
  206. /* Print a chunk debugging message. */
  207. #define if_debug_chunk(c, msg, cp)\
  208.   if_debug7(c, "%s 0x%lx (0x%lx..0x%lx, 0x%lx..0x%lx..0x%lx)\n", msg,\
  209.         (ulong)(cp), (ulong)(cp)->cbase, (ulong)(cp)->cbot,\
  210.         (ulong)(cp)->ctop, (ulong)(cp)->climit, (ulong)(cp)->cend)
  211.  
  212. /* ================ Allocator state ================ */
  213.  
  214. /* Structures for save/restore (not defined here). */
  215. struct alloc_save_s;
  216. struct alloc_change_s;
  217.  
  218. /* Define the number of freelists.  The index in the freelist array */
  219. /* is the ceiling of the size of the object contents (i.e., not including */
  220. /* the header) divided by obj_align_mod. */
  221. #define max_freelist_size 500        /* big enough for gstate & contents */
  222. #define num_freelists\
  223.   ((max_freelist_size + obj_align_mod - 1) / obj_align_mod)
  224.  
  225. /* Define the memory manager subclass for this allocator. */
  226. struct gs_ref_memory_s {
  227.         /* The following are set at initialization time. */
  228.     gs_memory_common;
  229.     gs_memory_t *parent;        /* for allocating chunks */
  230.     uint chunk_size;
  231.     uint large_size;        /* min size to give large object */
  232.                     /* its own chunk: must be */
  233.                     /* 1 mod obj_align_mod */
  234.     gs_ref_memory_t *global;    /* global VM for this allocator */
  235.                     /* (may point to itself) */
  236.     uint local_attr;        /* 0 if global VM, a_local if local */
  237.         /* Callers can change the following dynamically */
  238.         /* (through a procedural interface). */
  239.     gs_memory_gc_status_t gc_status;
  240.         /* The following are updated dynamically. */
  241.     ulong limit;            /* signal a VMerror when total */
  242.                     /* allocated exceeds this */
  243.     chunk_t *cfirst;        /* head of chunk list */
  244.     chunk_t *clast;            /* tail of chunk list */
  245.     chunk_t cc;            /* current chunk */
  246.     chunk_t *pcc;            /* where to store cc */
  247.     chunk_locator_t cfreed;        /* chunk where last object freed */
  248.     ulong allocated;        /* total size of all chunks */
  249.                     /* allocated at this save level */
  250.     ulong gc_allocated;        /* value of (allocated + */
  251.                 /* previous_status.allocated) after last GC */
  252.     ulong freed_lost;        /* space freed and 'lost' */
  253.         /* Garbage collector information */
  254.     gs_gc_root_t *roots;        /* roots for GC */
  255.         /* Sharing / saved state information */
  256.     int num_contexts;        /* # of contexts sharing this VM */
  257.     struct alloc_change_s *changes;
  258.     struct alloc_save_s *saved;
  259.     struct alloc_save_s *reloc_saved;    /* for GC */
  260.     gs_memory_status_t previous_status;    /* total allocated & used */
  261.                     /* in outer save levels */
  262.         /* We put the freelists last to keep the scalar */
  263.         /* offsets small. */
  264.     obj_header_t *freelists[num_freelists];
  265. };
  266. /* The procedures for a gs_ref_memory_t must be public because alloc_save_t */
  267. /* is a subclass; aside from that, the descriptor should be private. */
  268. struct_proc_enum_ptrs(ref_memory_enum_ptrs);
  269. struct_proc_reloc_ptrs(ref_memory_reloc_ptrs);
  270. #define private_st_ref_memory()    /* in ialloc.c */\
  271.   gs_private_st_composite_only(st_ref_memory, gs_ref_memory_t,\
  272.     "gs_ref_memory", ref_memory_enum_ptrs, ref_memory_reloc_ptrs)
  273. #define st_ref_memory_max_ptrs 2    /* changes, saved */
  274.  
  275. /*
  276.  * Scan the chunks of an allocator:
  277.  *    SCAN_MEM_CHUNKS(mem, cp)
  278.  *        << code to process chunk cp >>
  279.  *    END_CHUNKS_SCAN
  280.  */
  281. #define SCAN_MEM_CHUNKS(mem, cp)\
  282.     {    chunk_t *cp = (mem)->cfirst;\
  283.         for ( ; cp != 0; cp = cp->cnext )\
  284.         {
  285. #define END_CHUNKS_SCAN\
  286.         }\
  287.     }
  288.