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
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 Boston, MA 02110-1301 USA. */
26 /* The size (in bytes) of each cache buffer. */
27 #define CACHE_BUFFER_SIZE (4096)
30 /* A string cached here will never be freed, so we don't need to worry about
31 reference counting. We just store the string, and then remember it in a
32 hash so it can be looked up again. */
35 struct strcache
*next
; /* The next block of strings. */
36 char *end
; /* Pointer to the beginning of the free space. */
37 int count
; /* # of strings in this buffer (for stats). */
38 int bytesfree
; /* The amount of the buffer that is free. */
39 char buffer
[1]; /* The buffer comes after this. */
42 static int bufsize
= CACHE_BUFFER_SIZE
;
43 static struct strcache
*strcache
= NULL
;
45 static struct strcache
*
49 new = (struct strcache
*) xmalloc (sizeof (*new) + bufsize
);
50 new->end
= new->buffer
;
52 new->bytesfree
= bufsize
;
61 add_string(const char *str
, int len
)
63 struct strcache
*best
= NULL
;
67 /* If the string we want is too large to fit into a single buffer, then we're
68 screwed; nothing will ever fit! Change the maximum size of the cache to
73 /* First, find a cache with enough free space. We always look through all
74 the blocks and choose the one with the best fit (the one that leaves the
75 least amount of space free). */
76 for (sp
= strcache
; sp
!= NULL
; sp
= sp
->next
)
77 if (sp
->bytesfree
> len
&& (!best
|| best
->bytesfree
> sp
->bytesfree
))
80 /* If nothing is big enough, make a new cache. */
84 assert (best
->bytesfree
> len
);
86 /* Add the string to the best cache. */
88 memcpy (best
->end
, str
, len
);
90 *(best
->end
++) = '\0';
91 best
->bytesfree
-= len
+ 1;
98 /* Hash table of strings in the cache. */
101 str_hash_1 (const void *key
)
103 return_ISTRING_HASH_1 ((const char *) key
);
107 str_hash_2 (const void *key
)
109 return_ISTRING_HASH_2 ((const char *) key
);
113 str_hash_cmp (const void *x
, const void *y
)
115 return_ISTRING_COMPARE ((const char *) x
, (const char *) y
);
118 static struct hash_table strings
;
121 add_hash (const char *str
, int len
)
123 /* Look up the string in the hash. If it's there, return it. */
124 char **slot
= (char **) hash_find_slot (&strings
, str
);
125 const char *key
= *slot
;
127 if (!HASH_VACANT (key
))
130 /* Not there yet so add it to a buffer, then into the hash table. */
131 key
= add_string (str
, len
);
132 hash_insert_at (&strings
, key
, slot
);
136 /* Returns true if the string is in the cache; false if not. */
138 strcache_iscached (const char *str
)
142 for (sp
= strcache
; sp
!= 0; sp
= sp
->next
)
143 if (str
>= sp
->buffer
&& str
< sp
->end
)
149 /* If the string is already in the cache, return a pointer to the cached
150 version. If not, add it then return a pointer to the cached version.
151 Note we do NOT take control of the string passed in. */
153 strcache_add (const char *str
)
155 return add_hash (str
, strlen (str
));
159 strcache_add_len (const char *str
, int len
)
161 char *key
= alloca (len
+ 1);
162 memcpy (key
, str
, len
);
165 return add_hash (key
, len
);
169 strcache_setbufsize(int size
)
179 hash_init (&strings
, 1000, str_hash_1
, str_hash_2
, str_hash_cmp
);
183 /* Generate some stats output. */
186 strcache_print_stats (const char *prefix
)
188 int numbuffs
= 0, numstrs
= 0;
189 int totsize
= 0, avgsize
, maxsize
= 0, minsize
= bufsize
;
190 int totfree
= 0, avgfree
, maxfree
= 0, minfree
= bufsize
;
191 const struct strcache
*sp
;
193 for (sp
= strcache
; sp
!= NULL
; sp
= sp
->next
)
195 int bf
= sp
->bytesfree
;
196 int sz
= (sp
->end
- sp
->buffer
) + bf
;
199 numstrs
+= sp
->count
;
202 maxsize
= (sz
> maxsize
? sz
: maxsize
);
203 minsize
= (sz
< minsize
? sz
: minsize
);
206 maxfree
= (bf
> maxfree
? bf
: maxfree
);
207 minfree
= (bf
< minfree
? bf
: minfree
);
210 avgsize
= numbuffs
? (int)(totsize
/ numbuffs
) : 0;
211 avgfree
= numbuffs
? (int)(totfree
/ numbuffs
) : 0;
213 printf ("\n%s # of strings in strcache: %d\n", prefix
, numstrs
);
214 printf ("%s # of strcache buffers: %d\n", prefix
, numbuffs
);
215 printf ("%s strcache size: total = %d / max = %d / min = %d / avg = %d\n",
216 prefix
, totsize
, maxsize
, minsize
, avgsize
);
217 printf ("%s strcache free: total = %d / max = %d / min = %d / avg = %d\n",
218 prefix
, totfree
, maxfree
, minfree
, avgfree
);