diff options
Diffstat (limited to 'src/timer.c')
-rw-r--r-- | src/timer.c | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/src/timer.c b/src/timer.c index bb00e17..71f63f9 100644 --- a/src/timer.c +++ b/src/timer.c @@ -19,7 +19,6 @@ #include <sys/resource.h> #include <unistd.h> #include <string.h> -#include <hash.h> #include <ctype.h> #include <xalloc.h> @@ -36,11 +35,11 @@ struct timer_slot struct rusage children_mark; }; -static Hash_table *timer_table; +static struct grecs_symtab *timer_table; static size_t _timer_count; -static size_t -hash_string_ci (const char *string, size_t n_buckets) +static unsigned +hash_string_ci (const char *string, unsigned long n_buckets) { size_t value = 0; unsigned char ch; @@ -50,52 +49,38 @@ hash_string_ci (const char *string, size_t n_buckets) return value; } -/* Calculate the hash of a string. */ -static size_t -timer_hasher (void const *data, unsigned n_buckets) +static unsigned +timer_hasher (void *ptr, unsigned long n_buckets) { - const struct timer_slot *t = data; - return hash_string_ci (t->name, n_buckets); -} - -/* Compare two strings for equality. */ -static bool -timer_compare (void const *data1, void const *data2) -{ - const struct timer_slot *t1 = data1; - const struct timer_slot *t2 = data2; - return strcasecmp (t1->name, t2->name) == 0; -} - -/* Allocate a timer structure */ -static wydawca_timer_t -timer_alloc (const char *name) -{ - wydawca_timer_t t = xmalloc (sizeof (*t) + strlen (name) + 1); - memset (t, 0, sizeof (*t)); - t->name = (char *) (t + 1); - strcpy (t->name, name); - return t; + struct timer_slot *tp = ptr; + return hash_string_ci (tp->name, n_buckets); } /* Lookup a timer by its name. If it does not exist, create it. */ wydawca_timer_t timer_get (const char *name) { - wydawca_timer_t tp, ret; - - tp = timer_alloc (name); + struct timer_slot key, *ret; + int install = 1; + + key.name = (char*) name; - if (!((timer_table - || (timer_table = hash_initialize (0, 0, - timer_hasher, - timer_compare, 0))) - && (ret = hash_insert (timer_table, tp)))) - xalloc_die (); + if (!timer_table) + { + timer_table = grecs_symtab_create (sizeof (key), + timer_hasher, + NULL, + NULL, + NULL, + NULL); + if (!timer_table) + grecs_alloc_die (); + } - if (ret != tp) - free (tp); - else + ret = grecs_symtab_lookup_or_install (timer_table, &key, &install); + if (!ret) + grecs_alloc_die(); + if (install) _timer_count++; return ret; } @@ -210,11 +195,11 @@ struct timer_data size_t num; }; -bool -_fill_meta (void *data, void *proc_data) +int +_fill_meta (void *sym, void *data) { - struct timer_slot *slot = data; - struct timer_data *tp = proc_data; + struct timer_slot *slot = sym; + struct timer_data *tp = data; #define CREATE_DEF(arg) \ if (tp->num) \ { \ @@ -230,7 +215,7 @@ _fill_meta (void *data, void *proc_data) CREATE_DEF(real); CREATE_DEF(user); - return tp->num > 0; + return tp->num <= 0; } void @@ -241,7 +226,7 @@ timer_fill_meta (struct metadef *def, size_t num) return; td.def = def; td.num = num; - hash_do_for_each (timer_table, _fill_meta, &td); + grecs_symtab_enumerate (timer_table, _fill_meta, &td); } void |