Back to index

radiance  4R0+20100331
savqstr.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: savqstr.c,v 2.9 2003/07/30 10:11:06 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Save unshared strings.
00006  *
00007  *  External symbols declared in standard.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include <stdlib.h>
00013 
00014 #include "rtio.h"
00015 #include "rterror.h"
00016 
00017 
00018 #if 1
00019 
00020 char *
00021 savqstr(s)                  /* save a private string */
00022 register char  *s;
00023 {
00024        register char  *cp;
00025        char  *newp;
00026 
00027        for (cp = s; *cp++; )                     /* compute strlen()+1 */
00028               ;
00029        newp = (char *)malloc(cp-s);
00030        if (newp == NULL) {
00031               eputs("out of memory in savqstr");
00032               quit(1);
00033        }
00034        for (cp = newp; (*cp++ = *s++); )         /* inline strcpy() */
00035               ;
00036        return(newp);                      /* return new location */
00037 }
00038 
00039 
00040 void
00041 freeqstr(s)                 /* free a private string */
00042 char  *s;
00043 {
00044        if (s != NULL)
00045               free((void *)s);
00046 }
00047 
00048 #else
00049 
00050 /*
00051  *  Save unshared strings, packing them together into
00052  *  large blocks to optimize paging in VM environments.
00053  */
00054 
00055 #ifdef  SMLMEM
00056 #ifndef  MINBLOCK
00057 #define  MINBLOCK    (1<<10)              /* minimum allocation block size */
00058 #endif
00059 #ifndef  MAXBLOCK
00060 #define  MAXBLOCK    (1<<14)              /* maximum allocation block size */
00061 #endif
00062 #else
00063 #ifndef  MINBLOCK
00064 #define  MINBLOCK    (1<<12)              /* minimum allocation block size */
00065 #endif
00066 #ifndef  MAXBLOCK
00067 #define  MAXBLOCK    (1<<16)              /* maximum allocation block size */
00068 #endif
00069 #endif
00070 
00071 extern char  *bmalloc();
00072 
00073 
00074 char *
00075 savqstr(s)                  /* save a private string */
00076 register char  *s;
00077 {
00078        static char  *curp = NULL;         /* allocated memory pointer */
00079        static unsigned  nrem = 0;         /* bytes remaining in block */
00080        static unsigned  nextalloc = MINBLOCK;    /* next block size */
00081        register char  *cp;
00082        register unsigned  n;
00083 
00084        for (cp = s; *cp++; )                     /* compute strlen()+1 */
00085               ;
00086        if ((n = cp-s) > nrem) {           /* do we need more core? */
00087               bfree(curp, nrem);                 /* free remnant */
00088               while (n > nextalloc)
00089                      nextalloc <<= 1;
00090               if ((curp = bmalloc(nrem=nextalloc)) == NULL) {
00091                      eputs("out of memory in savqstr");
00092                      quit(1);
00093               }
00094               if ((nextalloc <<= 1) > MAXBLOCK)  /* double block size */
00095                      nextalloc = MAXBLOCK;
00096        }
00097        for (cp = curp; *cp++ = *s++; )           /* inline strcpy() */
00098               ;
00099        s = curp;                          /* update allocation info. */
00100        curp = cp;
00101        nrem -= n;
00102        return(s);                         /* return new location */
00103 }
00104 
00105 
00106 void
00107 freeqstr(s)                 /* free a private string (not recommended) */
00108 char  *s;
00109 {
00110        bfree(s, strlen(s)+1);
00111 }
00112 
00113 #endif