lcr/hash.c

Go to the documentation of this file.
00001 /*
00002  * Header file for hash table functions
00003  *
00004  * Copyright (C) 2008-2009 Juha Heinanen
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 
00024 #include "../../mem/shm_mem.h"
00025 #include "lcr_mod.h"
00026 
00027 #define lcr_hash(_s) core_hash( _s, 0, lcr_hash_size_param)
00028 
00029 /* Add lcr entry into hash table */
00030 int lcr_hash_table_insert(struct lcr_info **hash_table,
00031            unsigned short prefix_len, char *prefix,
00032            unsigned short from_uri_len, char *from_uri,
00033            pcre *from_uri_re, unsigned int grp_id,
00034            unsigned short first_gw, unsigned short priority)
00035 {
00036     struct lcr_info *lcr;
00037     str prefix_str;
00038     unsigned int hash_val;
00039 
00040     lcr = (struct lcr_info *)shm_malloc(sizeof(struct lcr_info));
00041     if (lcr == NULL) {
00042    LM_ERR("Cannot allocate memory for lcr hash table entry\n");
00043    return 0;
00044     }
00045     memset(lcr, 0, sizeof(struct lcr_info));
00046 
00047     lcr->prefix_len = prefix_len;
00048     if (prefix_len) {
00049    memcpy(lcr->prefix, prefix, prefix_len);
00050     }
00051     lcr->from_uri_len = from_uri_len;
00052     if (from_uri_len) {
00053    memcpy(lcr->from_uri, from_uri, from_uri_len);
00054    (lcr->from_uri)[from_uri_len] = '\0';
00055    lcr->from_uri_re = from_uri_re;
00056     }
00057     lcr->grp_id = grp_id;
00058     lcr->first_gw = first_gw;
00059     lcr->priority = priority;
00060     
00061     prefix_str.len = lcr->prefix_len;
00062     prefix_str.s = lcr->prefix;
00063 
00064     hash_val = lcr_hash(&prefix_str);
00065     lcr->next = hash_table[hash_val];
00066     hash_table[hash_val] = lcr;
00067     
00068     LM_DBG("inserted prefix <%.*s>, from_uri <%.*s>, grp_id <%u>, "
00069       "priority <%u> into index <%u>\n",
00070       prefix_len, prefix, from_uri_len, from_uri, grp_id,
00071       priority, hash_val);
00072 
00073     return 1;
00074 }
00075 
00076 
00077 /* 
00078  * Return pointer to lcr hash table entry to which given prefix hashes to.
00079  */
00080 struct lcr_info *lcr_hash_table_lookup(struct lcr_info **hash_table,
00081                    unsigned short prefix_len, char *prefix)
00082 {
00083     str prefix_str;
00084 
00085     LM_DBG("looking for <%.*s>\n", prefix_len, prefix);
00086 
00087     prefix_str.len = prefix_len;
00088     prefix_str.s = prefix;
00089 
00090     return (hash_table)[lcr_hash(&prefix_str)];
00091 }
00092 
00093 
00094 /* Free contents of lcr hash table */
00095 void lcr_hash_table_contents_free(struct lcr_info **hash_table)
00096 {
00097     int i;
00098     struct lcr_info *lcr_rec, *next;
00099    
00100     if (hash_table == 0)
00101    return;
00102 
00103     for (i = 0; i <= lcr_hash_size_param; i++) {
00104    lcr_rec = hash_table[i];
00105    while (lcr_rec) {
00106        LM_DBG("freeing lcr hash table prefix <%.*s> grp_id <%u>\n",
00107          lcr_rec->prefix_len, lcr_rec->prefix, lcr_rec->grp_id);
00108        if (lcr_rec->from_uri_re) {
00109       shm_free(lcr_rec->from_uri_re);
00110        }
00111        next = lcr_rec->next;
00112        shm_free(lcr_rec);
00113        lcr_rec = next;
00114    }
00115    hash_table[i] = NULL;
00116     }
00117 }

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