#include <sys/types.h>
#include <sys/cpuset.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/pmc.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <pmc.h>
#include <pmclog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "libpmcstat.h"
static LIST_HEAD(,pmcstat_string) pmcstat_string_hash[PMCSTAT_NHASH];
pmcstat_interned_string
pmcstat_string_intern(const char *s)
{
struct pmcstat_string *ps;
const struct pmcstat_string *cps;
int hash, len;
if ((cps = pmcstat_string_lookup(s)) != NULL)
return (cps);
hash = pmcstat_string_compute_hash(s);
len = strlen(s);
if ((ps = malloc(sizeof(*ps))) == NULL)
err(EX_OSERR, "ERROR: Could not intern string");
ps->ps_len = len;
ps->ps_hash = hash;
ps->ps_string = strdup(s);
LIST_INSERT_HEAD(&pmcstat_string_hash[hash], ps, ps_next);
return ((pmcstat_interned_string) ps);
}
const char *
pmcstat_string_unintern(pmcstat_interned_string str)
{
const char *s;
s = ((const struct pmcstat_string *) str)->ps_string;
return (s);
}
int
pmcstat_string_compute_hash(const char *s)
{
unsigned hash;
for (hash = 2166136261; *s; s++)
hash = (hash ^ *s) * 16777619;
return (hash & PMCSTAT_HASH_MASK);
}
pmcstat_interned_string
pmcstat_string_lookup(const char *s)
{
struct pmcstat_string *ps;
int hash, len;
hash = pmcstat_string_compute_hash(s);
len = strlen(s);
LIST_FOREACH(ps, &pmcstat_string_hash[hash], ps_next)
if (ps->ps_len == len && ps->ps_hash == hash &&
strcmp(ps->ps_string, s) == 0)
return (ps);
return (NULL);
}
int
pmcstat_string_lookup_hash(pmcstat_interned_string s)
{
const struct pmcstat_string *ps;
ps = (const struct pmcstat_string *) s;
return (ps->ps_hash);
}
void
pmcstat_string_shutdown(void)
{
int i;
struct pmcstat_string *ps, *pstmp;
for (i = 0; i < PMCSTAT_NHASH; i++)
LIST_FOREACH_SAFE(ps, &pmcstat_string_hash[i], ps_next,
pstmp) {
LIST_REMOVE(ps, ps_next);
free(ps->ps_string);
free(ps);
}
}
void
pmcstat_string_initialize(void)
{
int i;
for (i = 0; i < PMCSTAT_NHASH; i++)
LIST_INIT(&pmcstat_string_hash[i]);
}