belle_sip_object.c 18 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"

Simon Morlat's avatar
Simon Morlat committed
21
static void _belle_sip_object_pool_remove_from_stack(belle_sip_object_pool_t *pool);
22

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
	if (obj->ref==0){
43 44
		belle_sip_object_pool_t *pool=belle_sip_object_pool_get_current();
		if (pool) belle_sip_object_pool_add(pool,obj);
45
	}
Simon Morlat's avatar
Simon Morlat committed
46 47 48
	return obj;
}

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

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

62 63
void belle_sip_object_unref(void *ptr){
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
64 65 66
	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);
67
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
68
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
69 70 71 72
		return;
	}
	obj->ref--;
	if (obj->ref==0){
73
		obj->ref=-1;
Simon Morlat's avatar
Simon Morlat committed
74
		belle_sip_object_delete(obj);
Simon Morlat's avatar
Simon Morlat committed
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 122
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
123 124 125 126 127 128 129 130 131
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);
}

Ghislain MARY's avatar
Ghislain MARY committed
132 133
static belle_sip_error_code _belle_object_marshal(belle_sip_object_t* obj, char* buff, size_t buff_size, unsigned int *offset) {
	return belle_sip_snprintf(buff,buff_size,offset,"{%s::%s %p}",obj->vptr->type_name,obj->name ? obj->name : "(no name)",obj);
Simon Morlat's avatar
Simon Morlat committed
134 135
}

Simon Morlat's avatar
Simon Morlat committed
136 137
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
138 139
	"belle_sip_object_t",
	FALSE,
Simon Morlat's avatar
Simon Morlat committed
140 141 142
	NULL, /*no parent, it's god*/
	NULL,
	_belle_sip_object_uninit,
jehan's avatar
jehan committed
143
	_belle_sip_object_clone,
Ghislain MARY's avatar
Ghislain MARY committed
144
	_belle_object_marshal
Simon Morlat's avatar
Simon Morlat committed
145 146 147
};

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

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

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

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

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

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

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

258 259 260
const char* belle_sip_object_get_name(belle_sip_object_t* object) {
	return object->name;
}
jehan's avatar
jehan committed
261

262 263 264 265 266
/*turn this to 1 if you feel a marshal method is buggy.*/
#define CHECKED_MARSHAL 0

#if CHECKED_MARSHAL

Ghislain MARY's avatar
Ghislain MARY committed
267
static belle_sip_error_code checked_marshal(belle_sip_object_vptr_t *vptr, belle_sip_object_t* obj, char* buff, size_t buff_size, unsigned int *offset){
268 269 270
	int tmp_buf_size=buff_size*2;
	char *p=(char*)belle_sip_malloc0(tmp_buf_size);
	int i;
Ghislain MARY's avatar
Ghislain MARY committed
271 272
	unsigned int initial_offset=*offset;
	belle_sip_error_code error=vptr->marshal(obj,p,buff_size,offset);
273 274
	int written;
	
Ghislain MARY's avatar
Ghislain MARY committed
275 276 277 278 279 280
	if (error==BELLE_SIP_BUFFER_OVERFLOW){
		belle_sip_fatal("Object of type %s commited a buffer overflow by marshalling %i bytes",
			vptr->type_name,*offset-initial_offset);
	} else if (error!=BELLE_SIP_OK){
		belle_sip_fatal("Object of type %s produced an error during marshalling: %i",
			vptr->type_name,error);
281
	}
Ghislain MARY's avatar
Ghislain MARY committed
282
	memcpy(buff+initial_offset,p,*offset-initial_offset);
283 284 285 286 287 288
	belle_sip_free(p);
	return ret;
}

#endif

Ghislain MARY's avatar
Ghislain MARY committed
289
belle_sip_error_code belle_sip_object_marshal(belle_sip_object_t* obj, char* buff, size_t buff_size, unsigned int *offset) {
jehan's avatar
jehan committed
290 291 292
	belle_sip_object_vptr_t *vptr=obj->vptr;
	while (vptr != NULL) {
		if (vptr->marshal != NULL) {
293
#if CHECKED_MARSHAL
Ghislain MARY's avatar
Ghislain MARY committed
294
			return checked_marshal(vptr,obj,buff,buff_size,offset);
295
#else
Ghislain MARY's avatar
Ghislain MARY committed
296
			return vptr->marshal(obj,buff,buff_size,offset);
297
#endif
jehan's avatar
jehan committed
298 299 300 301
		} else {
			vptr=vptr->parent;
		}
	}
Ghislain MARY's avatar
Ghislain MARY committed
302
	return BELLE_SIP_NOT_IMPLEMENTED; /*no implementation found*/
jehan's avatar
jehan committed
303
}
Simon Morlat's avatar
Simon Morlat committed
304

