cosmetic fix in -configure-pkg-config-var-
[k8jam.git] / src / newstr.c
blob586d57f0a644649f3c619bd961a77a56e05add42
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * newstr.c - string manipulation routines
21 * To minimize string copying, string creation, copying, and freeing
22 * is done through newstr.
24 * External functions:
26 * newstr() - return a malloc'ed copy of a string
27 * copystr() - return a copy of a string previously returned by newstr()
28 * freestr() - free a string returned by newstr() or copystr()
29 * donestr() - free string tables
31 * Once a string is passed to newstr(), the returned string is readonly.
33 * This implementation builds a hash table of all strings, so that multiple
34 * calls of newstr() on the same string allocate memory for the string once.
35 * Strings are never actually freed.
37 #include "jam.h"
38 #include "newstr.h"
39 #include "hash.h"
42 typedef const char *STRING;
44 static struct hash *strhash = 0;
45 static int strtotal = 0;
49 * newstr() - return a malloc'ed copy of a string
51 const char *newstr (const char *string) {
52 STRING str, *s = &str;
53 if (!strhash) strhash = hashinit(sizeof(STRING), "strings");
54 *s = string;
55 if (hashenter(strhash, (HASHDATA **)&s)) {
56 int l = strlen(string);
57 char *m = (char *)malloc(l+1);
58 if (DEBUG_MEM) printf("newstr: allocating %d bytes\n", l+1);
59 strtotal += l+1;
60 memcpy(m, string, l+1);
61 *s = m;
63 return *s;
68 * copystr() - return a copy of a string previously returned by newstr()
70 JAMFA_CONST const char *copystr (const char *s) {
71 return s;
76 * freestr() - free a string returned by newstr() or copystr()
78 JAMFA_CONST void freestr (const char *s) {
83 * donestr() - free string tables
85 void donestr (void) {
86 hashdone(strhash);
87 if (DEBUG_MEM) printf("%dK in strings\n", strtotal/1024);