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