src/modules/usrloc/hslot.c
5c4bff80
 /*
31ccf6a2
  * Copyright (C) 2001-2003 FhG Fokus
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * 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.
  *
10d6b2b7
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
31ccf6a2
  */
 
adfa93a7
 /*! \file
  *  \brief USRLOC - Hash table collision slot related functions
  *  \ingroup usrloc
35882b72
  *
  * - Module: \ref usrloc
adfa93a7
  */
 
 
31ccf6a2
 
 #include "hslot.h"
 
5c4bff80
 /*!
  * \brief Initialize cache slot structure
  * \param _d domain for the hash slot
  * \param _s hash slot
  * \param n used to get the slot number (modulo number or locks)
31ccf6a2
  */
ab867565
 int init_slot(struct udomain* _d, hslot_t* _s, int n)
31ccf6a2
 {
 	_s->n = 0;
 	_s->first = 0;
 	_s->last = 0;
 	_s->d = _d;
ab867565
 	if(rec_lock_init(&_s->rlock)==NULL) {
 		LM_ERR("failed to initialize the slock (%d)\n", n);
 		return -1;
 	}
 	return 0;
31ccf6a2
 }
 
 
5c4bff80
 /*!
  * \brief Deinitialize given slot structure
  * \param _s hash slot
31ccf6a2
  */
 void deinit_slot(hslot_t* _s)
 {
 	struct urecord* ptr;
ab867565
 
 	/* Remove all elements */
31ccf6a2
 	while(_s->first) {
 		ptr = _s->first;
5d5a0c00
 		_s->first = _s->first->next;
31ccf6a2
 		free_urecord(ptr);
 	}
ab867565
 	rec_lock_destroy(&_s->rlock);
 
31ccf6a2
 	_s->n = 0;
 	_s->last = 0;
5d5a0c00
     _s->d = 0;
31ccf6a2
 }
 
 
5c4bff80
 /*!
  * \brief Add an element to an slot's linked list
  * \param _s hash slot
  * \param _r added record
31ccf6a2
  */
 void slot_add(hslot_t* _s, struct urecord* _r)
 {
 	if (_s->n == 0) {
 		_s->first = _s->last = _r;
 	} else {
5d5a0c00
 		_r->prev = _s->last;
 		_s->last->next = _r;
31ccf6a2
 		_s->last = _r;
 	}
 	_s->n++;
 	_r->slot = _s;
 }
 
 
5c4bff80
 /*!
  * \brief Remove an element from slot linked list
  * \param _s hash slot
  * \param _r removed record
31ccf6a2
  */
 void slot_rem(hslot_t* _s, struct urecord* _r)
 {
5d5a0c00
 	if (_r->prev) {
 		_r->prev->next = _r->next;
31ccf6a2
 	} else {
5d5a0c00
 		_s->first = _r->next;
31ccf6a2
 	}
 
5d5a0c00
 	if (_r->next) {
 		_r->next->prev = _r->prev;
31ccf6a2
 	} else {
5d5a0c00
 		_s->last = _r->prev;
31ccf6a2
 	}
 
5d5a0c00
 	_r->prev = _r->next = 0;
31ccf6a2
 	_r->slot = 0;
 	_s->n--;
 }