ul_callback.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /*! \file
  22. * \brief USRLOC - Callback functions
  23. * \ingroup usrloc
  24. *
  25. * - Module: \ref usrloc
  26. */
  27. #include <stdlib.h>
  28. #include "../../dprint.h"
  29. #include "../../error.h"
  30. #include "../../mem/shm_mem.h"
  31. #include "../usrloc/ul_callback.h"
  32. #include "udomain.h"
  33. struct ulcb_head_list* ulcb_list = 0;
  34. int init_ulcb_list(void)
  35. {
  36. ulcb_list = (struct ulcb_head_list*)shm_malloc
  37. ( sizeof(struct ulcb_head_list) );
  38. if (ulcb_list==0) {
  39. LM_CRIT("no more shared mem\n");
  40. return -1;
  41. }
  42. ulcb_list->first = 0;
  43. ulcb_list->reg_types = 0;
  44. return 1;
  45. }
  46. void destroy_ulcb_list(void)
  47. {
  48. struct ul_callback *cbp, *cbp_tmp;
  49. if (!ulcb_list)
  50. return;
  51. for( cbp=ulcb_list->first; cbp ; ) {
  52. cbp_tmp = cbp;
  53. cbp = cbp->next;
  54. if (cbp_tmp->param) shm_free( cbp_tmp->param );
  55. shm_free( cbp_tmp );
  56. }
  57. shm_free(ulcb_list);
  58. }
  59. /*! \brief
  60. register a callback function 'f' for 'types' mask of events;
  61. */
  62. int register_ulcb( int types, ul_cb f, void *param )
  63. {
  64. struct ul_callback *cbp;
  65. /* are the callback types valid?... */
  66. if ( types<0 || types>ULCB_MAX ) {
  67. LM_CRIT("invalid callback types: mask=%d\n",types);
  68. return E_BUG;
  69. }
  70. /* we don't register null functions */
  71. if (f==0) {
  72. LM_CRIT("null callback function\n");
  73. return E_BUG;
  74. }
  75. /* build a new callback structure */
  76. if (!(cbp=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) {
  77. LM_ERR("no more share mem\n");
  78. return E_OUT_OF_MEM;
  79. }
  80. /* link it into the proper place... */
  81. cbp->next = ulcb_list->first;
  82. ulcb_list->first = cbp;
  83. ulcb_list->reg_types |= types;
  84. /* ... and fill it up */
  85. cbp->callback = f;
  86. cbp->param = param;
  87. cbp->types = types;
  88. if (cbp->next)
  89. cbp->id = cbp->next->id+1;
  90. else
  91. cbp->id = 0;
  92. return 1;
  93. }