modules_k/usrloc/ul_callback.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2004-03-16  created (bogdan)
00025  */
00026 
00035 #include <stdlib.h>
00036 
00037 #include "../../dprint.h"
00038 #include "../../error.h"
00039 #include "../../mem/shm_mem.h"
00040 #include "ul_callback.h"
00041 #include "ucontact.h"
00042 
00043 struct ulcb_head_list* ulcb_list = 0;
00044 
00045 
00046 
00047 int init_ulcb_list(void)
00048 {
00049         ulcb_list = (struct ulcb_head_list*)shm_malloc
00050                 ( sizeof(struct ulcb_head_list) );
00051         if (ulcb_list==0) {
00052                 LM_CRIT("no more shared mem\n");
00053                 return -1;
00054         }
00055         ulcb_list->first = 0;
00056         ulcb_list->reg_types = 0;
00057         return 1;
00058 }
00059 
00060 
00061 void destroy_ulcb_list(void)
00062 {
00063         struct ul_callback *cbp, *cbp_tmp;
00064 
00065         if (!ulcb_list)
00066                 return;
00067 
00068         for( cbp=ulcb_list->first; cbp ; ) {
00069                 cbp_tmp = cbp;
00070                 cbp = cbp->next;
00071                 if (cbp_tmp->param) shm_free( cbp_tmp->param );
00072                 shm_free( cbp_tmp );
00073         }
00074 
00075         shm_free(ulcb_list);
00076 }
00077 
00078 
00079 
00083 int register_ulcb( int types, ul_cb f, void *param )
00084 {
00085         struct ul_callback *cbp;
00086 
00087         /* are the callback types valid?... */
00088         if ( types<0 || types>ULCB_MAX ) {
00089                 LM_CRIT("invalid callback types: mask=%d\n",types);
00090                 return E_BUG;
00091         }
00092         /* we don't register null functions */
00093         if (f==0) {
00094                 LM_CRIT("null callback function\n");
00095                 return E_BUG;
00096         }
00097 
00098         /* build a new callback structure */
00099         if (!(cbp=(struct ul_callback*)shm_malloc(sizeof( struct ul_callback)))) {
00100                 LM_ERR("no more share mem\n");
00101                 return E_OUT_OF_MEM;
00102         }
00103 
00104         /* link it into the proper place... */
00105         cbp->next = ulcb_list->first;
00106         ulcb_list->first = cbp;
00107         ulcb_list->reg_types |= types;
00108         /* ... and fill it up */
00109         cbp->callback = f;
00110         cbp->param = param;
00111         cbp->types = types;
00112         if (cbp->next)
00113                 cbp->id = cbp->next->id+1;
00114         else
00115                 cbp->id = 0;
00116 
00117         return 1;
00118 }
00119 
00120 
00121