Ejemplo n.º 1
0
/*
 * mowgli_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
mowgli_object_unref(void *object)
{
	mowgli_object_t *obj = mowgli_object(object);

	return_if_fail(object != NULL);

	obj->refcount--;

	if (obj->refcount <= 0)
	{
		mowgli_object_message_broadcast(obj, "destroy");

		if (obj->name != NULL)
			free(obj->name);

		if (obj->klass != NULL)
		{
			mowgli_destructor_t destructor = obj->klass->destructor;

			if (obj->klass->dynamic == TRUE)
				mowgli_object_class_destroy(obj->klass);

			if (destructor != NULL)
				destructor(obj);
			else
				free(obj);
		}
		else
		{
			mowgli_log_warning("invalid object class");
		}
	}
}
Ejemplo n.º 2
0
/*
 * mowgli_object_ref
 *
 * Increment the reference counter on an object.
 *
 * Inputs:
 *      - the object to refcount
 *
 * Outputs:
 *      - none
 *
 * Side Effects:
 *      - none
 */
void *
mowgli_object_ref(void *object)
{
	return_val_if_fail(object != NULL, NULL);

	mowgli_object(object)->refcount++;

	return object;
}
Ejemplo n.º 3
0
mowgli_allocation_policy_t *
mowgli_allocation_policy_create(const char *name, mowgli_allocation_func_t allocator,
	mowgli_deallocation_func_t deallocator)
{
	mowgli_allocation_policy_t *policy;

	if (mowgli_allocation_policy_dict == NULL)
		mowgli_allocation_policy_dict = mowgli_patricia_create(_allocation_policy_key_canon);

	if ((policy = mowgli_patricia_retrieve(mowgli_allocation_policy_dict, name)))
		return policy;

	policy = mowgli_alloc(sizeof(mowgli_allocation_policy_t));
	mowgli_object_init_from_class(mowgli_object(policy), name, &klass);

	policy->allocate = allocator;
	policy->deallocate = deallocator;

	mowgli_patricia_add(mowgli_allocation_policy_dict, name, policy);

	return policy;
}