ul_callback.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * History:
  23. * --------
  24. * 2004-03-16 created (bogdan)
  25. */
  26. /*! \file
  27. * \brief USRLOC - Callback functions
  28. * \ingroup usrloc
  29. *
  30. * - Module: \ref usrloc
  31. */
  32. #include <stdlib.h>
  33. #include "../../dprint.h"
  34. #include "../../error.h"
  35. #include "../../mem/shm_mem.h"
  36. #include "ul_callback.h"
  37. #include "ucontact.h"
  38. struct ulcb_head_list* ulcb_list = 0;
  39. int init_ulcb_list(void)
  40. {
  41. ulcb_list = (struct ulcb_head_list*)shm_malloc
  42. ( sizeof(struct ulcb_head_list) );
  43. if (ulcb_list==0) {
  44. LM_CRIT("no more shared mem\n");
  45. return -1;
  46. }
  47. ulcb_list->first = 0;
  48. ulcb_list->reg_types = 0;
  49. return 1;
  50. }
  51. void destroy_ulcb_list(void)
  52. {
  53. struct ul_callback *cbp, *cbp_tmp;
  54. if (!ulcb_list)
  55. return;
  56. for( cbp=ulcb_list->first; cbp ; ) {
  57. cbp_tmp = cbp;
  58. cbp = cbp->next;
  59. if (cbp_tmp->param) shm_free( cbp_tmp->param );
  60. shm_free( cbp_tmp );
  61. }
  62. shm_free(ulcb_list);
  63. }
  64. /*! \brief
  65. register a callback function 'f' for 'types' mask of events;
  66. */
  67. int register_ulcb( int types, ul_cb f, void *param )
  68. {
  69. struct ul_callback *cbp;
  70. /* are the callback types valid?... */
  71. if ( types<0 || types>ULCB_MAX ) {
  72. LM_CRIT("invalid callback types: mask=%d\n",types);
  73. return E_BUG;
  74. }
  75. /* we don't register null functions */
  76. if (f==0) {
  77. LM_CRIT("null callback function\n");
  78. return E_BUG;
  79. }
  80. /* build a new callback structure */
  81. if (!(cbp=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) {
  82. LM_ERR("no more share mem\n");
  83. return E_OUT_OF_MEM;
  84. }
  85. /* link it into the proper place... */
  86. cbp->next = ulcb_list->first;
  87. ulcb_list->first = cbp;
  88. ulcb_list->reg_types |= types;
  89. /* ... and fill it up */
  90. cbp->callback = f;
  91. cbp->param = param;
  92. cbp->types = types;
  93. if (cbp->next)
  94. cbp->id = cbp->next->id+1;
  95. else
  96. cbp->id = 0;
  97. return 1;
  98. }