ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/savqstr.c
Revision: 2.10
Committed: Fri Jun 1 19:17:17 2012 UTC (11 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R2P1, rad5R3
Changes since 2.9: +10 -14 lines
Log Message:
Minor optimizations

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.10 static const char RCSid[] = "$Id: savqstr.c,v 2.9 2003/07/30 10:11:06 schorsch Exp $";
3 greg 1.1 #endif
4 greg 2.3 /*
5     * Save unshared strings.
6     *
7     * External symbols declared in standard.h
8     */
9    
10 greg 2.4 #include "copyright.h"
11 greg 2.3
12     #include <stdlib.h>
13    
14 schorsch 2.9 #include "rtio.h"
15 schorsch 2.7 #include "rterror.h"
16    
17 greg 2.3
18     #if 1
19    
20     char *
21 greg 2.10 savqstr(char *s) /* save a private string */
22 greg 2.3 {
23 greg 2.10 char *cp;
24 greg 2.3 char *newp;
25    
26     for (cp = s; *cp++; ) /* compute strlen()+1 */
27     ;
28     newp = (char *)malloc(cp-s);
29     if (newp == NULL) {
30     eputs("out of memory in savqstr");
31     quit(1);
32     }
33 schorsch 2.8 for (cp = newp; (*cp++ = *s++); ) /* inline strcpy() */
34 greg 2.3 ;
35     return(newp); /* return new location */
36     }
37    
38    
39     void
40 greg 2.10 freeqstr(char *s) /* free a private string */
41 greg 2.3 {
42 greg 2.5 if (s != NULL)
43     free((void *)s);
44 greg 2.3 }
45    
46     #else
47 greg 1.1
48     /*
49 greg 2.2 * Save unshared strings, packing them together into
50     * large blocks to optimize paging in VM environments.
51 greg 1.1 */
52    
53 greg 2.10 #include "rtmisc.h"
54    
55 greg 2.6 #ifdef SMLMEM
56 greg 2.2 #ifndef MINBLOCK
57 greg 2.6 #define MINBLOCK (1<<10) /* minimum allocation block size */
58 greg 2.2 #endif
59     #ifndef MAXBLOCK
60 greg 2.6 #define MAXBLOCK (1<<14) /* maximum allocation block size */
61 greg 2.2 #endif
62     #else
63     #ifndef MINBLOCK
64 greg 2.6 #define MINBLOCK (1<<12) /* minimum allocation block size */
65 greg 2.2 #endif
66     #ifndef MAXBLOCK
67 greg 2.6 #define MAXBLOCK (1<<16) /* maximum allocation block size */
68 greg 2.2 #endif
69     #endif
70 greg 1.1
71 greg 2.2
72 greg 1.1 char *
73 greg 2.10 savqstr(char *s) /* save a private string */
74 greg 1.1 {
75 greg 2.2 static char *curp = NULL; /* allocated memory pointer */
76     static unsigned nrem = 0; /* bytes remaining in block */
77     static unsigned nextalloc = MINBLOCK; /* next block size */
78 greg 2.10 char *cp;
79     unsigned n;
80 greg 1.1
81 greg 2.2 for (cp = s; *cp++; ) /* compute strlen()+1 */
82     ;
83     if ((n = cp-s) > nrem) { /* do we need more core? */
84     bfree(curp, nrem); /* free remnant */
85     while (n > nextalloc)
86     nextalloc <<= 1;
87     if ((curp = bmalloc(nrem=nextalloc)) == NULL) {
88     eputs("out of memory in savqstr");
89     quit(1);
90     }
91     if ((nextalloc <<= 1) > MAXBLOCK) /* double block size */
92     nextalloc = MAXBLOCK;
93 greg 1.1 }
94 greg 2.2 for (cp = curp; *cp++ = *s++; ) /* inline strcpy() */
95     ;
96     s = curp; /* update allocation info. */
97     curp = cp;
98     nrem -= n;
99     return(s); /* return new location */
100 greg 1.1 }
101    
102    
103 greg 2.3 void
104 greg 2.10 freeqstr(char *s) /* free a private string (not recommended) */
105 greg 1.1 {
106     bfree(s, strlen(s)+1);
107     }
108 greg 2.3
109     #endif