home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / icont / util.c < prev   
C/C++ Source or Header  |  2001-12-12  |  2KB  |  107 lines

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