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