ul_callback.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2004-03-16 created (bogdan)
  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. struct ulcb_head_list* ulcb_list = 0;
  38. int init_ulcb_list()
  39. {
  40. ulcb_list = (struct ulcb_head_list*)shm_malloc
  41. ( sizeof(struct ulcb_head_list) );
  42. if (ulcb_list==0) {
  43. LOG(L_CRIT,"ERROR:usrloc:init_ulcb_list: no more shared mem\n");
  44. return -1;
  45. }
  46. ulcb_list->first = 0;
  47. ulcb_list->reg_types = 0;
  48. return 1;
  49. }
  50. void destroy_ulcb_list()
  51. {
  52. struct ul_callback *cbp, *cbp_tmp;
  53. if (!ulcb_list)
  54. return;
  55. for( cbp=ulcb_list->first; cbp ; ) {
  56. cbp_tmp = cbp;
  57. cbp = cbp->next;
  58. if (cbp_tmp->param) shm_free( cbp_tmp->param );
  59. shm_free( cbp_tmp );
  60. }
  61. shm_free(ulcb_list);
  62. }
  63. /* register a callback function 'f' for 'types' mask of events;
  64. */
  65. int register_ulcb( int types, ul_cb f, void *param )
  66. {
  67. struct ul_callback *cbp;
  68. /* are the callback types valid?... */
  69. if ( types<0 || types>ULCB_MAX ) {
  70. LOG(L_CRIT, "BUG:usrloc:register_ulcb: invalid callback types: "
  71. "mask=%d\n",types);
  72. return E_BUG;
  73. }
  74. /* we don't register null functions */
  75. if (f==0) {
  76. LOG(L_CRIT, "BUG:usrloc:register_ulcb: null callback function\n");
  77. return E_BUG;
  78. }
  79. /* build a new callback structure */
  80. if (!(cbp=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) {
  81. LOG(L_ERR, "ERROR:usrloc:register_ulcb: out of shm. mem\n");
  82. return E_OUT_OF_MEM;
  83. }
  84. /* link it into the proper place... */
  85. cbp->next = ulcb_list->first;
  86. ulcb_list->first = cbp;
  87. ulcb_list->reg_types |= types;
  88. /* ... and fill it up */
  89. cbp->callback = f;
  90. cbp->param = param;
  91. cbp->types = types;
  92. if (cbp->next)
  93. cbp->id = cbp->next->id+1;
  94. else
  95. cbp->id = 0;
  96. return 1;
  97. }