home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / icont / util.c < prev   
C/C++ Source or Header  |  1996-03-22  |  3KB  |  130 lines

  1. /*
  2.  *  util.c -- general utility functions.
  3.  */
  4.  
  5. #include <ctype.h>
  6. #include "::h:gsupport.h"
  7. #include "tproto.h"
  8. #include "tglobals.h"
  9. #include "tree.h"
  10.  
  11.  
  12. extern int optindex;
  13.  
  14. extern char *ofile;
  15.  
  16.  
  17. /*
  18.  * Information about Icon functions.
  19.  */
  20.  
  21. /*
  22.  * Number of arguments.
  23.  */
  24.  
  25.  
  26. /*
  27.  * Names of Icon functions.
  28.  */
  29. char *ftable[] = {
  30. #define FncDef(p,n) Lit(p),
  31. #define FncDefV(p) Lit(p),
  32. #include "::h:fdefs.h"
  33. #undef FncDef
  34. #undef FncDefV
  35.    };
  36.  
  37. int ftbsize = sizeof(ftable)/sizeof(char *);
  38.  
  39. /*
  40.  * tcalloc - allocate and zero m*n bytes
  41.  */
  42. pointer tcalloc(m,n)
  43. unsigned int m, n;
  44.    {
  45.    pointer a;
  46.  
  47. #if SCCX_MX
  48.    /* Avoids compiler warning */
  49.    if ( (a = calloc(m,n)) == 0 )
  50. #else
  51.    if (!(a = calloc(m,n)))
  52. #endif                    /* SCCX_MX */
  53.  
  54.       quit("out of memory");
  55.    return a;
  56.    }
  57.  
  58. /*
  59.  * trealloc - realloc a table making it half again larger and zero the
  60.  *   new part of the table.
  61.  */
  62. pointer trealloc(table, tblfree, size, unit_size, min_units, tbl_name)
  63. pointer table;      /* table to be realloc()ed */
  64. pointer tblfree;    /* reference to table free pointer if there is one */
  65. unsigned int *size; /* size of table */
  66. int unit_size;      /* number of bytes in a unit of the table */
  67. int min_units;      /* the minimum number of units that must be allocated. */
  68. char *tbl_name;     /* name of the table */
  69.    {
  70.    word new_size;
  71.    word num_bytes;
  72.    word free_offset;
  73.    word i;
  74.    char *new_tbl;
  75.  
  76.    new_size = (*size * 3) / 2;
  77.    if (new_size - *size < min_units)
  78.       new_size = *size + min_units;
  79.    num_bytes = new_size * unit_size;
  80.  
  81. #if IntBits == 16
  82.     {
  83.     word max_bytes = 64000;
  84.  
  85.     if (num_bytes > max_bytes) {
  86.        new_size = max_bytes / unit_size;
  87.        num_bytes = new_size * unit_size;
  88.        if (new_size - *size < min_units)
  89.           quitf("out of memory for %s", tbl_name);
  90.        }
  91.     }
  92. #endif                    /* IntBits == 16 */
  93.  
  94.    if (tblfree != NULL)
  95.       free_offset = DiffPtrs(*(char **)tblfree,  (char *)table);
  96.  
  97. #if SCCX_MX
  98.    /* Avoids compiler warning */
  99.    if ( (new_tbl = (char *)realloc(table, (unsigned)num_bytes)) == 0 )
  100. #else
  101.    if (!(new_tbl = (char *)realloc(table, (unsigned)num_bytes)))
  102. #endif                    /* SCCX_MX */
  103.  
  104.       quitf("out of memory for %s", tbl_name);
  105.  
  106.    for (i = *size * unit_size; i < num_bytes; ++i)
  107.       new_tbl[i] = 0;
  108.  
  109.    *size = new_size;
  110.    if (tblfree != NULL)
  111.       *(char **)tblfree = (char *)(new_tbl + free_offset);
  112.  
  113.    return (pointer)new_tbl;
  114.    }
  115.  
  116.  
  117. /*
  118.  * round2 - round an integer up to the next power of 2.
  119.  */
  120. unsigned int round2(n)
  121. unsigned int n;
  122.    {
  123.    unsigned int b = 1;
  124.    while (b < n)
  125.       b <<= 1;
  126.    return b;
  127.    }
  128.  
  129.  
  130.