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, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * newstr.c - string manipulation routines
20 * To minimize string copying, string creation, copying, and freeing
21 * is done through newstr.
25 * newstr() - return a malloc'ed copy of a string
26 * copystr() - return a copy of a string previously returned by newstr()
27 * freestr() - free a string returned by newstr() or copystr()
28 * donestr() - free string tables
30 * Once a string is passed to newstr(), the returned string is readonly.
32 * This implementation builds a hash table of all strings, so that multiple
33 * calls of newstr() on the same string allocate memory for the string once.
34 * Strings are never actually freed.
41 typedef const char *STRING
;
43 static struct hash
*strhash
= 0;
44 static int strtotal
= 0;
48 * newstr() - return a malloc'ed copy of a string
50 const char *newstr (const char *string
) {
51 STRING str
, *s
= &str
;
52 if (!strhash
) strhash
= hashinit(sizeof(STRING
), "strings");
54 if (hashenter(strhash
, (HASHDATA
**)&s
)) {
55 int l
= strlen(string
);
56 char *m
= (char *)malloc(l
+1);
57 if (DEBUG_MEM
) printf("newstr: allocating %d bytes\n", l
+1);
59 memcpy(m
, string
, l
+1);
67 * copystr() - return a copy of a string previously returned by newstr()
69 JAMFA_CONST
const char *copystr (const char *s
) {
75 * freestr() - free a string returned by newstr() or copystr()
77 void freestr (const char *s
) {
82 * donestr() - free string tables
86 if (DEBUG_MEM
) printf("%dK in strings\n", strtotal
/1024);