Release GNU make 3.81.
[make.git] / strcache.c
blob6cfae42461a5ef997c0f3f6707251220e9d754bd
1 /* Constant string caching for GNU Make.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 2, or (at your option) any later version.
9 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNU Make; see the file COPYING. If not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
17 #include "make.h"
19 #include <assert.h>
21 #include "hash.h"
23 /* The size (in bytes) of each cache buffer. */
24 #define CACHE_BUFFER_SIZE (4096)
27 /* A string cached here will never be freed, so we don't need to worry about
28 reference counting. We just store the string, and then remember it in a
29 hash so it can be looked up again. */
31 struct strcache {
32 struct strcache *next; /* The next block of strings. */
33 char *end; /* Pointer to the beginning of the free space. */
34 int count; /* # of strings in this buffer (for stats). */
35 int bytesfree; /* The amount of the buffer that is free. */
36 char buffer[1]; /* The buffer comes after this. */
39 static int bufsize = CACHE_BUFFER_SIZE;
40 static struct strcache *strcache = NULL;
42 static struct strcache *
43 new_cache()
45 struct strcache *new;
46 new = (struct strcache *) xmalloc (sizeof (*new) + bufsize);
47 new->end = new->buffer;
48 new->count = 0;
49 new->bytesfree = bufsize;
51 new->next = strcache;
52 strcache = new;
54 return new;
57 static const char *
58 add_string(const char *str, int len)
60 struct strcache *best = NULL;
61 struct strcache *sp;
62 const char *res;
64 /* If the string we want is too large to fit into a single buffer, then we're
65 screwed; nothing will ever fit! Change the maximum size of the cache to
66 be big enough. */
67 if (len > bufsize)
68 bufsize = len * 2;
70 /* First, find a cache with enough free space. We always look through all
71 the blocks and choose the one with the best fit (the one that leaves the
72 least amount of space free). */
73 for (sp = strcache; sp != NULL; sp = sp->next)
74 if (sp->bytesfree > len && (!best || best->bytesfree > sp->bytesfree))
75 best = sp;
77 /* If nothing is big enough, make a new cache. */
78 if (!best)
79 best = new_cache();
81 assert (best->bytesfree > len);
83 /* Add the string to the best cache. */
84 res = best->end;
85 memcpy (best->end, str, len);
86 best->end += len;
87 *(best->end++) = '\0';
88 best->bytesfree -= len + 1;
89 ++best->count;
91 return res;
95 /* Hash table of strings in the cache. */
97 static unsigned long
98 str_hash_1 (const void *key)
100 return_ISTRING_HASH_1 ((const char *) key);
103 static unsigned long
104 str_hash_2 (const void *key)
106 return_ISTRING_HASH_2 ((const char *) key);
109 static int
110 str_hash_cmp (const void *x, const void *y)
112 return_ISTRING_COMPARE ((const char *) x, (const char *) y);
115 static struct hash_table strings;
117 static const char *
118 add_hash (const char *str, int len)
120 /* Look up the string in the hash. If it's there, return it. */
121 char **slot = (char **) hash_find_slot (&strings, str);
122 const char *key = *slot;
124 if (!HASH_VACANT (key))
125 return key;
127 /* Not there yet so add it to a buffer, then into the hash table. */
128 key = add_string (str, len);
129 hash_insert_at (&strings, key, slot);
130 return key;
133 /* Returns true if the string is in the cache; false if not. */
135 strcache_iscached (const char *str)
137 struct strcache *sp;
139 for (sp = strcache; sp != 0; sp = sp->next)
140 if (str >= sp->buffer && str < sp->end)
141 return 1;
143 return 0;
146 /* If the string is already in the cache, return a pointer to the cached
147 version. If not, add it then return a pointer to the cached version.
148 Note we do NOT take control of the string passed in. */
149 const char *
150 strcache_add (const char *str)
152 return add_hash (str, strlen (str));
155 const char *
156 strcache_add_len (const char *str, int len)
158 char *key = alloca (len + 1);
159 memcpy (key, str, len);
160 key[len] = '\0';
162 return add_hash (key, len);
166 strcache_setbufsize(int size)
168 if (size > bufsize)
169 bufsize = size;
170 return bufsize;
173 void
174 strcache_init (void)
176 hash_init (&strings, 1000, str_hash_1, str_hash_2, str_hash_cmp);
180 /* Generate some stats output. */
182 void
183 strcache_print_stats (const char *prefix)
185 int numbuffs = 0, numstrs = 0;
186 int totsize = 0, avgsize, maxsize = 0, minsize = bufsize;
187 int totfree = 0, avgfree, maxfree = 0, minfree = bufsize;
188 const struct strcache *sp;
190 for (sp = strcache; sp != NULL; sp = sp->next)
192 int bf = sp->bytesfree;
193 int sz = (sp->end - sp->buffer) + bf;
195 ++numbuffs;
196 numstrs += sp->count;
198 totsize += sz;
199 maxsize = (sz > maxsize ? sz : maxsize);
200 minsize = (sz < minsize ? sz : minsize);
202 totfree += bf;
203 maxfree = (bf > maxfree ? bf : maxfree);
204 minfree = (bf < minfree ? bf : minfree);
207 avgsize = numbuffs ? (int)(totsize / numbuffs) : 0;
208 avgfree = numbuffs ? (int)(totfree / numbuffs) : 0;
210 printf (_("\n%s # of strings in strcache: %d\n"), prefix, numstrs);
211 printf (_("%s # of strcache buffers: %d\n"), prefix, numbuffs);
212 printf (_("%s strcache size: total = %d / max = %d / min = %d / avg = %d\n"),
213 prefix, totsize, maxsize, minsize, avgsize);
214 printf (_("%s strcache free: total = %d / max = %d / min = %d / avg = %d\n"),
215 prefix, totfree, maxfree, minfree, avgfree);