📄 domains.c
字号:
/** * $Id: domains.c,v 1.4 2006/07/05 10:48:29 bogdan_iancu Exp $ * * Copyright (C) 2001-2003 FhG Fokus * * This file is part of openser, a free SIP server. * * openser is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version * * openser is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** * History * ------- * 2003-04-07: a structure for both hashes introduced (ramona) * 2005-01-26: double hash removed (ramona) * FIFO operations are kept as a diff list (ramona) * domain hash kept in share memory along with FIFO ops (ramona) * 2006-01-30: multidomain support added * 2006-03-13: use hash function from core (bogdan) */#include <stdio.h>#include <string.h>#include "../../sr_module.h"#include "../../parser/parse_fline.h"#include "../../db/db.h"#include "../../mem/shm_mem.h"#include "../../mem/mem.h"#include "../../dprint.h"#include "../../hash_func.h"#include "domains.h"#include "utils.h"#define pdt_compute_hash(_s) core_case_hash(_s,0,0)#define get_hash_entry(_h,_size) (_h)&((_size)-1)pd_op_t* new_pd_op(pd_t *cell, int id, int op){ pd_op_t *pdo; if(cell==NULL) { LOG(L_ERR, "PDT:new_pd_op: bad parameters\n"); return NULL; } pdo = (pd_op_t*)shm_malloc(sizeof(pd_op_t)); if(pdo==NULL) { LOG(L_ERR, "PDT:new_pd_op: no more shm\n"); return NULL; } memset(pdo, 0, sizeof(pd_op_t)); pdo->cell = cell; pdo->id = id; pdo->op = op; return pdo;}void free_pd_op(pd_op_t *pdo){ if(pdo==NULL) return; free_cell(pdo->cell); shm_free(pdo); pdo = NULL; return;}pd_t* new_cell(str* p, str *d){ pd_t* cell = NULL; if(p==NULL || p->s==NULL || d==NULL || d->s==NULL) { LOG(L_ERR, "PDT:new_cell: bad parameters\n"); return NULL; } /* the cell is in share memory */ cell = (pd_t*)shm_malloc(sizeof(pd_t)); /* if there is no space return just NULL */ if(cell==NULL) { LOG(L_ERR, "PDT:new_cell: no more shm memory.\n"); return NULL; } memset(cell, 0, sizeof(pd_t)); cell->prefix.s = (char*)shm_malloc((1+p->len)*sizeof(char)); if(cell->prefix.s==NULL) { shm_free(cell); LOG(L_ERR, "PDT:new_cell: no more shm memory\n"); return NULL; } strncpy(cell->prefix.s, p->s, p->len); cell->prefix.len = p->len; cell->prefix.s[p->len] = '\0'; cell->domain.s = (char*)shm_malloc((1+d->len)*sizeof(char)); if(cell->domain.s==NULL) { shm_free(cell->prefix.s); shm_free(cell); LOG(L_ERR, "PDT:new_cell: no more shm memory!\n"); return NULL; } strncpy(cell->domain.s, d->s, d->len); cell->domain.len = d->len; cell->domain.s[d->len] = '\0'; cell->dhash = pdt_compute_hash(&cell->domain); /* return the newly allocated in share memory cell */ return cell;}void free_cell(pd_t* cell){ if(cell==NULL) return; if(cell->prefix.s) shm_free(cell->prefix.s); if(cell->domain.s) shm_free(cell->domain.s); shm_free(cell);}/* returns a pointer to a hashtable */pd_t** init_hash_entries(unsigned int hash_size){ pd_t **hash = NULL; /* space for the hash is allocated in share memory */ hash = (pd_t**)shm_malloc(hash_size*sizeof(pd_t*)); if(hash == NULL) { LOG(L_ERR, "PDT:init_hash: no more shm\n"); return NULL; } memset(hash, 0, hash_size*sizeof(pd_t*)); /* the allocated hash */ return hash;}void free_hash_entries(pd_t** hash, unsigned int hash_size){ int i; pd_t *tmp, *it; if(hash==NULL || hash_size<=0) return; for(i=0; i<hash_size; i++) { it = hash[i]; while(it != NULL) { tmp = it->n; free_cell(it); it = tmp; } } shm_free(hash);}hash_t* init_hash(int hash_size, str *sdomain){ hash_t* hash = NULL; /* space allocated in shared memory */ hash = (hash_t*)shm_malloc(sizeof(hash_t)); if(hash == NULL) { LOG(L_ERR, "PDT: pdt_init_hash: no more shm\n"); return NULL; } memset(hash, 0, sizeof(hash_t)); hash->sdomain.s = (char*)shm_malloc((sdomain->len+1)*sizeof(char)); if(hash->sdomain.s==NULL) { LOG(L_ERR, "PDT: pdt_init_hash: no more shm\n"); shm_free(hash); return NULL; } memset(hash->sdomain.s, 0, sdomain->len+1); memcpy(hash->sdomain.s, sdomain->s, sdomain->len); hash->sdomain.len = sdomain->len; if( (hash->dhash = init_hash_entries(hash_size)) == NULL ) { shm_free(hash->sdomain.s); shm_free(hash); LOG(L_ERR, "PDT:pdt_init_hash: no more shm!\n"); return NULL; } hash->hash_size = hash_size; return hash;}int set_hash_domain(hash_t *h, str *s){ if(s==NULL || s->s==NULL || h==NULL) { LOG(L_ERR, "PDT:set_hash_domain(): wrong parameters\n"); return -1; } h->sdomain.s = (char*)shm_malloc((s->len+1)*sizeof(char)); if( h->sdomain.s == NULL ) { LOG(L_ERR, "PDT:set_hash_domain: no more shm!\n"); return -1; } memset(h->sdomain.s, 0, s->len+1); memcpy(h->sdomain.s, s->s, s->len); h->sdomain.len = s->len; return 0;}void free_hash(hash_t* hash){ pd_op_t *op, *op_t; if(hash==NULL) return; free_hash_entries(hash->dhash, hash->hash_size); /* free the allocated sdomain */ if(hash->sdomain.s!=NULL) shm_free(hash->sdomain.s); /* destroy diff list */ for( op = hash->diff ; op ; op=op_t) { op_t = op->n; shm_free(op); } /* free next element in the list */ free_hash(hash->next); shm_free(hash);}hash_list_t* init_hash_list(int hs_two_pow){ hash_list_t* hl = NULL; int hash_size; if(hs_two_pow>MAX_HSIZE_TWO_POW || hs_two_pow<0) hash_size = MAX_HASH_SIZE; else hash_size = 1<<hs_two_pow; /* space allocated in shared memory */ hl = (hash_list_t*)shm_malloc(sizeof(hash_list_t)); if(hl == NULL) { LOG(L_ERR, "PDT: init_hash_list: no more shm\n"); return NULL; } memset(hl, 0, sizeof(hash_list_t)); if(lock_init(&hl->hl_lock) == 0) { shm_free(hl); LOG(L_ERR, "PDT:init_hash_list: cannot init the hl_lock\n"); return NULL; } hl->hash_size = hash_size; return hl;}void free_hash_list(hash_list_t* hl){ if(hl==NULL) return; if(hl->hash!=NULL) free_hash(hl->hash); lock_destroy(&hash->hl_lock); shm_free(hl);}int add_to_hash(hash_t *hash, str *sp, str *sd){ int hash_entry=0; unsigned int dhash; pd_t *it, *prev, *cell; pd_op_t *ito, *tmp; if(hash==NULL || sp==NULL || sp->s==NULL || sd==NULL || sd->s==NULL) { LOG(L_ERR, "PDT: add_to_hash: bad parameters\n"); return -1; } dhash = pdt_compute_hash(sd); hash_entry = get_hash_entry(dhash, hash->hash_size); it = hash->dhash[hash_entry]; prev = NULL; while(it!=NULL && it->dhash < dhash) { prev = it; it = it->n; } /* we need a new entry for this cell */ cell = new_cell(sp, sd); if(cell == NULL) return -1; if(prev) prev->n=cell; else hash->dhash[hash_entry]= cell; cell->p=prev; cell->n=it; if(it) it->p=cell; /* mark the changes for the sync with pdtree */ tmp = new_pd_op(cell, 0, PDT_ADD); if(tmp==NULL) { LOG(L_ERR, "PDT:add_to_hash: no more shm! Cache not synchron!!\n"); return -1; } hash->max_id++; tmp->id = hash->max_id; if(hash->diff==NULL) { hash->diff = tmp; return 0; } ito = hash->diff; while(ito->n!=NULL) ito = ito->n; ito->n = tmp; tmp->p = ito; return 0;}int pdt_add_to_hash(hash_list_t *hl, str* sdomain, str *sp, str *sd){ hash_t *it, *prev, *ph; if(hl==NULL || sdomain==NULL || sdomain->s==NULL || sp==NULL || sp->s==NULL || sd==NULL || sd->s==NULL) { LOG(L_ERR, "PDT: pdt_add_to_hash: bad parameters\n"); return -1; } lock_get(&hl->hl_lock); /* search the it position where to insert new domain */ it = hl->hash; prev=NULL; while(it!=NULL && scmp(&it->sdomain, sdomain)<0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -