1
/* *** This file was borrowed from jam 2.5. The copyright statement from
2
* *** jam.c appears below.
6
* +\ Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
9
* This file is part of jam.
11
* License is hereby granted to use this software and distribute it
12
* freely, as long as this copyright notice is retained and modifications
15
* ALL WARRANTIES ARE HEREBY DISCLAIMED.
19
* Copyright 1993, 1995 Christopher Seiwald.
21
* This file is part of Jam - see jam.c for Copyright information.
25
* newstr.c - string manipulation routines
27
* To minimize string copying, string creation, copying, and freeing
28
* is done through newstr.
32
* newstr() - return a malloc'ed copy of a string
33
* copystr() - return a copy of a string previously returned by newstr()
34
* freestr() - free a string returned by newstr() or copystr()
35
* donestr() - free string tables
37
* Once a string is passed to newstr(), the returned string is readonly.
39
* This implementation builds a hash table of all strings, so that multiple
40
* calls of newstr() on the same string allocate memory for the string once.
41
* Strings are never actually freed.
43
* 11/04/02 (seiwald) - const-ing for string literals
52
static ALLOC *stralloc = 0;
55
typedef const char *STRING;
57
static struct hash *strhash = 0;
58
static int strtotal = 0;
61
* newstr() - return a malloc'ed copy of a string
65
newstr( const char *string )
67
STRING str, *s = &str;
70
strhash = hashinit( sizeof( STRING ), "strings" );
74
if( hashenter( strhash, (HASHDATA **)&s ) )
76
int l = strlen( string );
79
stralloc = alloc2_init(4096);
80
char *m = alloc2_enter(stralloc, l + 1);
82
char *m = (char *)malloc( l + 1 );
85
printf("newstr: allocating %d bytes\n", l + 1 );
88
memcpy( m, string, l + 1 );
96
* copystr() - return a copy of a string previously returned by newstr()
100
copystr( const char *s )
106
* freestr() - free a string returned by newstr() or copystr()
110
freestr( const char *s )
115
* donestr() - free string tables
122
strhash = 0; /* TNB */
123
strtotal = 0; /* TNB */
124
alloc_free(stralloc); /* TNB */
125
stralloc = 0; /* TNB */
127
printf( "%dK in strings\n", strtotal / 1024 );