hashtable.c

Go to the documentation of this file.
00001 /* OpenSER PURPLE MODULE
00002  * 
00003  * Copyright (C) 2008 Atos Worldline
00004  * Contact: Eric PTAK <eric.ptak@atosorigin.com>
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 

Generated on Wed May 23 06:00:46 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6