home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / mem.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  6KB  |  269 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1994 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.  char *
  31. #ifdef KR_headers
  32. gmem(n, round)
  33.     int n;
  34.     int round;
  35. #else
  36. gmem(int n, int round)
  37. #endif
  38. {
  39.     static char *last, *next;
  40.     char *rv;
  41.     if (round)
  42. #ifdef CRAY
  43.         if ((long)next & 0xe000000000000000)
  44.             next = (char *)(((long)next & 0x1fffffffffffffff) + 1);
  45. #else
  46. #ifdef MSDOS
  47.         if ((int)next & 1)
  48.             next++;
  49. #else
  50.         next = (char *)(((long)next + sizeof(char *)-1)
  51.                 & ~((long)sizeof(char *)-1));
  52. #endif
  53. #endif
  54.     rv = next;
  55.     if ((next += n) > last) {
  56.         rv = Alloc(n + GMEMBSIZE);
  57.  
  58.         next = rv + n;
  59.         last = next + GMEMBSIZE;
  60.         }
  61.     return rv;
  62.     }
  63.  
  64.  struct memblock {
  65.     struct memblock *next;
  66.     char buf[MEMBSIZE];
  67.     };
  68.  typedef struct memblock memblock;
  69.  
  70.  static memblock *mem0;
  71.  memblock *curmemblock, *firstmemblock;
  72.  
  73.  char *mem_first, *mem_next, *mem_last, *mem0_last;
  74.  
  75.  void
  76. mem_init(Void)
  77. {
  78.     curmemblock = firstmemblock = mem0
  79.         = (memblock *)Alloc(sizeof(memblock));
  80.     mem_first = mem0->buf;
  81.     mem_next  = mem0->buf;
  82.     mem_last  = mem0->buf + MEMBSIZE;
  83.     mem0_last = mem0->buf + MEMBSIZE;
  84.     mem0->next = 0;
  85.     }
  86.  
  87.  char *
  88. #ifdef KR_headers
  89. mem(n, round)
  90.     int n;
  91.     int round;
  92. #else
  93. mem(int n, int round)
  94. #endif
  95. {
  96.     memblock *b;
  97.     register char *rv, *s;
  98.  
  99.     if (round)
  100. #ifdef CRAY
  101.         if ((long)mem_next & 0xe000000000000000)
  102.             mem_next = (char *)(((long)mem_next & 0x1fffffffffffffff) + 1);
  103. #else
  104. #ifdef MSDOS
  105.         if ((int)mem_next & 1)
  106.             mem_next++;
  107. #else
  108.         mem_next = (char *)(((long)mem_next + sizeof(char *)-1)
  109.                 & ~((long)sizeof(char *)-1));
  110. #endif
  111. #endif
  112.     rv = mem_next;
  113.     s = rv + n;
  114.     if (s >= mem_last) {
  115.         if (n > MEMBSIZE)  {
  116.             fprintf(stderr, "mem(%d) failure!\n", n);
  117.             exit(1);
  118.             }
  119.         if (!(b = curmemblock->next)) {
  120.             b = (memblock *)Alloc(sizeof(memblock));
  121.             curmemblock->next = b;
  122.             b->next = 0;
  123.             }
  124.         curmemblock = b;
  125.         rv = b->buf;
  126.         mem_last = rv + sizeof(b->buf);
  127.         s = rv + n;
  128.         }
  129.     mem_next = s;
  130.     return rv;
  131.     }
  132.  
  133.  char *
  134. #ifdef KR_headers
  135. tostring(s, n)
  136.     register char *s;
  137.     int n;
  138. #else
  139. tostring(register char *s, int n)
  140. #endif
  141. {
  142.     register char *s1, *se, **sf;
  143.     char *rv, *s0;
  144.     register int k = n + 2, t;
  145.  
  146.     sf = str_fmt;
  147.     sf['%'] = "%";
  148.     s0 = s;
  149.     se = s + n;
  150.     for(; s < se; s++) {
  151.         t = *(unsigned char *)s;
  152.         s1 = sf[t];
  153.         while(*++s1)
  154.             k++;
  155.         }
  156.     sf['%'] = "%%";
  157.     rv = s1 = mem(k,0);
  158.     *s1++ = '"';
  159.     for(s = s0; s < se; s++) {
  160.         t = *(unsigned char *)s;
  161.         sprintf(s1, sf[t], t);
  162.         s1 += strlen(s1);
  163.         }
  164.     *s1 = 0;
  165.     return rv;
  166.     }
  167.  
  168.  char *
  169. #ifdef KR_headers
  170. cpstring(s)
  171.     register char *s;
  172. #else
  173. cpstring(register char *s)
  174. #endif
  175. {
  176.     return strcpy(mem(strlen(s)+1,0), s);
  177.     }
  178.  
  179.  void
  180. #ifdef KR_headers
  181. new_iob_data(ios, name)
  182.     register io_setup *ios;
  183.     char *name;
  184. #else
  185. new_iob_data(register io_setup *ios, char *name)
  186. #endif
  187. {
  188.     register iob_data *iod;
  189.     register char **s, **se;
  190.  
  191.     iod = (iob_data *)
  192.         mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
  193.     iod->next = iob_list;
  194.     iob_list = iod;
  195.     iod->type = ios->fields[0];
  196.     iod->name = cpstring(name);
  197.     s = iod->fields;
  198.     se = s + ios->nelt;
  199.     while(s < se)
  200.         *s++ = "0";
  201.     *s = 0;
  202.     }
  203.  
  204.  char *
  205. #ifdef KR_headers
  206. string_num(pfx, n)
  207.     char *pfx;
  208.     long n;
  209. #else
  210. string_num(char *pfx, long n)
  211. #endif
  212. {
  213.     char buf[32];
  214.     sprintf(buf, "%s%ld", pfx, n);
  215.     /* can't trust return type of sprintf -- BSD gets it wrong */
  216.     return strcpy(mem(strlen(buf)+1,0), buf);
  217.     }
  218.  
  219. static defines *define_list;
  220.  
  221.  void
  222. #ifdef KR_headers
  223. def_start(outfile, s1, s2, post)
  224.     FILE *outfile;
  225.     char *s1;
  226.     char *s2;
  227.     char *post;
  228. #else
  229. def_start(FILE *outfile, char *s1, char *s2, char *post)
  230. #endif
  231. {
  232.     defines *d;
  233.     int n, n1;
  234.     extern int in_define;
  235.  
  236.     n = n1 = strlen(s1);
  237.     if (s2)
  238.         n += strlen(s2);
  239.     d = (defines *)mem(sizeof(defines)+n, 1);
  240.     d->next = define_list;
  241.     define_list = d;
  242.     strcpy(d->defname, s1);
  243.     if (s2)
  244.         strcpy(d->defname + n1, s2);
  245.     in_define = 1;
  246.     nice_printf(outfile, "#define %s", d->defname);
  247.     if (post)
  248.         nice_printf(outfile, " %s", post);
  249.     }
  250.  
  251.  void
  252. #ifdef KR_headers
  253. other_undefs(outfile)
  254.     FILE *outfile;
  255. #else
  256. other_undefs(FILE *outfile)
  257. #endif
  258. {
  259.     defines *d;
  260.     if (d = define_list) {
  261.         define_list = 0;
  262.         nice_printf(outfile, "\n");
  263.         do
  264.             nice_printf(outfile, "#undef %s\n", d->defname);
  265.             while(d = d->next);
  266.         nice_printf(outfile, "\n");
  267.         }
  268.     }
  269.