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