belle_sip_object.c 10.5 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_internal.h"

21 22
static belle_sip_list_t *unowned_objects=NULL;

Simon Morlat's avatar
Simon Morlat committed
23 24 25 26 27 28
static int has_type(belle_sip_object_t *obj, belle_sip_type_id_t id){
	belle_sip_object_vptr_t *vptr=obj->vptr;
	
	while(vptr!=NULL){
		if (vptr->id==id) return TRUE;
		vptr=vptr->parent;
Simon Morlat's avatar
Simon Morlat committed
29
	}
Simon Morlat's avatar
Simon Morlat committed
30
	return FALSE;
Simon Morlat's avatar
Simon Morlat committed
31
}
32 33

int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id) {
34 35
	return has_type(obj,id);
}
36

Simon Morlat's avatar
Simon Morlat committed
37
belle_sip_object_t * _belle_sip_object_new(size_t objsize, belle_sip_object_vptr_t *vptr){
Simon Morlat's avatar
Simon Morlat committed
38
	belle_sip_object_t *obj=(belle_sip_object_t *)belle_sip_malloc0(objsize);
Simon Morlat's avatar
Simon Morlat committed
39
	obj->ref=vptr->initially_unowned ? 0 : 1;
40
	obj->vptr=vptr;
Simon Morlat's avatar
Simon Morlat committed
41
	obj->size=objsize;
42 43 44
	if (obj->ref==0){
		unowned_objects=belle_sip_list_prepend(unowned_objects,obj);
	}
Simon Morlat's avatar
Simon Morlat committed
45 46 47
	return obj;
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61
void belle_sip_object_delete_unowned(void){
	belle_sip_list_t *elem,*next;
	for(elem=unowned_objects;elem!=NULL;elem=next){
		belle_sip_object_t *obj=(belle_sip_object_t*)elem->data;
		if (obj->ref==0){
			belle_sip_message("Garbage collecting unowned object of type %s",obj->vptr->type_name);
			obj->ref=-1;
			belle_sip_object_delete(obj);
			next=elem->next;
			unowned_objects=belle_sip_list_delete_link(unowned_objects,elem);
		}else next=elem->next;
	}
}

Simon Morlat's avatar
Simon Morlat committed
62 63
int belle_sip_object_is_initially_unowned(const belle_sip_object_t *obj){
	return obj->vptr->initially_unowned;
Simon Morlat's avatar
Simon Morlat committed
64 65
}

66
belle_sip_object_t * belle_sip_object_ref(void *obj){
67 68 69 70 71
	belle_sip_object_t *o=BELLE_SIP_OBJECT(obj);
	if (o->ref==0){
		unowned_objects=belle_sip_list_remove(unowned_objects,obj);
	}
	o->ref++;
jehan's avatar
jehan committed
72
	return obj;
Simon Morlat's avatar
Simon Morlat committed
73 74
}

75 76
void belle_sip_object_unref(void *ptr){
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
jehan's avatar
jehan committed
77
	if (obj->ref==-1) belle_sip_fatal("Object of type [%s] freed twice !",obj->name);
Simon Morlat's avatar
Simon Morlat committed
78
	if (obj->ref==0){
79
		unowned_objects=belle_sip_list_remove(unowned_objects,obj);
80
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
81
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
82 83 84 85
		return;
	}
	obj->ref--;
	if (obj->ref==0){
86
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
87
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
88 89 90
	}
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static weak_ref_t *weak_ref_new(belle_sip_object_destroy_notify_t destroy_notify, void *userpointer){
	weak_ref_t *r=belle_sip_new(weak_ref_t);
	r->next=NULL;
	r->notify=destroy_notify;
	r->userpointer=userpointer;
	return r;
}

belle_sip_object_t *belle_sip_object_weak_ref(void *obj, belle_sip_object_destroy_notify_t destroy_notify, void *userpointer){
	belle_sip_object_t *o=BELLE_SIP_OBJECT(obj);
	weak_ref_t *old=o->weak_refs;
	o->weak_refs=weak_ref_new(destroy_notify,userpointer);
	o->weak_refs->next=old;
	return o;
}

void belle_sip_object_weak_unref(void *obj, belle_sip_object_destroy_notify_t destroy_notify, void *userpointer){
	belle_sip_object_t *o=BELLE_SIP_OBJECT(obj);
	weak_ref_t *ref,*prevref=NULL,*next=NULL;

	if (o->ref==-1) return; /*too late and avoid recursions*/
	for(ref=o->weak_refs;ref!=NULL;ref=next){
		next=ref->next;
		if (ref->notify==destroy_notify && ref->userpointer==userpointer){
			if (prevref==NULL) o->weak_refs=next;
			else prevref->next=next;
			belle_sip_free(ref);
			return;
		}else{
			prevref=ref;
		}
	}
	belle_sip_fatal("Could not find weak_ref, you're a looser.");
}

static void belle_sip_object_loose_weak_refs(belle_sip_object_t *obj){
	weak_ref_t *ref,*next;
	for(ref=obj->weak_refs;ref!=NULL;ref=next){
		next=ref->next;
		ref->notify(ref->userpointer,obj);
		belle_sip_free(ref);
	}
	obj->weak_refs=NULL;
}

Simon Morlat's avatar
Simon Morlat committed
136 137 138 139 140 141 142 143 144
static void _belle_sip_object_uninit(belle_sip_object_t *obj){
	if (obj->name)
		belle_sip_free(obj->name);
}

static void _belle_sip_object_clone(belle_sip_object_t *obj, const belle_sip_object_t *orig){
	if (orig->name!=NULL) obj->name=belle_sip_strdup(obj->name);
}

Simon Morlat's avatar
Simon Morlat committed
145 146 147 148
static int _belle_object_marshall(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size) {
	return snprintf(buff+offset,buff_size,"{%s::%s %p}",obj->vptr->type_name,obj->name ? obj->name : "(no name)",obj);
}

Simon Morlat's avatar
Simon Morlat committed
149 150
belle_sip_object_vptr_t belle_sip_object_t_vptr={
	BELLE_SIP_TYPE_ID(belle_sip_object_t),
Simon Morlat's avatar
Simon Morlat committed
151 152
	"belle_sip_object_t",
	FALSE,
Simon Morlat's avatar
Simon Morlat committed
153 154 155
	NULL, /*no parent, it's god*/
	NULL,
	_belle_sip_object_uninit,
jehan's avatar
jehan committed
156
	_belle_sip_object_clone,
Simon Morlat's avatar
Simon Morlat committed
157
	_belle_object_marshall
Simon Morlat's avatar
Simon Morlat committed
158 159 160
};

void belle_sip_object_delete(void *ptr){
161
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
Simon Morlat's avatar
Simon Morlat committed
162
	belle_sip_object_vptr_t *vptr;
163 164 165 166 167 168 169 170
	
	belle_sip_object_loose_weak_refs(obj);
	vptr=obj->vptr;
	while(vptr!=NULL){
		if (vptr->destroy) vptr->destroy(obj);
		vptr=vptr->parent;
	}
	belle_sip_free(obj);
Simon Morlat's avatar
Simon Morlat committed
171 172
}

Simon Morlat's avatar
Simon Morlat committed
173 174 175 176 177 178 179 180 181 182
static belle_sip_object_vptr_t *find_common_floor(belle_sip_object_vptr_t *vptr1, belle_sip_object_vptr_t *vptr2){
	belle_sip_object_vptr_t *it1,*it2;
	for (it1=vptr1;it1!=NULL;it1=it1->parent){
		if (it1==vptr2)
			return vptr2;
	}
	for(it2=vptr2;it2!=NULL;it2=it2->parent){
		if (vptr1==it2)
			return vptr1;
	}
Simon Morlat's avatar
Simon Morlat committed
183
	return find_common_floor(vptr1->parent,vptr2);
Simon Morlat's avatar
Simon Morlat committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

/*copy the content of ref object to new object, for the part they have in common in their inheritence diagram*/
void _belle_sip_object_copy(belle_sip_object_t *newobj, const belle_sip_object_t *ref){
	belle_sip_object_vptr_t *vptr;
	vptr=find_common_floor(newobj->vptr,ref->vptr);
	if (vptr==NULL){
		belle_sip_fatal("Should not happen");
	}
	while(vptr!=NULL){
		if (vptr->clone==NULL){
			belle_sip_fatal("Object of type %s cannot be cloned, it does not provide a clone() implementation.",vptr->type_name);
			return;
		}else vptr->clone(newobj,ref);
		vptr=vptr->parent;
	}
}

Simon Morlat's avatar
Simon Morlat committed
202 203 204 205
belle_sip_object_t *belle_sip_object_clone(const belle_sip_object_t *obj){
	belle_sip_object_t *newobj;
	
	newobj=belle_sip_malloc0(obj->size);
jehan's avatar
jehan committed
206
	newobj->ref=obj->vptr->initially_unowned ? 0 : 1;
Simon Morlat's avatar
Simon Morlat committed
207
	newobj->vptr=obj->vptr;
208
	newobj->size=obj->size;
Simon Morlat's avatar
Simon Morlat committed
209
	_belle_sip_object_copy(newobj,obj);
210 211 212
	if (newobj->ref==0){
		unowned_objects=belle_sip_list_prepend(unowned_objects,newobj);
	}
Simon Morlat's avatar
Simon Morlat committed
213 214 215
	return newobj;
}

216 217 218
belle_sip_object_t *belle_sip_object_clone_and_ref(const belle_sip_object_t *obj) {
	return belle_sip_object_ref(belle_sip_object_clone(obj));
}
Simon Morlat's avatar
Simon Morlat committed
219

Simon Morlat's avatar
Simon Morlat committed
220
void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno){
221 222 223 224 225
	if (obj!=NULL){
		if (has_type(obj,id)==0){
			belle_sip_fatal("Bad cast to %s at %s:%i",castname,file,fileno);
			return NULL;
		}
Simon Morlat's avatar
Simon Morlat committed
226 227 228
	}
	return obj;
}
Simon Morlat's avatar
Simon Morlat committed
229

230 231 232 233
void *belle_sip_object_get_interface_methods(belle_sip_object_t *obj, belle_sip_interface_id_t ifid){
	if (obj!=NULL){
		belle_sip_object_vptr_t *vptr;
		for (vptr=obj->vptr;vptr!=NULL;vptr=vptr->parent){
Simon Morlat's avatar
Simon Morlat committed
234
			belle_sip_interface_desc_t **ifaces=vptr->interfaces;
235 236
			if (ifaces!=NULL){
				for(;*ifaces!=0;++ifaces){
Simon Morlat's avatar
Simon Morlat committed
237
					if ((*ifaces)->id==ifid){
238 239 240 241 242 243 244 245 246 247 248 249 250
						return *ifaces;
					}
				}
			}
		}
	}
	return NULL;
}

int belle_sip_object_implements(belle_sip_object_t *obj, belle_sip_interface_id_t id){
	return belle_sip_object_get_interface_methods(obj,id)!=NULL;
}

Simon Morlat's avatar
Simon Morlat committed
251
void *belle_sip_object_interface_cast(belle_sip_object_t *obj, belle_sip_interface_id_t ifid, const char *castname, const char *file, int fileno){
252 253 254 255 256 257 258 259
	if (obj!=NULL){
		if (belle_sip_object_get_interface_methods(obj,ifid)==0){
			belle_sip_fatal("Bad cast to interface %s at %s:%i",castname,file,fileno);
			return NULL;
		}
	}
	return obj;
}
Simon Morlat's avatar
Simon Morlat committed
260

261
void belle_sip_object_set_name(belle_sip_object_t* object,const char* name) {
Simon Morlat's avatar
Simon Morlat committed
262 263 264 265 266 267
	if (object->name) {
		belle_sip_free(object->name);
		object->name=NULL;
	}
	if (name)
		object->name=belle_sip_strdup(name);
268
}
Simon Morlat's avatar
Simon Morlat committed
269

270 271 272
const char* belle_sip_object_get_name(belle_sip_object_t* object) {
	return object->name;
}
jehan's avatar
jehan committed
273 274 275 276 277 278 279 280 281 282 283 284

int belle_sip_object_marshal(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size) {
	belle_sip_object_vptr_t *vptr=obj->vptr;
	while (vptr != NULL) {
		if (vptr->marshal != NULL) {
			return vptr->marshal(obj,buff,offset,buff_size);
		} else {
			vptr=vptr->parent;
		}
	}
	return -1; /*no implementation found*/
}
Simon Morlat's avatar
Simon Morlat committed
285

jehan's avatar
jehan committed
286
char* belle_sip_object_to_string(belle_sip_object_t* obj) {
287
	char buff[BELLE_SIP_MAX_TO_STRING_SIZE]; /*to be optimized*/
jehan's avatar
jehan committed
288 289
	int size = belle_sip_object_marshal(obj,buff,0,sizeof(buff));
	buff[size]='\0';
jehan's avatar
jehan committed
290
	return belle_sip_strdup(buff);
jehan's avatar
jehan committed
291 292

}
Simon Morlat's avatar
Simon Morlat committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

char * _belle_sip_object_describe_type(belle_sip_object_vptr_t *vptr){
	const int maxbufsize=2048;
	char *ret=belle_sip_malloc(maxbufsize);
	belle_sip_object_vptr_t *it;
	int pos=0;
	belle_sip_list_t *l=NULL,*elem;
	pos+=snprintf(ret+pos,maxbufsize-pos,"Ownership:\n");
	pos+=snprintf(ret+pos,maxbufsize-pos,"\t%s is created initially %s\n",vptr->type_name,
	              vptr->initially_unowned ? "unowned" : "owned");
	pos+=snprintf(ret+pos,maxbufsize-pos,"\nInheritance diagram:\n");
	for(it=vptr;it!=NULL;it=it->parent){
		l=belle_sip_list_prepend(l,it);
	}
	for(elem=l;elem!=NULL;elem=elem->next){
		it=(belle_sip_object_vptr_t*)elem->data;
		pos+=snprintf(ret+pos,maxbufsize-pos,"\t%s\n",it->type_name);
		if (elem->next)
			pos+=snprintf(ret+pos,maxbufsize-pos,"\t        |\n");
	}
	belle_sip_list_free(l);
	pos+=snprintf(ret+pos,maxbufsize-pos,"\nImplemented interfaces:\n");
	for(it=vptr;it!=NULL;it=it->parent){
		belle_sip_interface_desc_t **desc=it->interfaces;
		if (desc!=NULL){
			for(;*desc!=NULL;desc++){
				pos+=snprintf(ret+pos,maxbufsize-pos,"\t* %s\n",(*desc)->ifname);
			}
		}
	}
	return ret;
}

char *belle_sip_object_describe(void *obj){
	belle_sip_object_t *o=BELLE_SIP_OBJECT(obj);
	return _belle_sip_object_describe_type(o->vptr);
}

Simon Morlat's avatar
Simon Morlat committed
331 332
#if !defined(WIN32)

Simon Morlat's avatar
Simon Morlat committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
#include <dlfcn.h>

char *belle_sip_object_describe_type_from_name(const char *name){
	char *vptr_name;
	void *handle;
	void *symbol;
	
	handle=dlopen(NULL,RTLD_LAZY);
	if (handle==NULL){
		belle_sip_error("belle_sip_object_describe_type_from_name: dlopen() failed: %s",dlerror());
		return NULL;
	}
	vptr_name=belle_sip_strdup_printf("%s_vptr",name);
	symbol=dlsym(handle,vptr_name);
	belle_sip_free(vptr_name);
	dlclose(handle);
	if (symbol==NULL){
		belle_sip_error("belle_sip_object_describe_type_from_name: could not find vptr for type %s",name);
		return NULL;
	}
	return _belle_sip_object_describe_type((belle_sip_object_vptr_t*)symbol);
}
Simon Morlat's avatar
Simon Morlat committed
355 356 357 358 359 360 361 362 363 364

#else

char *belle_sip_object_describe_type_from_name(const char *name){
	return belle_sip_strdup_printf("Sorry belle_sip_object_describe_type_from_name() is not implemented on this platform.");
}

#endif