belle_sip_object.c 2.64 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
	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"

static uint8_t *find_type(belle_sip_object_t *obj, belle_sip_type_id_t id){
	int i;
	for(i=0;i<sizeof(obj->type_ids);++i){
		if (obj->type_ids[i]==(uint8_t)id)
			return &obj->type_ids[i];
	}
	return NULL;
}

void _belle_sip_object_init_type(belle_sip_object_t *obj, belle_sip_type_id_t id){
	uint8_t * t=find_type(obj,id);
	if (t!=NULL) belle_sip_fatal("This object already inherits type %i",id);
	t=find_type(obj,0);
	if (t==NULL) belle_sip_fatal("This object has too much inheritance !");
	*t=id;
}

38
belle_sip_object_t * _belle_sip_object_new(size_t objsize, belle_sip_type_id_t id, void *vptr, belle_sip_object_destroy_t destroy_func, int initially_unowed){
Simon Morlat's avatar
Simon Morlat committed
39 40 41
	belle_sip_object_t *obj=(belle_sip_object_t *)belle_sip_malloc0(objsize);
	obj->type_ids[0]=id;
	obj->ref=initially_unowed ? 0 : 1;
42
	obj->vptr=vptr;
Simon Morlat's avatar
Simon Morlat committed
43 44 45 46 47 48 49 50
	obj->destroy=destroy_func;
	return obj;
}

int belle_sip_object_is_unowed(const belle_sip_object_t *obj){
	return obj->ref==0;
}

51 52
belle_sip_object_t * belle_sip_object_ref(void *obj){
	BELLE_SIP_OBJECT(obj)->ref++;
jehan's avatar
jehan committed
53
	return obj;
Simon Morlat's avatar
Simon Morlat committed
54 55
}

56 57
void belle_sip_object_unref(void *ptr){
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
Simon Morlat's avatar
Simon Morlat committed
58 59 60 61 62 63 64 65 66 67 68
	if (obj->ref==0){
		belle_sip_warning("Destroying unowed object");
		belle_sip_object_destroy(obj);
		return;
	}
	obj->ref--;
	if (obj->ref==0){
		belle_sip_object_destroy(obj);
	}
}

69 70
void belle_sip_object_destroy(void *ptr){
	belle_sip_object_t *obj=BELLE_SIP_OBJECT(ptr);
Simon Morlat's avatar
Simon Morlat committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
	if (obj->ref!=0){
		belle_sip_error("Destroying referenced object !");
		if (obj->destroy) obj->destroy(obj);
		belle_sip_free(obj);
	}
}

void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno){
	if (find_type(obj,id)==NULL){
		belle_sip_fatal("Bad cast to %s at %s:%i",castname,file,fileno);
		return NULL;
	}
	return obj;
}
jehan's avatar
jehan committed
85 86 87
void belle_sip_object_init(belle_sip_object_t *obj) {
	belle_sip_object_init_type(obj,belle_sip_object_t);
}
Simon Morlat's avatar
Simon Morlat committed
88