home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z31.c < prev    next >
Text File  |  1994-01-23  |  10KB  |  227 lines

  1. /*@z31.c:Memory Allocator:DebugMemory()@**************************************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z31.c                                                      */
  26. /*  MODULE:       Memory Allocator                                           */
  27. /*  EXTERNS:      DebugMemory(), zz_free[], MemInit(), GetMemory()           */
  28. /*                                                                           */
  29. /*****************************************************************************/
  30. #include "externs"
  31.  
  32. #define    MEM_CHUNK    1020        /* how many ALIGNs to get from sys   */
  33.  
  34.  
  35. #if DEBUG_ON
  36. static    int    no_of_calls    = 0;    /* number of calls to calloc()       */
  37.     int    zz_newcount    = 0;    /* number of calls to New()          */
  38.     int    zz_disposecount    = 0;    /* number of calls to Dispose()      */
  39.  
  40. /*****************************************************************************/
  41. /*                                                                           */
  42. /*  DebugMemory()                                                            */
  43. /*                                                                           */
  44. /*  Print memory usage.                                                      */
  45. /*                                                                           */
  46. /*****************************************************************************/
  47.  
  48. DebugMemory()
  49. { int i, j;  OBJECT p;
  50.   debug2(DMA, D, "calloc called %d times (%d bytes total)",
  51.     no_of_calls, no_of_calls * MEM_CHUNK * sizeof(ALIGN));
  52.   debug2(DMA, D, "New() called %d times;  Dispose() called %d times",
  53.     zz_newcount, zz_disposecount);
  54.   for( i = 0;  i < MAX_OBJECT_REC;  i++ )
  55.   { if( zz_free[i] != nil )
  56.     { j = 0;
  57.       for( p = zz_free[i];  p != nil;  p = pred(p, CHILD) )  j++;
  58.       debug2(DMA, DD, "zz_free[%2d]: %3d", i, j);
  59.     }
  60.   }
  61. } /* end DebugMemory */
  62. #endif
  63.  
  64.  
  65. /*@::zz_free[], zz_lengths[], MemInit()@**************************************/
  66. /*                                                                           */
  67. /*  OBJECT         zz_free[], zz_hold, zz_tmp, zz_res                        */
  68. /*  int            zz_size                                                   */
  69. /*  unsigned char  zz_lengths[]                                              */
  70. /*                                                                           */
  71. /*  zz_free[i]:    free records of size i*sizeof(ALIGN).                     */
  72. /*  zz_lengths[i]: the number of ALIGNs in a record of type i.               */
  73. /*  These variables are used only within the New() and Dispose() macros,     */
  74. /*  and the list handling macros.                                            */
  75. /*                                                                           */
  76. /*****************************************************************************/
  77.  
  78. OBJECT        zz_free[MAX_OBJECT_REC], zz_hold, zz_tmp, zz_res;
  79. int        zz_size;
  80. unsigned char    zz_lengths[DISPOSED];        /* DISPOSED is 1 + max type */
  81. OBJECT         xx_link, xx_tmp, xx_res, xx_hold;
  82.  
  83.  
  84. /*****************************************************************************/
  85. /*                                                                           */
  86. /*  MemInit()                                                                */
  87. /*                                                                           */
  88. /*  Initialise memory allocator.                                             */
  89. /*                                                                           */
  90. /*****************************************************************************/
  91.  
  92. MemInit()
  93. {
  94.   zz_lengths[ WORD        ] = 0;
  95.   zz_lengths[ QWORD       ] = 0;
  96.   zz_lengths[ LINK        ] = ceiling( sizeof(struct link_type), sizeof(ALIGN));
  97.  
  98.   /* object types, except closure NB have actual() field in token phase! */
  99.   zz_lengths[ SPLIT       ] =
  100.   zz_lengths[ HEAD        ] =
  101.   zz_lengths[ PAR         ] =
  102.   zz_lengths[ ROW_THR     ] =
  103.   zz_lengths[ COL_THR     ] =
  104.   zz_lengths[ CLOSURE     ] =
  105.   zz_lengths[ NULL_CLOS   ] =
  106.   zz_lengths[ CROSS       ] =
  107.   zz_lengths[ ONE_COL     ] =
  108.   zz_lengths[ ONE_ROW     ] =
  109.   zz_lengths[ WIDE        ] =
  110.   zz_lengths[ HIGH        ] =
  111.   zz_lengths[ HSCALE      ] =
  112.   zz_lengths[ VSCALE      ] =
  113.   zz_lengths[ HCONTRACT   ] =
  114.   zz_lengths[ VCONTRACT   ] =
  115.   zz_lengths[ HEXPAND     ] =
  116.   zz_lengths[ VEXPAND     ] =
  117.   zz_lengths[ PADJUST     ] =
  118.   zz_lengths[ HADJUST     ] =
  119.   zz_lengths[ VADJUST     ] =
  120.   zz_lengths[ ROTATE      ] =
  121.   zz_lengths[ SCALE       ] =
  122.   zz_lengths[ CASE        ] =
  123.   zz_lengths[ YIELD       ] =
  124.   zz_lengths[ XCHAR       ] =
  125.   zz_lengths[ FONT        ] =
  126.   zz_lengths[ SPACE       ] =
  127.   zz_lengths[ BREAK       ] =
  128.   zz_lengths[ NEXT        ] =
  129.   zz_lengths[ ENV         ] =
  130.   zz_lengths[ CLOS        ] =
  131.   zz_lengths[ LVIS        ] =
  132.   zz_lengths[ OPEN        ] =
  133.   zz_lengths[ TAGGED      ] =
  134.   zz_lengths[ INCGRAPHIC  ] =
  135.   zz_lengths[ SINCGRAPHIC ] =
  136.   zz_lengths[ GRAPHIC     ] =
  137.   zz_lengths[ ACAT        ] =
  138.   zz_lengths[ HCAT        ] =
  139.   zz_lengths[ VCAT        ] =
  140.   zz_lengths[ LBR         ] =
  141.   zz_lengths[ RBR         ] =
  142.   zz_lengths[ BEGIN       ] =
  143.   zz_lengths[ END         ] =
  144.   zz_lengths[ USE         ] =
  145.   zz_lengths[ PREPEND     ] =
  146.   zz_lengths[ SYS_PREPEND ] =
  147.   zz_lengths[ DATABASE    ] =
  148.   zz_lengths[ SYS_DATABASE] =
  149.   zz_lengths[ GSTUB_NONE  ] =
  150.   zz_lengths[ GSTUB_INT   ] =
  151.   zz_lengths[ GSTUB_EXT   ] =
  152.   zz_lengths[ DEAD        ] =
  153.   zz_lengths[ UNATTACHED  ] =
  154.   zz_lengths[ RECEPTIVE   ] =
  155.   zz_lengths[ RECEIVING   ] =
  156.   zz_lengths[ RECURSIVE   ] =
  157.   zz_lengths[ PRECEDES    ] =
  158.   zz_lengths[ FOLLOWS     ] =
  159.   zz_lengths[ CROSS_FOLL  ] =
  160.   zz_lengths[ GALL_FOLL   ] =
  161.   zz_lengths[ CROSS_TARG  ] =
  162.   zz_lengths[ GALL_TARG   ] =
  163.   zz_lengths[ GALL_PREC   ] =
  164.   zz_lengths[ CROSS_PREC  ] =
  165.   zz_lengths[ EXPAND_IND  ] =
  166.   zz_lengths[ THREAD      ] =
  167.   zz_lengths[ CR_LIST     ] =
  168.     ceiling(sizeof(struct closure_type), sizeof(ALIGN));
  169.  
  170.   /* symbol types */
  171.   zz_lengths[ MACRO       ] =
  172.   zz_lengths[ LOCAL       ] =
  173.   zz_lengths[ LPAR        ] =
  174.   zz_lengths[ RPAR        ] =
  175.   zz_lengths[ NPAR        ] =
  176.     ceiling(sizeof(struct symbol_type), sizeof(ALIGN));
  177.  
  178.   /* gap objects */
  179.   zz_lengths[ TSPACE      ] =
  180.   zz_lengths[ TJUXTA      ] =
  181.   zz_lengths[ GAP_OBJ     ] =
  182.     ceiling(sizeof(struct gapobj_type), sizeof(ALIGN));
  183.  
  184.   /* cross-reference and data base types */
  185.   zz_lengths[ CROSS_SYM   ] =
  186.   zz_lengths[ CR_ROOT     ] = ceiling(sizeof(struct cr_type) , sizeof(ALIGN));
  187.  
  188.   /* external galley record */
  189.   zz_lengths[ EXT_GALL  ] = ceiling(sizeof(struct ext_gall_type),sizeof(ALIGN));
  190.  
  191. } /* end MemInit() */
  192.  
  193.  
  194. /*@::GetMemory()@*************************************************************/
  195. /*                                                                           */
  196. /*  OBJECT GetMemory(siz, pos)                                               */
  197. /*                                                                           */
  198. /*  Return a pointer to siz ALIGNs of memory (0 < siz < MAX_OBJECT_REC).     */
  199. /*                                                                           */
  200. /*****************************************************************************/
  201.  
  202. OBJECT GetMemory(siz, pos)
  203. int siz;  FILE_POS *pos;
  204. { static ALIGN *next_free = (ALIGN *) nil;
  205.   static ALIGN *top_free  = (ALIGN *) nil;
  206.   OBJECT res;
  207.   char *calloc();
  208.  
  209.   debug1(DMA, DDD, "GetMemory( %d )", siz);
  210.  
  211.   /* get memory from operating system, if not enough left here */
  212.   if( &next_free[siz] > top_free )
  213.   { next_free = (ALIGN *) calloc(MEM_CHUNK, sizeof(ALIGN));
  214.     ifdebug(DMA, D, no_of_calls++; )
  215.     if( next_free == NULL ) Error(FATAL,pos,"run out of memory - exiting now");
  216.     top_free = &next_free[MEM_CHUNK];
  217.     debug2(DMA, D, "GetMemory: calloc returned %d - %d",
  218.       (int) next_free, (int) top_free);
  219.   }
  220.  
  221.   res = (OBJECT) next_free;
  222.   next_free = &next_free[siz];
  223.   debug3(DMA, DDD, "GetMemory returning @%d (next_free = @%d, top_free = @%d",
  224.     (int) res, (int) next_free, (int) top_free);
  225.   return res;
  226. } /* end GetMemory */
  227.