/* * object.C: Object management. * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Rights to this code are documented in doc/pod/license.pod. * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) */ static char const rcsid[] = "$Id: object.C,v 1.5 2007/09/16 18:54:45 pippijn Exp $"; #include "atheme.h" /* * object_init * * Populates the object manager part of an object. * * Inputs: * - pointer to object manager area * - (optional) name of object * - (optional) custom destructor * * Outputs: * - none * * Side Effects: * - none */ void object_init (object_t * obj, const char *name, destructor_t des) { return_if_fail (obj != NULL); if (name != NULL) obj->name = sstrdup (name); else obj->name = sstrdup (""); obj->destructor = des; obj->refcount = 1; } /* * object_ref * * Increment the reference counter on an object. * * Inputs: * - the object to refcount * * Outputs: * - none * * Side Effects: * - none */ void * object_ref (void *object) { return_val_if_fail (object != NULL, NULL); asobject (object)->refcount++; return object; } /* * object_unref * * Decrement the reference counter on an object. * * Inputs: * - the object to refcount * * Outputs: * - none * * Side Effects: * - if the refcount is 0, the object is destroyed. */ void object_unref (void *object) { object_t *obj; return_if_fail (object != NULL); obj = asobject (object); obj->refcount--; if (obj->refcount <= 0) { sfree (obj->name); if (obj->destructor != NULL) obj->destructor (obj); else sfree (obj); } }