2004-12-05 06:44:55 +08:00
|
|
|
#include "sigar.h"
|
|
|
|
#include "sigar_private.h"
|
|
|
|
#include "sigar_util.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hash table to cache values where key is a unique number
|
|
|
|
* such as:
|
|
|
|
* pid -> some process data
|
|
|
|
* uid -> user name
|
|
|
|
* gid -> group name
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ENTRIES_SIZE(n) \
|
|
|
|
(sizeof(sigar_cache_entry_t *) * (n))
|
|
|
|
|
2005-11-10 03:31:02 +08:00
|
|
|
/* wrap free() for use w/ dmalloc */
|
|
|
|
static void free_value(void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2004-12-05 06:44:55 +08:00
|
|
|
sigar_cache_t *sigar_cache_new(int size)
|
|
|
|
{
|
|
|
|
sigar_cache_t *table = malloc(sizeof(*table));
|
|
|
|
table->count = 0;
|
|
|
|
table->size = size;
|
|
|
|
table->entries = malloc(ENTRIES_SIZE(size));
|
|
|
|
memset(table->entries, '\0', ENTRIES_SIZE(size));
|
2005-11-10 03:31:02 +08:00
|
|
|
table->free_value = free_value;
|
2004-12-05 06:44:55 +08:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
|
|
|
|
sigar_uint64_t key)
|
|
|
|
{
|
|
|
|
sigar_cache_entry_t *entry, **ptr;
|
|
|
|
|
|
|
|
for (ptr = table->entries + (key % table->size), entry = *ptr;
|
|
|
|
entry; ptr = &entry->next, entry = *ptr)
|
|
|
|
{
|
|
|
|
if (entry->id == key) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table->count++ > table->size) {
|
|
|
|
unsigned int new_size = table->size * 2;
|
|
|
|
|
|
|
|
table->entries =
|
|
|
|
realloc(table->entries, ENTRIES_SIZE(new_size));
|
|
|
|
|
|
|
|
memset(table->entries + table->size, '\0',
|
|
|
|
ENTRIES_SIZE(new_size - table->size));
|
|
|
|
|
|
|
|
table->size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = entry = malloc(sizeof(*entry));
|
|
|
|
entry->id = key;
|
|
|
|
entry->value = NULL;
|
|
|
|
entry->next = NULL;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigar_cache_destroy(sigar_cache_t *table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sigar_cache_entry_t **entries = table->entries;
|
|
|
|
|
|
|
|
for (i=0; i<table->size; i++) {
|
|
|
|
sigar_cache_entry_t *entry, *ptr;
|
2005-11-10 03:45:52 +08:00
|
|
|
entry = *entries++;
|
2004-12-05 06:44:55 +08:00
|
|
|
|
2005-11-10 03:45:52 +08:00
|
|
|
while (entry) {
|
|
|
|
if (entry->value) {
|
|
|
|
table->free_value(entry->value);
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|
2005-11-10 03:45:52 +08:00
|
|
|
ptr = entry->next;
|
|
|
|
free(entry);
|
|
|
|
entry = ptr;
|
|
|
|
}
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free(table->entries);
|
2004-12-06 05:40:56 +08:00
|
|
|
free(table);
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|