home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / mem.c < prev    next >
C/C++ Source or Header  |  2000-06-23  |  5KB  |  231 lines

  1. /****************************************************************
  2. Copyright 1990, 1991 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "iob.h"
  26.  
  27. #define MEMBSIZE    32000
  28. #define GMEMBSIZE    16000
  29.  
  30.  extern void exit();
  31.  
  32.  char *
  33. gmem(n, round)
  34.  int n, round;
  35. {
  36.     static char *last, *next;
  37.     char *rv;
  38.     if (round)
  39. #ifdef CRAY
  40.         if ((long)next & 0xe000000000000000)
  41.             next = (char *)(((long)next & 0x1fffffffffffffff) + 1);
  42. #else
  43. #ifdef MSDOS
  44.         if ((int)next & 1)
  45.             next++;
  46. #else
  47.         next = (char *)(((long)next + sizeof(char *)-1)
  48.                 & ~((long)sizeof(char *)-1));
  49. #endif
  50. #endif
  51.     rv = next;
  52.     if ((next += n) > last) {
  53.         rv = Alloc(n + GMEMBSIZE);
  54.  
  55.         next = rv + n;
  56.         last = next + GMEMBSIZE;
  57.         }
  58.     return rv;
  59.     }
  60.  
  61.  struct memblock {
  62.     struct memblock *next;
  63.     char buf[MEMBSIZE];
  64.     };
  65.  typedef struct memblock memblock;
  66.  
  67.  static memblock *mem0;
  68.  memblock *curmemblock, *firstmemblock;
  69.  
  70.  char *mem_first, *mem_next, *mem_last, *mem0_last;
  71.  
  72.  void
  73. mem_init()
  74. {
  75.     curmemblock = firstmemblock = mem0
  76.         = (memblock *)Alloc(sizeof(memblock));
  77.     mem_first = mem0->buf;
  78.     mem_next  = mem0->buf;
  79.     mem_last  = mem0->buf + MEMBSIZE;
  80.     mem0_last = mem0->buf + MEMBSIZE;
  81.     mem0->next = 0;
  82.     }
  83.  
  84.  char *
  85. mem(n, round)
  86.  int n, round;
  87. {
  88.     memblock *b;
  89.     register char *rv, *s;
  90.  
  91.     if (round)
  92. #ifdef CRAY
  93.         if ((long)mem_next & 0xe000000000000000)
  94.             mem_next = (char *)(((long)mem_next & 0x1fffffffffffffff) + 1);
  95. #else
  96. #ifdef MSDOS
  97.         if ((int)mem_next & 1)
  98.             mem_next++;
  99. #else
  100.         mem_next = (char *)(((long)mem_next + sizeof(char *)-1)
  101.                 & ~((long)sizeof(char *)-1));
  102. #endif
  103. #endif
  104.     rv = mem_next;
  105.     s = rv + n;
  106.     if (s >= mem_last) {
  107.         if (n > MEMBSIZE)  {
  108.             fprintf(stderr, "mem(%d) failure!\n", n);
  109.             exit(1);
  110.             }
  111.         if (!(b = curmemblock->next)) {
  112.             b = (memblock *)Alloc(sizeof(memblock));
  113.             curmemblock->next = b;
  114.             b->next = 0;
  115.             }
  116.         curmemblock = b;
  117.         rv = b->buf;
  118.         mem_last = rv + sizeof(b->buf);
  119.         s = rv + n;
  120.         }
  121.     mem_next = s;
  122.     return rv;
  123.     }
  124.  
  125.  char *
  126. tostring(s,n)
  127.  register char *s;
  128.  int n;
  129. {
  130.     register char *s1, *se, **sf;
  131.     char *rv, *s0;
  132.     register int k = n + 2, t;
  133.  
  134.     sf = str_fmt;
  135.     sf['%'] = "%";
  136.     s0 = s;
  137.     se = s + n;
  138.     for(; s < se; s++) {
  139.         t = *(unsigned char *)s;
  140.         s1 = sf[t];
  141.         while(*++s1)
  142.             k++;
  143.         }
  144.     sf['%'] = "%%";
  145.     rv = s1 = mem(k,0);
  146.     *s1++ = '"';
  147.     for(s = s0; s < se; s++) {
  148.         t = *(unsigned char *)s;
  149.         sprintf(s1, sf[t], t);
  150.         s1 += strlen(s1);
  151.         }
  152.     *s1 = 0;
  153.     return rv;
  154.     }
  155.  
  156.  char *
  157. cpstring(s)
  158.  register char *s;
  159. {
  160.     return strcpy(mem(strlen(s)+1,0), s);
  161.     }
  162.  
  163.  void
  164. new_iob_data(ios, name)
  165.  register io_setup *ios;
  166.  char *name;
  167. {
  168.     register iob_data *iod;
  169.     register char **s, **se;
  170.  
  171.     iod = (iob_data *)
  172.         mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
  173.     iod->next = iob_list;
  174.     iob_list = iod;
  175.     iod->type = ios->fields[0];
  176.     iod->name = cpstring(name);
  177.     s = iod->fields;
  178.     se = s + ios->nelt;
  179.     while(s < se)
  180.         *s++ = "0";
  181.     *s = 0;
  182.     }
  183.  
  184.  char *
  185. string_num(pfx, n)
  186.  char *pfx;
  187.  long n;
  188. {
  189.     char buf[32];
  190.     sprintf(buf, "%s%ld", pfx, n);
  191.     /* can't trust return type of sprintf -- BSD gets it wrong */
  192.     return strcpy(mem(strlen(buf)+1,0), buf);
  193.     }
  194.  
  195. static defines *define_list;
  196.  
  197.  void
  198. def_start(outfile, s1, s2, post)
  199.  FILE *outfile;
  200.  char *s1, *s2, *post;
  201. {
  202.     defines *d;
  203.     int n, n1;
  204.  
  205.     n = n1 = strlen(s1);
  206.     if (s2)
  207.         n += strlen(s2);
  208.     d = (defines *)mem(sizeof(defines)+n, 1);
  209.     d->next = define_list;
  210.     define_list = d;
  211.     strcpy(d->defname, s1);
  212.     if (s2)
  213.         strcpy(d->defname + n1, s2);
  214.     nice_printf(outfile, "#define %s %s", d->defname, post);
  215.     }
  216.  
  217.  void
  218. other_undefs(outfile)
  219.  FILE *outfile;
  220. {
  221.     defines *d;
  222.     if (d = define_list) {
  223.         define_list = 0;
  224.         nice_printf(outfile, "\n");
  225.         do
  226.             nice_printf(outfile, "#undef %s\n", d->defname);
  227.             while(d = d->next);
  228.         nice_printf(outfile, "\n");
  229.         }
  230.     }
  231.