small irrelevant typo
[k8jam.git] / newstr.c
blob07e1fca61daacca87ff9e455f204e028146593a0
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * newstr.c - string manipulation routines
10 * To minimize string copying, string creation, copying, and freeing
11 * is done through newstr.
13 * External functions:
15 * newstr() - return a malloc'ed copy of a string
16 * copystr() - return a copy of a string previously returned by newstr()
17 * freestr() - free a string returned by newstr() or copystr()
18 * donestr() - free string tables
20 * Once a string is passed to newstr(), the returned string is readonly.
22 * This implementation builds a hash table of all strings, so that multiple
23 * calls of newstr() on the same string allocate memory for the string once.
24 * Strings are never actually freed.
26 * 11/04/02 (seiwald) - const-ing for string literals
29 # include "jam.h"
30 # include "newstr.h"
31 # include "hash.h"
33 typedef const char *STRING;
35 static struct hash *strhash = 0;
36 static int strtotal = 0;
40 * newstr() - return a malloc'ed copy of a string
42 const char *newstr (const char *string) {
43 STRING str, *s = &str;
45 if (!strhash) strhash = hashinit(sizeof(STRING), "strings");
46 *s = string;
47 if (hashenter(strhash, (HASHDATA **)&s)) {
48 int l = strlen(string);
49 char *m = (char *)malloc(l+1);
51 if (DEBUG_MEM) printf("newstr: allocating %d bytes\n", l+1);
52 strtotal += l+1;
53 memcpy(m, string, l+1);
54 *s = m;
56 return *s;
61 * copystr() - return a copy of a string previously returned by newstr()
63 const char *copystr (const char *s) {
64 return s;
69 * freestr() - free a string returned by newstr() or copystr()
71 void freestr (const char *s) {
76 * donestr() - free string tables
78 void donestr (void) {
79 hashdone(strhash);
80 if (DEBUG_MEM) printf("%dK in strings\n", strtotal/1024);