ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/savqstr.c
(Generate patch)

Comparing ray/src/common/savqstr.c (file contents):
Revision 2.1 by greg, Tue Nov 12 16:56:07 1991 UTC vs.
Revision 2.2 by greg, Wed Jan 27 20:23:49 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
6  
7   /*
8 < *  Quick and dirty string saver.
8 > *  Save unshared strings, packing them together into
9 > *  large blocks to optimize paging in VM environments.
10   */
11  
12   #define  NULL           0
13  
14 < extern char  *strcpy(), *strcat(), *bmalloc();
14 > #ifdef  BIGMEM
15 > #ifndef  MINBLOCK
16 > #define  MINBLOCK       (1<<12)         /* minimum allocation block size */
17 > #endif
18 > #ifndef  MAXBLOCK
19 > #define  MAXBLOCK       (1<<16)         /* maximum allocation block size */
20 > #endif
21 > #else
22 > #ifndef  MINBLOCK
23 > #define  MINBLOCK       (1<<10)         /* minimum allocation block size */
24 > #endif
25 > #ifndef  MAXBLOCK
26 > #define  MAXBLOCK       (1<<14)         /* maximum allocation block size */
27 > #endif
28 > #endif
29  
30 + extern char  *bmalloc();
31  
32 +
33   char *
34   savqstr(s)                      /* save a private string */
35 < char  *s;
35 > register char  *s;
36   {
37 +        static char  *curp = NULL;              /* allocated memory pointer */
38 +        static unsigned  nrem = 0;              /* bytes remaining in block */
39 +        static unsigned  nextalloc = MINBLOCK;  /* next block size */
40          register char  *cp;
41 +        register unsigned  n;
42  
43 <        if ((cp = bmalloc(strlen(s)+1)) == NULL) {
44 <                eputs("out of memory in savqstr");
45 <                quit(1);
43 >        for (cp = s; *cp++; )                   /* compute strlen()+1 */
44 >                ;
45 >        if ((n = cp-s) > nrem) {                /* do we need more core? */
46 >                bfree(curp, nrem);                      /* free remnant */
47 >                while (n > nextalloc)
48 >                        nextalloc <<= 1;
49 >                if ((curp = bmalloc(nrem=nextalloc)) == NULL) {
50 >                        eputs("out of memory in savqstr");
51 >                        quit(1);
52 >                }
53 >                if ((nextalloc <<= 1) > MAXBLOCK)       /* double block size */
54 >                        nextalloc = MAXBLOCK;
55          }
56 <        (void)strcpy(cp, s);
57 <        return(cp);
56 >        for (cp = curp; *cp++ = *s++; )         /* inline strcpy() */
57 >                ;
58 >        s = curp;                               /* update allocation info. */
59 >        curp = cp;
60 >        nrem -= n;
61 >        return(s);                              /* return new location */
62   }
63  
64  
65 < freeqstr(s)                     /* free a private string */
65 > freeqstr(s)                     /* free a private string (not recommended) */
66   char  *s;
67   {
68          bfree(s, strlen(s)+1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines