Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

mem.c

Go to the documentation of this file.
00001 /****************************************************************
00002 Copyright 1990, 1991, 1994 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  char *
00031 #ifdef KR_headers
00032 gmem(n, round)
00033         int n;
00034         int round;
00035 #else
00036 gmem(int n, int round)
00037 #endif
00038 {
00039         static char *last, *next;
00040         char *rv;
00041         if (round)
00042 #ifdef CRAY
00043                 if ((long)next & 0xe000000000000000)
00044                         next = (char *)(((long)next & 0x1fffffffffffffff) + 1);
00045 #else
00046 #ifdef MSDOS
00047                 if ((int)next & 1)
00048                         next++;
00049 #else
00050                 next = (char *)(((long)next + sizeof(char *)-1)
00051                                 & ~((long)sizeof(char *)-1));
00052 #endif
00053 #endif
00054         rv = next;
00055         if ((next += n) > last) {
00056                 rv = Alloc(n + GMEMBSIZE);
00057 
00058                 next = rv + n;
00059                 last = next + GMEMBSIZE;
00060                 }
00061         return rv;
00062         }
00063 
00064  struct memblock {
00065         struct memblock *next;
00066         char buf[MEMBSIZE];
00067         };
00068  typedef struct memblock memblock;
00069 
00070  static memblock *mem0;
00071  memblock *curmemblock, *firstmemblock;
00072 
00073  char *mem_first, *mem_next, *mem_last, *mem0_last;
00074 
00075  void
00076 mem_init(Void)
00077 {
00078         curmemblock = firstmemblock = mem0
00079                 = (memblock *)Alloc(sizeof(memblock));
00080         mem_first = mem0->buf;
00081         mem_next  = mem0->buf;
00082         mem_last  = mem0->buf + MEMBSIZE;
00083         mem0_last = mem0->buf + MEMBSIZE;
00084         mem0->next = 0;
00085         }
00086 
00087  char *
00088 #ifdef KR_headers
00089 mem(n, round)
00090         int n;
00091         int round;
00092 #else
00093 mem(int n, int round)
00094 #endif
00095 {
00096         memblock *b;
00097         register char *rv, *s;
00098 
00099         if (round)
00100 #ifdef CRAY
00101                 if ((long)mem_next & 0xe000000000000000)
00102                         mem_next = (char *)(((long)mem_next & 0x1fffffffffffffff) + 1);
00103 #else
00104 #ifdef MSDOS
00105                 if ((int)mem_next & 1)
00106                         mem_next++;
00107 #else
00108                 mem_next = (char *)(((long)mem_next + sizeof(char *)-1)
00109                                 & ~((long)sizeof(char *)-1));
00110 #endif
00111 #endif
00112         rv = mem_next;
00113         s = rv + n;
00114         if (s >= mem_last) {
00115                 if (n > MEMBSIZE)  {
00116                         fprintf(stderr, "mem(%d) failure!\n", n);
00117                         exit(1);
00118                         }
00119                 if (!(b = curmemblock->next)) {
00120                         b = (memblock *)Alloc(sizeof(memblock));
00121                         curmemblock->next = b;
00122                         b->next = 0;
00123                         }
00124                 curmemblock = b;
00125                 rv = b->buf;
00126                 mem_last = rv + sizeof(b->buf);
00127                 s = rv + n;
00128                 }
00129         mem_next = s;
00130         return rv;
00131         }
00132 
00133  char *
00134 #ifdef KR_headers
00135 tostring(s, n)
00136         register char *s;
00137         int n;
00138 #else
00139 tostring(register char *s, int n)
00140 #endif
00141 {
00142         register char *s1, *se, **sf;
00143         char *rv, *s0;
00144         register int k = n + 2, t;
00145 
00146         sf = str_fmt;
00147         sf['%'] = "%";
00148         s0 = s;
00149         se = s + n;
00150         for(; s < se; s++) {
00151                 t = *(unsigned char *)s;
00152                 s1 = sf[t];
00153                 while(*++s1)
00154                         k++;
00155                 }
00156         sf['%'] = "%%";
00157         rv = s1 = mem(k,0);
00158         *s1++ = '"';
00159         for(s = s0; s < se; s++) {
00160                 t = *(unsigned char *)s;
00161                 sprintf(s1, sf[t], t);
00162                 s1 += strlen(s1);
00163                 }
00164         *s1 = 0;
00165         return rv;
00166         }
00167 
00168  char *
00169 #ifdef KR_headers
00170 cpstring(s)
00171         register char *s;
00172 #else
00173 cpstring(register char *s)
00174 #endif
00175 {
00176         return strcpy(mem(strlen(s)+1,0), s);
00177         }
00178 
00179  void
00180 #ifdef KR_headers
00181 new_iob_data(ios, name)
00182         register io_setup *ios;
00183         char *name;
00184 #else
00185 new_iob_data(register io_setup *ios, char *name)
00186 #endif
00187 {
00188         register iob_data *iod;
00189         register char **s, **se;
00190 
00191         iod = (iob_data *)
00192                 mem(sizeof(iob_data) + ios->nelt*sizeof(char *), 1);
00193         iod->next = iob_list;
00194         iob_list = iod;
00195         iod->type = ios->fields[0];
00196         iod->name = cpstring(name);
00197         s = iod->fields;
00198         se = s + ios->nelt;
00199         while(s < se)
00200                 *s++ = "0";
00201         *s = 0;
00202         }
00203 
00204  char *
00205 #ifdef KR_headers
00206 string_num(pfx, n)
00207         char *pfx;
00208         long n;
00209 #else
00210 string_num(char *pfx, long n)
00211 #endif
00212 {
00213         char buf[32];
00214         sprintf(buf, "%s%ld", pfx, n);
00215         /* can't trust return type of sprintf -- BSD gets it wrong */
00216         return strcpy(mem(strlen(buf)+1,0), buf);
00217         }
00218 
00219 static defines *define_list;
00220 
00221  void
00222 #ifdef KR_headers
00223 def_start(outfile, s1, s2, post)
00224         FILE *outfile;
00225         char *s1;
00226         char *s2;
00227         char *post;
00228 #else
00229 def_start(FILE *outfile, char *s1, char *s2, char *post)
00230 #endif
00231 {
00232         defines *d;
00233         int n, n1;
00234         extern int in_define;
00235 
00236         n = n1 = strlen(s1);
00237         if (s2)
00238                 n += strlen(s2);
00239         d = (defines *)mem(sizeof(defines)+n, 1);
00240         d->next = define_list;
00241         define_list = d;
00242         strcpy(d->defname, s1);
00243         if (s2)
00244                 strcpy(d->defname + n1, s2);
00245         in_define = 1;
00246         nice_printf(outfile, "#define %s", d->defname);
00247         if (post)
00248                 nice_printf(outfile, " %s", post);
00249         }
00250 
00251  void
00252 #ifdef KR_headers
00253 other_undefs(outfile)
00254         FILE *outfile;
00255 #else
00256 other_undefs(FILE *outfile)
00257 #endif
00258 {
00259         defines *d;
00260         if (d = define_list) {
00261                 define_list = 0;
00262                 nice_printf(outfile, "\n");
00263                 do
00264                         nice_printf(outfile, "#undef %s\n", d->defname);
00265                         while(d = d->next);
00266                 nice_printf(outfile, "\n");
00267                 }
00268         }
 

Powered by Plone

This site conforms to the following standards: