belle_sip_object.c 14.7 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

Simon Morlat's avatar
Simon Morlat committed
22 23 24 25 26 27
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
28
	}
Simon Morlat's avatar
Simon Morlat committed
29
	return FALSE;
Simon Morlat's avatar
Simon Morlat committed
30
}
31 32

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

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

Simon Morlat's avatar
Simon Morlat committed
48 49
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
50 51
}

52
belle_sip_object_t * belle_sip_object_ref(void *obj){
53
	belle_sip_object_t *o=BELLE_SIP_OBJECT(obj);
54 55
	if (o->ref==0 && o->pool){
		belle_sip_object_pool_remove(o->pool,obj);
56 57
	}
	o->ref++;
jehan's avatar
jehan committed
58
	return obj;
Simon Morlat's avatar
Simon Morlat committed
59 60
}

61 62
void belle_sip_object_unref(void *ptr){
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
63 64 65
	if (obj->ref==-1) belle_sip_fatal("Object with name [%s] freed twice !",obj->name);
	if (obj->ref==0 && obj->pool){
		belle_sip_object_pool_remove(obj->pool,obj);
66
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
67
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
68 69 70 71
		return;
	}
	obj->ref--;
	if (obj->ref==0){
72
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
73
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
74 75 76
	}
}

77 78 79 80 81 82 83 84 85 86 87 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
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
122 123 124 125 126 127 128 129 130
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
131 132 133 134
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
135 136
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
137 138
	"belle_sip_object_t",
	FALSE,
Simon Morlat's avatar
Simon Morlat committed
139 140 141
	NULL, /*no parent, it's god*/
	NULL,
	_belle_sip_object_uninit,
jehan's avatar
jehan committed
142
	_belle_sip_object_clone,
Simon Morlat's avatar
Simon Morlat committed
143
	_belle_object_marshall
Simon Morlat's avatar
Simon Morlat committed
144 145 146
};

void belle_sip_object_delete(void *ptr){
147
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
Simon Morlat's avatar
Simon Morlat committed
148
	belle_sip_object_vptr_t *vptr;
149 150 151 152 153 154 155 156
	
	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
157 158
}

Simon Morlat's avatar
Simon Morlat committed
159 160 161 162 163 164 165 166 167 168
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
169
	return find_common_floor(vptr1->parent,vptr2);
Simon Morlat's avatar
Simon Morlat committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

/*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
188 189 190 191
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
192
	newobj->ref=obj->vptr->initially_unowned ? 0 : 1;
Simon Morlat's avatar
Simon Morlat committed
193
	newobj->vptr=obj->vptr;
194
	newobj->size=obj->size;
Simon Morlat's avatar
Simon Morlat committed
195
	_belle_sip_object_copy(newobj,obj);
196
	if (newobj->ref==0){
197 198
		belle_sip_object_pool_t *pool=belle_sip_object_pool_get_current();
		if (pool) belle_sip_object_pool_add(pool,newobj);
199
	}
Simon Morlat's avatar
Simon Morlat committed
200 201 202
	return newobj;
}

203 204 205
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
206

Simon Morlat's avatar
Simon Morlat committed
207
void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno){
208 209 210 211 212
	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
213 214 215
	}
	return obj;
}
Simon Morlat's avatar
Simon Morlat committed
216

217 218 219 220
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
221
			belle_sip_interface_desc_t **ifaces=vptr->interfaces;
222 223
			if (ifaces!=NULL){
				for(;*ifaces!=0;++ifaces){
Simon Morlat's avatar
Simon Morlat committed
224
					if ((*ifaces)->id==ifid){
225 226 227 228 229 230 231 232 233 234 235 236 237
						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
238
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){
239 240 241 242 243 244 245 246
	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
247

248
void belle_sip_object_set_name(belle_sip_object_t* object,const char* name) {
Simon Morlat's avatar
Simon Morlat committed
249 250 251 252 253 254
	if (object->name) {
		belle_sip_free(object->name);
		object->name=NULL;
	}
	if (name)
		object->name=belle_sip_strdup(name);
255
}
Simon Morlat's avatar
Simon Morlat committed
256

257 258 259
const char* belle_sip_object_get_name(belle_sip_object_t* object) {
	return object->name;
}
jehan's avatar
jehan committed
260 261 262 263 264 265 266 267 268 269 270 271

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
272

jehan's avatar
jehan committed
273
char* belle_sip_object_to_string(belle_sip_object_t* obj) {
274
	char buff[BELLE_SIP_MAX_TO_STRING_SIZE]; /*to be optimized*/
jehan's avatar
jehan committed
275 276
	int size = belle_sip_object_marshal(obj,buff,0,sizeof(buff));
	buff[size]='\0';
jehan's avatar
jehan committed
277
	return belle_sip_strdup(buff);
jehan's avatar
jehan committed
278 279

}
Simon Morlat's avatar
Simon Morlat committed
280 281 282 283 284 285 286 287 288 289 290 291 292 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

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
318 319
#if !defined(WIN32)

Simon Morlat's avatar
Simon Morlat committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
#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
342 343 344 345 346 347 348 349 350

#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

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
struct belle_sip_object_pool{
	belle_sip_object_t base;
	belle_sip_list_t *objects;
	belle_sip_thread_t thread_id;
};

static void belle_sip_object_pool_destroy(belle_sip_object_pool_t *pool){
	belle_sip_object_pool_clean(pool);
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_object_pool_t);
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_object_pool_t,belle_sip_object_t,belle_sip_object_pool_destroy,NULL,NULL,FALSE);

