1 |
greg |
2.2 |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
greg |
2.2 |
* Save unshared strings, packing them together into |
9 |
|
|
* large blocks to optimize paging in VM environments. |
10 |
greg |
1.1 |
*/ |
11 |
|
|
|
12 |
|
|
#define NULL 0 |
13 |
|
|
|
14 |
greg |
2.2 |
#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 |
greg |
1.1 |
|
30 |
greg |
2.2 |
extern char *bmalloc(); |
31 |
greg |
1.1 |
|
32 |
greg |
2.2 |
|
33 |
greg |
1.1 |
char * |
34 |
|
|
savqstr(s) /* save a private string */ |
35 |
greg |
2.2 |
register char *s; |
36 |
greg |
1.1 |
{ |
37 |
greg |
2.2 |
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 |
greg |
1.1 |
register char *cp; |
41 |
greg |
2.2 |
register unsigned n; |
42 |
greg |
1.1 |
|
43 |
greg |
2.2 |
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 |
greg |
1.1 |
} |
56 |
greg |
2.2 |
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 |
greg |
1.1 |
} |
63 |
|
|
|
64 |
|
|
|
65 |
greg |
2.2 |
freeqstr(s) /* free a private string (not recommended) */ |
66 |
greg |
1.1 |
char *s; |
67 |
|
|
{ |
68 |
|
|
bfree(s, strlen(s)+1); |
69 |
|
|
} |