hashtable.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdlib.h>
00021 #include <glib.h>
00022
00023 #include "../../dprint.h"
00024 #include "../../mem/mem.h"
00025
00026 #include "hashtable.h"
00027
00028 GHashTable *hash;
00029
00030 void hashtable_init(void) {
00031 hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
00032 }
00033
00034 static int *get_counter(char *key) {
00035 int *d = g_hash_table_lookup(hash, key);
00036 if (d == NULL) {
00037 gchar *k = g_strdup(key);
00038 d = (int*) pkg_malloc(sizeof(int));
00039 LM_DBG("counter created @0x%08x\n", (unsigned int)d);
00040 *d = 0;
00041 g_hash_table_insert(hash, k, d);
00042 }
00043 LM_DBG("counter@0x%08x: key: %s ; value: %d\n", (unsigned int)d, key, *d);
00044 return d;
00045 }
00046
00047 static void remove_counter(char *key) {
00048 if (!g_hash_table_remove(hash, key))
00049 LM_ERR("error removing counter\n");
00050
00051 }
00052
00053 int hashtable_get_counter(char* key) {
00054 int *d = get_counter(key);
00055 return *d;
00056 }
00057
00058 int hashtable_inc_counter(char* key) {
00059 LM_DBG("incrementing counter for <%s>\n", key);
00060 int *d = get_counter(key);
00061 *d = *d + 1;
00062 return *d;
00063 }
00064
00065 int hashtable_dec_counter(char* key) {
00066 LM_DBG("decrementing counter for <%s>\n", key);
00067 int *d = get_counter(key);
00068 if (*d > 0)
00069 *d = *d - 1;
00070 if (*d == 0)
00071 remove_counter(key);
00072 return *d;
00073 }
00074