belle_sip_object_pool_t *belle_sip_object_pool_new(void){
	belle_sip_object_pool_t *pool=belle_sip_object_new(belle_sip_object_pool_t);
	pool->thread_id=belle_sip_thread_self();
	return pool;
}

void belle_sip_object_pool_add(belle_sip_object_pool_t *pool, belle_sip_object_t *obj){
	if (obj->pool!=NULL){
		belle_sip_fatal("It is not possible to add an object to multiple pools.");
	}
	pool->objects=belle_sip_list_prepend(pool->objects,obj);
	obj->pool_iterator=pool->objects;
	obj->pool=pool;
}

void belle_sip_object_pool_remove(belle_sip_object_pool_t *pool, belle_sip_object_t *obj){
	belle_sip_thread_t tid=belle_sip_thread_self();
	if (obj->pool!=pool){
		belle_sip_fatal("Attempting to remove object from an incorrect pool: obj->pool=%p, pool=%p",obj->pool,pool);
		return;
	}
	if (tid!=pool->thread_id){
		belle_sip_fatal("It is forbidden (and unsafe()) to ref()/unref() an unowned object outside of the thread that created it.");
		return;
	}
	pool->objects=belle_sip_list_delete_link(pool->objects,obj->pool_iterator);
	obj->pool_iterator=NULL;
	obj->pool=NULL;
}

394 395 396 397
int belle_sip_object_pool_cleanable(belle_sip_object_pool_t *pool){
	return belle_sip_thread_self()==pool->thread_id;
}

398 399
void belle_sip_object_pool_clean(belle_sip_object_pool_t *pool){
	belle_sip_list_t *elem,*next;
400 401
	
	if (!belle_sip_object_pool_cleanable(pool)){
jehan's avatar
jehan committed
402 403
		belle_sip_warning("Thread pool [%p] cannot be cleaned from thread [%ul] because it was created for thread [%ul]",
				 pool,(unsigned long)belle_sip_thread_self(),(unsigned long)pool->thread_id);
404 405 406
		return;
	}
	
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	for(elem=pool->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;
			belle_sip_free(elem);
		}else {
			belle_sip_fatal("Object %p is in unowned list but with ref count %i, bug.",obj,obj->ref);
			next=elem->next;
		}
	}
	pool->objects=NULL;
}

static void cleanup_pool_stack(void *data){
	belle_sip_list_t **pool_stack=(belle_sip_list_t**)data;
	belle_sip_list_free_with_data(*pool_stack, belle_sip_object_unref);
	belle_sip_message("Object pools for thread [%u] cleaned while exiting",(unsigned long)belle_sip_thread_self());
	*pool_stack=NULL;
	belle_sip_free(pool_stack);
}

431
static belle_sip_list_t** get_current_pool_stack(int *first_time){
432 433 434 435
	static belle_sip_thread_key_t pools_key;
	static int pools_key_created=0;
	belle_sip_list_t **pool_stack;
	
436 437
	if (first_time) *first_time=0;
	
438 439 440 441 442 443 444 445 446 447 448
	if (!pools_key_created){
		pools_key_created=1;
		if (belle_sip_thread_key_create(&pools_key, cleanup_pool_stack)!=0){
			return NULL;
		}
	}
	pool_stack=(belle_sip_list_t**)belle_sip_thread_getspecific(pools_key);
	if (pool_stack==NULL){
		pool_stack=belle_sip_new(belle_sip_list_t*);
		*pool_stack=NULL;
		belle_sip_thread_setspecific(pools_key,pool_stack);
449
		if (first_time) *first_time=1;
450 451 452 453 454
	}
	return pool_stack;
}

belle_sip_object_pool_t * belle_sip_object_pool_push(void){
455
	belle_sip_list_t **pools=get_current_pool_stack(NULL);
456 457 458 459 460 461 462 463 464 465 466
	belle_sip_object_pool_t *pool;
	if (pools==NULL) {
		belle_sip_error("Not possible to create a pool.");
		return NULL;
	}
	pool=belle_sip_object_pool_new();
	*pools=belle_sip_list_prepend(*pools,pool);
	return pool;
}

void belle_sip_object_pool_pop(void){
467
	belle_sip_list_t **pools=get_current_pool_stack(NULL);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	belle_sip_object_pool_t *pool;
	if (pools==NULL) {
		belle_sip_error("Not possible to pop a pool.");
		return;
	}
	if (*pools==NULL){
		belle_sip_error("There is no current pool in stack.");
		return;
	}
	pool=(belle_sip_object_pool_t*)(*pools)->data;
	*pools=belle_sip_list_remove_link(*pools,*pools);
	belle_sip_object_unref(pool);
}

belle_sip_object_pool_t *belle_sip_object_pool_get_current(void){
483 484
	int first_time;
	belle_sip_list_t **pools=get_current_pool_stack(&first_time);
485
	if (pools==NULL) return NULL;
486 487
	if (*pools==NULL ){
		if (first_time) {
jehan's avatar
jehan committed
488
			belle_sip_warning("There is no object pool created in thread [%ul]. "
489
			"Use belle_sip_stack_push_pool() to create one. Unowned objects not unref'd will be leaked.",
jehan's avatar
jehan committed
490
			(unsigned long)belle_sip_thread_self());
491
		}
492 493 494 495 496 497
		return NULL;
	}
	return (belle_sip_object_pool_t*)(*pools)->data;
}


Simon Morlat's avatar
Simon Morlat committed
498