#include "make.h"
#include <assert.h>
#include "hash.h"
#define CACHE_BUFFER_SIZE (4096)
struct strcache {
struct strcache *next;
char *end;
int count;
int bytesfree;
char buffer[1];
};
static int bufsize = CACHE_BUFFER_SIZE;
static struct strcache *strcache = NULL;
static struct strcache *
new_cache()
{
struct strcache *new;
new = (struct strcache *) xmalloc (sizeof (*new) + bufsize);
new->end = new->buffer;
new->count = 0;
new->bytesfree = bufsize;
new->next = strcache;
strcache = new;
return new;
}
static const char *
add_string(const char *str, int len)
{
struct strcache *best = NULL;
struct strcache *sp;
const char *res;
if (len > bufsize)
bufsize = len * 2;
for (sp = strcache; sp != NULL; sp = sp->next)
if (sp->bytesfree > len && (!best || best->bytesfree > sp->bytesfree))
best = sp;
if (!best)
best = new_cache();
assert (best->bytesfree > len);
res = best->end;
memcpy (best->end, str, len);
best->end += len;
*(best->end++) = '\0';
best->bytesfree -= len + 1;
++best->count;
return res;
}
static unsigned long
str_hash_1 (const void *key)
{
return_ISTRING_HASH_1 ((const char *) key);
}
static unsigned long
str_hash_2 (const void *key)
{
return_ISTRING_HASH_2 ((const char *) key);
}
static int
str_hash_cmp (const void *x, const void *y)
{
return_ISTRING_COMPARE ((const char *) x, (const char *) y);
}
static struct hash_table strings;
static const char *
add_hash (const char *str, int len)
{
char **slot = (char **) hash_find_slot (&strings, str);
const char *key = *slot;
if (!HASH_VACANT (key))
return key;
key = add_string (str, len);
hash_insert_at (&strings, key, slot);
return key;
}
int
strcache_iscached (const char *str)
{
struct strcache *sp;
for (sp = strcache; sp != 0; sp = sp->next)
if (str >= sp->buffer && str < sp->end)
return 1;
return 0;
}
const char *
strcache_add (const char *str)
{
return add_hash (str, strlen (str));
}
const char *
strcache_add_len (const char *str, int len)
{
char *key = alloca (len + 1);
memcpy (key, str, len);
key[len] = '\0';
return add_hash (key, len);
}
int
strcache_setbufsize(int size)
{
if (size > bufsize)
bufsize = size;
return bufsize;
}
void
strcache_init (void)
{
hash_init (&strings, 1000, str_hash_1, str_hash_2, str_hash_cmp);
}
void
strcache_print_stats (const char *prefix)
{
int numbuffs = 0, numstrs = 0;
int totsize = 0, avgsize, maxsize = 0, minsize = bufsize;
int totfree = 0, avgfree, maxfree = 0, minfree = bufsize;
const struct strcache *sp;
for (sp = strcache; sp != NULL; sp = sp->next)
{
int bf = sp->bytesfree;
int sz = (sp->end - sp->buffer) + bf;
++numbuffs;
numstrs += sp->count;
totsize += sz;
maxsize = (sz > maxsize ? sz : maxsize);
minsize = (sz < minsize ? sz : minsize);
totfree += bf;
maxfree = (bf > maxfree ? bf : maxfree);
minfree = (bf < minfree ? bf : minfree);
}
avgsize = numbuffs ? (int)(totsize / numbuffs) : 0;
avgfree = numbuffs ? (int)(totfree / numbuffs) : 0;
printf (_("\n%s # of strings in strcache: %d\n"), prefix, numstrs);
printf (_("%s # of strcache buffers: %d\n"), prefix, numbuffs);
printf (_("%s strcache size: total = %d / max = %d / min = %d / avg = %d\n"),
prefix, totsize, maxsize, minsize, avgsize);
printf (_("%s strcache free: total = %d / max = %d / min = %d / avg = %d\n"),
prefix, totfree, maxfree, minfree, avgfree);
}