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. */
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. */
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
*
46 new = (struct strcache
*) xmalloc (sizeof (*new) + bufsize
);
47 new->end
= new->buffer
;
49 new->bytesfree
= bufsize
;
58 add_string(const char *str
, int len
)
60 struct strcache
*best
= NULL
;
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
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
))
77 /* If nothing is big enough, make a new cache. */
81 assert (best
->bytesfree
> len
);
83 /* Add the string to the best cache. */
85 memcpy (best
->end
, str
, len
);
87 *(best
->end
++) = '\0';
88 best
->bytesfree
-= len
+ 1;
95 /* Hash table of strings in the cache. */
98 str_hash_1 (const void *key
)
100 return_ISTRING_HASH_1 ((const char *) key
);
104 str_hash_2 (const void *key
)
106 return_ISTRING_HASH_2 ((const char *) key
);
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
;
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
))
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
);
133 /* Returns true if the string is in the cache; false if not. */
135 strcache_iscached (const char *str
)
139 for (sp
= strcache
; sp
!= 0; sp
= sp
->next
)
140 if (str
>= sp
->buffer
&& str
< sp
->end
)
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. */
150 strcache_add (const char *str
)
152 return add_hash (str
, strlen (str
));
156 strcache_add_len (const char *str
, int len
)
158 char *key
= alloca (len
+ 1);
159 memcpy (key
, str
, len
);
162 return add_hash (key
, len
);
166 strcache_setbufsize(int size
)
176 hash_init (&strings
, 1000, str_hash_1
, str_hash_2
, str_hash_cmp
);
180 /* Generate some stats output. */
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
;
196 numstrs
+= sp
->count
;
199 maxsize
= (sz
> maxsize
? sz
: maxsize
);
200 minsize
= (sz
< minsize
? sz
: minsize
);
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
);