305 306 307
 
static char * belle_sip_object_to_alloc_string(belle_sip_object_t *obj, int size_hint){
	char *buf=belle_sip_malloc(size_hint);
Ghislain MARY's avatar
Ghislain MARY committed
308 309
	unsigned int offset=0;
	belle_sip_error_code error = belle_sip_object_marshal(obj,buf,size_hint-1,&offset);
310
	obj->vptr->tostring_bufsize_hint=size_hint;
Ghislain MARY's avatar
Ghislain MARY committed
311
	if (error==BELLE_SIP_BUFFER_OVERFLOW){
312 313 314 315
		belle_sip_message("belle_sip_object_to_alloc_string(): hint buffer was too short while doing to_string() for %s, retrying", obj->vptr->type_name);
		belle_sip_free(buf);
		return belle_sip_object_to_alloc_string(obj,2*size_hint);
	}
Ghislain MARY's avatar
Ghislain MARY committed
316 317
	buf=belle_sip_realloc(buf,offset+1);
	buf[offset]='\0';
318 319
	return buf;
}
jehan's avatar
jehan committed
320

321 322 323 324 325 326
static int get_hint_size(int size){
	if (size<128)
		return 128;
	return size;
}

327 328 329 330 331 332
char* belle_sip_object_to_string(void* _obj) {
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(_obj);
	if (obj->vptr->tostring_bufsize_hint!=0){
		return belle_sip_object_to_alloc_string(obj,obj->vptr->tostring_bufsize_hint);
	}else{
		char buff[BELLE_SIP_MAX_TO_STRING_SIZE];
Ghislain MARY's avatar
Ghislain MARY committed
333 334 335
		unsigned int offset=0;
		belle_sip_error_code error = belle_sip_object_marshal(obj,buff,sizeof(buff),&offset);
		if (error==BELLE_SIP_BUFFER_OVERFLOW){
336
			belle_sip_message("belle_sip_object_to_string(): temporary buffer is too short while doing to_string() for %s, retrying", obj->vptr->type_name);
Ghislain MARY's avatar
Ghislain MARY committed
337
			return belle_sip_object_to_alloc_string(obj,get_hint_size(2*offset));
338
		}
Ghislain MARY's avatar
Ghislain MARY committed
339 340
		buff[offset]='\0';
		obj->vptr->tostring_bufsize_hint=get_hint_size(2*offset);
341 342
		return belle_sip_strdup(buff);
	}
jehan's avatar
jehan committed
343
}
Simon Morlat's avatar
Simon Morlat committed
344 345 346 347 348

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;
Simon Morlat's avatar
Simon Morlat committed
349
	unsigned int pos=0;
Simon Morlat's avatar
Simon Morlat committed
350
	belle_sip_list_t *l=NULL,*elem;
Ghislain MARY's avatar
Ghislain MARY committed
351 352
	belle_sip_snprintf(ret,maxbufsize,&pos,"Ownership:\n");
	belle_sip_snprintf(ret,maxbufsize,&pos,"\t%s is created initially %s\n",vptr->type_name,
Simon Morlat's avatar
Simon Morlat committed
353
	              vptr->initially_unowned ? "unowned" : "owned");
Ghislain MARY's avatar
Ghislain MARY committed
354
	belle_sip_snprintf(ret,maxbufsize,&pos,"\nInheritance diagram:\n");
Simon Morlat's avatar
Simon Morlat committed
355 356 357 358 359
	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;
Ghislain MARY's avatar
Ghislain MARY committed
360
		belle_sip_snprintf(ret,maxbufsize,&pos,"\t%s\n",it->type_name);
Simon Morlat's avatar
Simon Morlat committed
361
		if (elem->next)
Ghislain MARY's avatar
Ghislain MARY committed
362
			belle_sip_snprintf(ret,maxbufsize,&pos,"\t        |\n");
Simon Morlat's avatar
Simon Morlat committed
363 364
	}
	belle_sip_list_free(l);
Ghislain MARY's avatar
Ghislain MARY committed
365
	belle_sip_snprintf(ret,maxbufsize,&pos,"\nImplemented interfaces:\n");
Simon Morlat's avatar
Simon Morlat committed
366 367 368 369
	for(it=vptr;it!=NULL;it=it->parent){
		belle_sip_interface_desc_t **desc=it->interfaces;
		if (desc!=NULL){
			for(;*desc!=NULL;desc++){
Ghislain MARY's avatar
Ghislain MARY committed
370
				belle_sip_snprintf(ret,maxbufsize,&pos,"\t* %s\n",(*desc)->ifname);
Simon Morlat's avatar
Simon Morlat committed
371 372 373 374 375 376 377 378 379 380 381
			}
		}
	}
	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
