hslot.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*! \file
  23. * \brief USRLOC - Hash table collision slot related functions
  24. * \ingroup usrloc
  25. *
  26. * - Module: \ref usrloc
  27. */
  28. #include "hslot.h"
  29. /*! number of locks */
  30. int ul_locks_no=4;
  31. /*! global list of locks */
  32. gen_lock_set_t* ul_locks=0;
  33. /*!
  34. * \brief Initialize locks for the hash table
  35. * \return 0 on success, -1 on failure
  36. */
  37. int ul_init_locks(void)
  38. {
  39. int i;
  40. i = ul_locks_no;
  41. do {
  42. if ((( ul_locks=lock_set_alloc(i))!=0)&&
  43. (lock_set_init(ul_locks)!=0))
  44. {
  45. ul_locks_no = i;
  46. LM_INFO("locks array size %d\n", ul_locks_no);
  47. return 0;
  48. }
  49. if (ul_locks){
  50. lock_set_dealloc(ul_locks);
  51. ul_locks=0;
  52. }
  53. i--;
  54. if(i==0)
  55. {
  56. LM_ERR("failed to allocate locks\n");
  57. return -1;
  58. }
  59. } while (1);
  60. }
  61. /*!
  62. * \brief Unlock all locks on the list
  63. */
  64. void ul_unlock_locks(void)
  65. {
  66. unsigned int i;
  67. if (ul_locks==0)
  68. return;
  69. for (i=0;i<ul_locks_no;i++) {
  70. #ifdef GEN_LOCK_T_PREFERED
  71. lock_release(&ul_locks->locks[i]);
  72. #else
  73. ul_release_idx(i);
  74. #endif
  75. };
  76. }
  77. /*!
  78. * \brief Destroy all locks on the list
  79. */
  80. void ul_destroy_locks(void)
  81. {
  82. if (ul_locks !=0){
  83. lock_set_destroy(ul_locks);
  84. lock_set_dealloc(ul_locks);
  85. };
  86. }
  87. #ifndef GEN_LOCK_T_PREFERED
  88. /*!
  89. * \brief Lock a lock with a certain index
  90. * \param idx lock index
  91. */
  92. void ul_lock_idx(int idx)
  93. {
  94. lock_set_get(ul_locks, idx);
  95. }
  96. /*!
  97. * \brief Release a lock with a certain index
  98. * \param idx lock index
  99. */
  100. void ul_release_idx(int idx)
  101. {
  102. lock_set_release(ul_locks, idx);
  103. }
  104. #endif
  105. /*!
  106. * \brief Initialize cache slot structure
  107. * \param _d domain for the hash slot
  108. * \param _s hash slot
  109. * \param n used to get the slot number (modulo number or locks)
  110. */
  111. void init_slot(struct udomain* _d, hslot_t* _s, int n)
  112. {
  113. _s->n = 0;
  114. _s->first = 0;
  115. _s->last = 0;
  116. _s->d = _d;
  117. #ifdef GEN_LOCK_T_PREFERED
  118. _s->lock = &ul_locks->locks[n%ul_locks_no];
  119. #else
  120. _s->lockidx = n%ul_locks_no;
  121. #endif
  122. }
  123. /*!
  124. * \brief Deinitialize given slot structure
  125. * \param _s hash slot
  126. */
  127. void deinit_slot(hslot_t* _s)
  128. {
  129. struct urecord* ptr;
  130. /* Remove all elements */
  131. while(_s->first) {
  132. ptr = _s->first;
  133. _s->first = _s->first->next;
  134. free_urecord(ptr);
  135. }
  136. _s->n = 0;
  137. _s->last = 0;
  138. _s->d = 0;
  139. }
  140. /*!
  141. * \brief Add an element to an slot's linked list
  142. * \param _s hash slot
  143. * \param _r added record
  144. */
  145. void slot_add(hslot_t* _s, struct urecord* _r)
  146. {
  147. if (_s->n == 0) {
  148. _s->first = _s->last = _r;
  149. } else {
  150. _r->prev = _s->last;
  151. _s->last->next = _r;
  152. _s->last = _r;
  153. }
  154. _s->n++;
  155. _r->slot = _s;
  156. }
  157. /*!
  158. * \brief Remove an element from slot linked list
  159. * \param _s hash slot
  160. * \param _r removed record
  161. */
  162. void slot_rem(hslot_t* _s, struct urecord* _r)
  163. {
  164. if (_r->prev) {
  165. _r->prev->next = _r->next;
  166. } else {
  167. _s->first = _r->next;
  168. }
  169. if (_r->next) {
  170. _r->next->prev = _r->prev;
  171. } else {
  172. _s->last = _r->prev;
  173. }
  174. _r->prev = _r->next = 0;
  175. _r->slot = 0;
  176. _s->n--;
  177. }