ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/savqstr.c
Revision: 2.9
Committed: Wed Jul 30 10:11:06 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.8: +2 -1 lines
Log Message:
Added prototypes submitted by Randolph Fritz.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.9 static const char RCSid[] = "$Id: savqstr.c,v 2.8 2003/07/27 22:12:01 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     savqstr(s) /* save a private string */
22     register char *s;
23     {
24     register char *cp;
25     char *newp;
26    
27     for (cp = s; *cp++; ) /* compute strlen()+1 */
28     ;
29     newp = (char *)malloc(cp-s);
30     if (newp == NULL) {
31     eputs("out of memory in savqstr");
32     quit(1);
33     }
34 schorsch 2.8 for (cp = newp; (*cp++ = *s++); ) /* inline strcpy() */
35 greg 2.3 ;
36     return(newp); /* return new location */
37     }
38    
39    
40     void
41     freeqstr(s) /* free a private string */
42     char *s;
43     {
44 greg 2.5 if (s != NULL)
45     free((void *)s);
46 greg 2.3 }
47    
48     #else
49 greg 1.1
50     /*
51 greg 2.2 * Save unshared strings, packing them together into
52     * large blocks to optimize paging in VM environments.
53 greg 1.1 */
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 extern char *bmalloc();
72 greg 1.1
73 greg 2.2
74 greg 1.1 char *
75     savqstr(s) /* save a private string */
76 greg 2.2 register char *s;
77 greg 1.1 {
78 greg 2.2 static char *curp = NULL; /* allocated memory pointer */
79     static unsigned nrem = 0; /* bytes remaining in block */
80     static unsigned nextalloc = MINBLOCK; /* next block size */
81 greg 1.1 register char *cp;
82 greg 2.2 register unsigned n;
83 greg 1.1
84 greg 2.2 for (cp = s; *cp++; ) /* compute strlen()+1 */
85     ;
86     if ((n = cp-s) > nrem) { /* do we need more core? */
87     bfree(curp, nrem); /* free remnant */
88     while (n > nextalloc)
89     nextalloc <<= 1;
90     if ((curp = bmalloc(nrem=nextalloc)) == NULL) {
91     eputs("out of memory in savqstr");
92     quit(1);
93     }
94     if ((nextalloc <<= 1) > MAXBLOCK) /* double block size */
95     nextalloc = MAXBLOCK;
96 greg 1.1 }
97 greg 2.2 for (cp = curp; *cp++ = *s++; ) /* inline strcpy() */
98     ;
99     s = curp; /* update allocation info. */
100     curp = cp;
101     nrem -= n;
102     return(s); /* return new location */
103 greg 1.1 }
104    
105    
106 greg 2.3 void
107 greg 2.2 freeqstr(s) /* free a private string (not recommended) */
108 greg 1.1 char *s;
109     {
110     bfree(s, strlen(s)+1);
111     }
112 greg 2.3
113     #endif