382 383
#if !defined(WIN32)

Simon Morlat's avatar
Simon Morlat committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
#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
406 407 408 409 410 411 412 413 414

#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

415 416 417 418 419 420 421 422
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);
Simon Morlat's avatar
Simon Morlat committed
423
	_belle_sip_object_pool_remove_from_stack(pool);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
}

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;
}

459
int belle_sip_object_pool_cleanable(belle_sip_object_pool_t *pool){
460
	return pool->thread_id!=0 && belle_sip_thread_self()==pool->thread_id;
461 462
}

463 464
void belle_sip_object_pool_clean(belle_sip_object_pool_t *pool){
	belle_sip_list_t *elem,*next;
465 466
	
	if (!belle_sip_object_pool_cleanable(pool)){
467
		belle_sip_warning("Thread pool [%p] cannot be cleaned from thread [%lu] because it was created for thread [%lu]",
jehan's avatar
jehan committed
468
				 pool,(unsigned long)belle_sip_thread_self(),(unsigned long)pool->thread_id);
469 470 471
		return;
	}
	
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	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;
}

488 489 490 491 492
static void belle_sip_object_pool_detach_from_thread(belle_sip_object_pool_t *pool){
	belle_sip_object_pool_clean(pool);
	pool->thread_id=(belle_sip_thread_t)0;
}

493 494
static void cleanup_pool_stack(void *data){
	belle_sip_list_t **pool_stack=(belle_sip_list_t**)data;
495 496 497 498 499 500 501 502
	if (*pool_stack){
		/*
		 * We would expect the pool_stack to be empty when the thread terminates.
		 * Otherwise that means the management of object pool is not properly done by the application.
		 * Since the object pools might be still referenced by the application, we can't destroy them.
		 * Instead, we mark them as detached, so that when the thread that will attempt to destroy them will do it,
		 * we'll accept (since anyway these object pool are no longer needed.
		 */
503
		belle_sip_warning("There were still [%i] object pools for thread [%lu] while the thread exited. ",
504 505 506
				 belle_sip_list_size(*pool_stack),(unsigned long)belle_sip_thread_self());
		belle_sip_list_free_with_data(*pool_stack,(void (*)(void*)) belle_sip_object_pool_detach_from_thread);
	}
507 508 509 510
	*pool_stack=NULL;
	belle_sip_free(pool_stack);
}

511
static belle_sip_list_t** get_current_pool_stack(int *first_time){
512 513 514 515
	static belle_sip_thread_key_t pools_key;
	static int pools_key_created=0;
	belle_sip_list_t **pool_stack;
	
516 517
	if (first_time) *first_time=0;
	
518 519 520 521 522 523 524 525 526 527 528
	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);
529
		if (first_time) *first_time=1;
530 531 532 533
	}
	return pool_stack;
}

Simon Morlat's avatar
Simon Morlat committed
534 535 536 537 538
static void _belle_sip_object_pool_remove_from_stack(belle_sip_object_pool_t *pool){
	belle_sip_list_t **pools=get_current_pool_stack(NULL);
	belle_sip_thread_t tid=belle_sip_thread_self();
	
	if (tid!=pool->thread_id){
539
		belle_sip_fatal("It is forbidden to destroy a pool outside the thread that created it.");
Simon Morlat's avatar
Simon Morlat committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553
		return;
	}
	
	if (pools==NULL) {
		belle_sip_fatal("Not possible to pop a pool.");
		return;
	}
	if (*pools==NULL){
		belle_sip_fatal("There is no current pool in stack.");
		return;
	}
	*pools=belle_sip_list_remove(*pools,pool);
}

554
belle_sip_object_pool_t * belle_sip_object_pool_push(void){
555
	belle_sip_list_t **pools=get_current_pool_stack(NULL);
556 557 558 559 560 561 562 563 564 565
	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;
}

Simon Morlat's avatar
Simon Morlat committed
566

567 568

belle_sip_object_pool_t *belle_sip_object_pool_get_current(void){
569 570
	int first_time;
	belle_sip_list_t **pools=get_current_pool_stack(&first_time);
571
	if (pools==NULL) return NULL;
572 573
	if (*pools==NULL ){
		if (first_time) {
574
			belle_sip_warning("There is no object pool created in thread [%lu]. "
575
			"Use belle_sip_object_pool_push() to create one. Unowned objects not unref'd will be leaked.",
jehan's avatar
jehan committed
576
			(unsigned long)belle_sip_thread_self());
577
		}
578 579 580 581 582 583
		return NULL;
	}
	return (belle_sip_object_pool_t*)(*pools)->data;
}


Simon Morlat's avatar
Simon Morlat committed
584