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

  1. /* Copyright (C) 1993 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. /* gsrefct.h */
  20. /* Reference counting definitions */
  21.  
  22. #ifndef gsrefct_INCLUDED
  23. #  define gsrefct_INCLUDED
  24.  
  25. /*
  26.  * A reference-counted object must include the following header:
  27.  *    rc_header rc;
  28.  * The header need not be the first element of the object.
  29.  */
  30. typedef struct rc_header_s rc_header;
  31. struct rc_header_s {
  32.     long ref_count;
  33. #define rc_free_proc(proc)\
  34.   void proc(P3(gs_memory_t *, char *, client_name_t))
  35.     rc_free_proc((*free));
  36. };
  37.  
  38. /* ------ Allocate/free ------ */
  39.  
  40. rc_free_proc(rc_free_struct_only);
  41. #define rc_alloc_struct_n(vp, typ, pstyp, mem, errstat, cname, rcinit)\
  42.    {    if ( (vp = gs_alloc_struct(mem, typ, pstyp, cname)) == 0 )\
  43.        errstat;\
  44.     vp->rc.ref_count = rcinit;\
  45.     vp->rc.free = rc_free_struct_only;\
  46.    }
  47. #define rc_alloc_struct_0(vp, typ, pstype, mem, errstat, cname)\
  48.   rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 0)
  49. #define rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname)\
  50.   rc_alloc_struct_n(vp, typ, pstype, mem, errstat, cname, 1)
  51.  
  52. #define rc_free_struct(vp, mem, cname)\
  53.   (*vp->rc.free)(mem, (char *)vp, cname)
  54.  
  55. /* ------ Reference counting ------ */
  56.  
  57. /* Increment a reference count. */
  58. #define rc_increment(vp)\
  59.   if ( vp != 0 ) vp->rc.ref_count++
  60.  
  61. /* Increment a reference count, allocating the structure if necessary. */
  62. #define rc_allocate_struct(vp, typ, pstype, mem, errstat, cname)\
  63.   if ( vp != 0 )\
  64.     vp->rc.ref_count++;\
  65.   else\
  66.    {    rc_alloc_struct_1(vp, typ, pstype, mem, errstat, cname);\
  67.    }
  68.  
  69. /* Guarantee that a structure is not shared. */
  70. #define rc_unshare_struct(vp, typ, pstype, mem, errstat, cname)\
  71.   if ( vp == 0 || vp->rc.ref_count > 1 )\
  72.    {    typ *new;\
  73.     rc_alloc_struct_1(new, typ, pstype, mem, errstat, cname);\
  74.     if ( vp ) vp->rc.ref_count--;\
  75.     vp = new;\
  76.    }
  77.  
  78. /* Adjust a reference count either up or down. */
  79. #define rc_adjust_(vp, delta, mem, cname, body)\
  80.   if ( vp != 0 && !(vp->rc.ref_count += delta) )\
  81.    {    rc_free_struct(vp, mem, cname);\
  82.     body;\
  83.    }
  84. #define rc_adjust(vp, delta, mem, cname)\
  85.   rc_adjust_(vp, delta, mem, cname, vp = 0)
  86. #define rc_adjust_const(vp, delta, mem, cname)\
  87.   rc_adjust_(vp, delta, mem, cname, DO_NOTHING)
  88. #define rc_decrement(vp, mem, cname)\
  89.   rc_adjust(vp, -1, mem, cname)
  90.  
  91. /* Assign a pointer, adjusting reference counts. */
  92. #define rc_assign(vpto, vpfrom, mem, cname)\
  93.   if ( vpto != vpfrom )\
  94.    {    rc_decrement(vpto, mem, cname);\
  95.     vpto = vpfrom;\
  96.     rc_increment(vpto);\
  97.    }
  98.  
  99. #endif                    /* gsrefct_INCLUDED */
  100.