mem.c

Go to the documentation of this file.
00001 /****************************************************************
00002 Copyright 1990, 1991, 1994, 2000 by AT&T, Lucent Technologies and Bellcore.
00003 
00004 Permission to use, copy, modify, and distribute this software
00005 and its documentation for any purpose and without fee is hereby
00006 granted, provided that the above copyright notice appear in all
00007 copies and that both that the copyright notice and this
00008 permission notice and warranty disclaimer appear in supporting
00009 documentation, and that the names of AT&T, Bell Laboratories,
00010 Lucent or Bellcore or any of their entities not be used in
00011 advertising or publicity pertaining to distribution of the
00012 software without specific, written prior permission.
00013 
00014 AT&T, Lucent and Bellcore disclaim all warranties with regard to
00015 this software, including all implied warranties of
00016 merchantability and fitness.  In no event shall AT&T, Lucent or
00017 Bellcore be liable for any special, indirect or consequential
00018 damages or any damages whatsoever resulting from loss of use,
00019 data or profits, whether in an action of contract, negligence or
00020 other tortious action, arising out of or in connection with the
00021 use or performance of this software.
00022 ****************************************************************/
00023 
00024 #include "defs.h"
00025 #include "iob.h"
00026 
00027 #define MEMBSIZE        32000
00028 #define GMEMBSIZE       16000
00029 
00030 #ifdef _WIN32
00031 #undef MSDOS
00032 #endif
00033 
00034  char *
00035 #ifdef KR_headers
00036 gmem(n, round)
00037         int n;
00038         int round;
00039 #else
00040 gmem(int n, int round)
00041 #endif
00042 {
00043         static char *last, *next;
00044         char *rv;
00045         if (round)
00046 #ifdef CRAY
00047                 if ((long)next & 0xe000000000000000)
00048                         next = (char *)(((long)next & 0x1fffffffffffffff) + 1);
00049 #else
00050 #ifdef MSDOS
00051                 if ((int)next & 1)
00052                         next++;
00053 #else
00054                 next = (char *)(((long)next + sizeof(char *)-1)
00055                                 & ~((long)sizeof(char *)-1));
00056 #endif
00057 #endif
00058         rv = next;
00059         if ((next += n) > last) {
00060                 rv = Alloc(n + GMEMBSIZE);
00061 
00062                 next = rv + n;
00063                 last = next + GMEMBSIZE;
00064                 }
00065         return rv;
00066         }
00067 
00068  struct memblock {
00069         struct memblock *next;
00070         char buf[MEMBSIZE];
00071         };
00072  typedef struct memblock memblock;
00073 
00074  static memblock *mem0;
00075  memblock *curmemblock, *firstmemblock;
00076 
00077  char *mem_first, *mem_next, *mem_last, *mem0_last;
00078 
00079  void
00080 mem_init(Void)
00081 {
00082         curmemblock = firstmemblock = mem0
00083                 = (memblock *)Alloc(sizeof(memblock));
00084         mem_first = mem0->buf;
00085         mem_next  = mem0->buf;
00086         mem_last  = mem0->buf + MEMBSIZE;
00087         mem0_last = mem0->buf + MEMBSIZE;
00088         mem0->next = 0;
00089         }
00090 
00091  char *
00092 #ifdef KR_headers
00093 mem(n, round)
00094         int n;
00095         int round;
00096 #else
00097 mem(int n, int round)
00098 #endif
00099 {
00100         memblock *b;
00101         register char *rv, *s;
00102 
00103         if (round)
00104 #ifdef CRAY
00105                 if ((long)mem_next & 0xe000000000000000)
00106                         mem_next = (char *)(((long)mem_next & 0x1fffffffffffffff) + 1);
00107 #else
00108 #ifdef MSDOS
00109                 if ((int)mem_next & 1)
00110                         mem_next++;
00111 #else
00112                 mem_next = (char *)(((long)mem_next + sizeof(char *)-1)
00113                                 & ~((long)sizeof(char *)-1));
00114 #endif
00115 #endif
00116         rv = mem_next;
00117         s = rv + n;
00118         if (s >= mem_last) {
00119                 if (n > MEMBSIZE)  {
00120                         fprintf(stderr, "mem(%d) failure!\n", n);
00121                         exit(1);
00122                         }
00123                 if (!(b = curmemblock->next)) {
00124                         b = (memblock *)Alloc(sizeof(memblock));
00125                         curmemblock->next = b;
00126                         b->next = 0;
00127                         }
00128                 curmemblock = b;
00129                 rv = b->buf;
00130                 mem_last = rv + sizeof(b->buf);
00131                 s = rv + n;
00132                 }
00133         mem_next = s;
00134         return rv;
00135         }
00136 
00137  char *
00138 #ifdef KR_headers
00139 tostring(s, n)
00140         register char *s;
00141         int n;
00142 #else
00143 tostring(register char *s, int n)
00144 #endif
00145 {
00146         register char *s1, *se, **sf;
00147         char *rv, *s0;
00148         register int k = n + 2, t;
00149 
00150         sf = str_fmt;
00151         sf['%'] = "%";
00152         s0 = s;
00153         se = s + n;
00154         for(; s < se; s++) {
00155                 t = *(unsigned char *)s;
00156                 s1 = sf[t];
00157                 while(*++s1)
00158                         k++;
00159                 }
00160         sf['%'] = "%%";
00161         rv = s1 = mem(k,0);
00162         *s1++ = '"';
00163         for(s = s0; s < se; s++) {
00164                 t = *(unsigned char *)s;
00165                 sprintf(s1, sf[t], t);
00166                 s1 += strlen(s1);
00167                 }
00168         *s1 = 0;
00169         return rv;
00170         }
00171 
00172  char *
00173 #ifdef KR_headers
00174 cpstring(s)
00175         register char *s;
00176 #else
00177 cpstring(register char *s)
00178 #endif
00179 {
00180         return strcpy(mem(strlen(s)+1,0), s);
00181         }
00182 
00183  void
00184 #ifdef KR_headers
00185 new_iob_data(ios, name)
00186         register io_setup *ios;
00187         char *name;
00188 #else
00189 new_iob_data(register io_setup *ios, char *name)
00190 #endif
00191 {
00192         register iob_data *iod;
00193         register char **s, **se;
00194 
00195         iod = (iob_data *)
00196                 mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
00197         iod->next = iob_list;
00198         iob_list = iod;
00199         iod->type = ios->fields[0];
00200         iod->name = cpstring(name);
00201         s = iod->fields;
00202         se = s + ios->nelt;
00203         while(s < se)
00204                 *s++ = "0";
00205         *s = 0;
00206         }
00207 
00208  char *
00209 #ifdef KR_headers
00210 string_num(pfx, n)
00211         char *pfx;
00212         long n;
00213 #else
00214 string_num(char *pfx, long n)
00215 #endif
00216 {
00217         char buf[32];
00218         sprintf(buf, "%s%ld", pfx, n);
00219         /* can't trust return type of sprintf -- BSD gets it wrong */
00220         return strcpy(mem(strlen(buf)+1,0), buf);
00221         }
00222 
00223 static defines *define_list;
00224 
00225  void
00226 #ifdef KR_headers
00227 def_start(outfile, s1, s2, post)
00228         FILE *outfile;
00229         char *s1;
00230         char *s2;
00231         char *post;
00232 #else
00233 def_start(FILE *outfile, char *s1, char *s2, char *post)
00234 #endif
00235 {
00236         defines *d;
00237         int n, n1;
00238         extern int in_define;
00239 
00240         n = n1 = strlen(s1);
00241         if (s2)
00242                 n += strlen(s2);
00243         d = (defines *)mem(sizeof(defines)+n, 1);
00244         d->next = define_list;
00245         define_list = d;
00246         strcpy(d->defname, s1);
00247         if (s2)
00248                 strcpy(d->defname + n1, s2);
00249         in_define = 1;
00250         nice_printf(outfile, "#define %s", d->defname);
00251         if (post)
00252                 nice_printf(outfile, " %s", post);
00253         }
00254 
00255  void
00256 #ifdef KR_headers
00257 other_undefs(outfile)
00258         FILE *outfile;
00259 #else
00260 other_undefs(FILE *outfile)
00261 #endif
00262 {
00263         defines *d;
00264         if (d = define_list) {
00265                 define_list = 0;
00266                 nice_printf(outfile, "\n");
00267                 do
00268                         nice_printf(outfile, "#undef %s\n", d->defname);
00269                         while(d = d->next);
00270                 nice_printf(outfile, "\n");
00271                 }
00272         }

Generated on Sun Mar 4 15:03:44 2007 for Scilab [trunk] by  doxygen 1.5.1