2012-11-30 16:25:07 +00:00
|
|
|
/*
|
|
|
|
* gstvaapiminiobject.c - A lightweight reference counted object
|
|
|
|
*
|
2014-12-02 13:15:49 +00:00
|
|
|
* Copyright (C) 2012-2014 Intel Corporation
|
2013-11-22 04:57:18 +00:00
|
|
|
* Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
|
2012-11-30 16:25:07 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2.1
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "gstvaapiminiobject.h"
|
|
|
|
|
2018-09-13 10:22:42 +00:00
|
|
|
static void
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_free (GstVaapiMiniObject * object)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
const GstVaapiMiniObjectClass *const klass = object->object_class;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
g_atomic_int_inc (&object->ref_count);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (klass->finalize)
|
|
|
|
klass->finalize (object);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (G_LIKELY (g_atomic_int_dec_and_test (&object->ref_count)))
|
2023-01-24 19:16:47 +00:00
|
|
|
g_free (object);
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_new:
|
|
|
|
* @object_class: (optional): The object class
|
|
|
|
*
|
|
|
|
* Creates a new #GstVaapiMiniObject. If @object_class is NULL, then the
|
|
|
|
* size of the allocated object is the same as sizeof(GstVaapiMiniObject).
|
|
|
|
* If @object_class is not NULL, typically when a sub-class is implemented,
|
|
|
|
* that pointer shall reference a statically allocated descriptor.
|
|
|
|
*
|
|
|
|
* This function does *not* zero-initialize the derived object data,
|
|
|
|
* use gst_vaapi_mini_object_new0() to fill this purpose.
|
|
|
|
*
|
|
|
|
* Returns: The newly allocated #GstVaapiMiniObject
|
|
|
|
*/
|
|
|
|
GstVaapiMiniObject *
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_new (const GstVaapiMiniObjectClass * object_class)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
GstVaapiMiniObject *object;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
static const GstVaapiMiniObjectClass default_object_class = {
|
|
|
|
.size = sizeof (GstVaapiMiniObject),
|
|
|
|
};
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (G_UNLIKELY (!object_class))
|
|
|
|
object_class = &default_object_class;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
g_return_val_if_fail (object_class->size >= sizeof (*object), NULL);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2023-01-24 19:16:47 +00:00
|
|
|
object = g_malloc (object_class->size);
|
2014-12-02 13:15:49 +00:00
|
|
|
if (!object)
|
|
|
|
return NULL;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
object->object_class = object_class;
|
2021-03-19 06:42:36 +00:00
|
|
|
g_atomic_int_set (&object->ref_count, 1);
|
2014-12-02 13:15:49 +00:00
|
|
|
object->flags = 0;
|
|
|
|
return object;
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_new0:
|
|
|
|
* @object_class: (optional): The object class
|
|
|
|
*
|
|
|
|
* Creates a new #GstVaapiMiniObject. This function is similar to
|
|
|
|
* gst_vaapi_mini_object_new() but derived object data is initialized
|
|
|
|
* to zeroes.
|
|
|
|
*
|
|
|
|
* Returns: The newly allocated #GstVaapiMiniObject
|
|
|
|
*/
|
|
|
|
GstVaapiMiniObject *
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_new0 (const GstVaapiMiniObjectClass * object_class)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
GstVaapiMiniObject *object;
|
|
|
|
guint sub_size;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
object = gst_vaapi_mini_object_new (object_class);
|
|
|
|
if (!object)
|
|
|
|
return NULL;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
object_class = object->object_class;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
sub_size = object_class->size - sizeof (*object);
|
|
|
|
if (sub_size > 0)
|
|
|
|
memset (((guchar *) object) + sizeof (*object), 0, sub_size);
|
|
|
|
return object;
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:22:42 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_ref_internal:
|
|
|
|
* @object: a #GstVaapiMiniObject
|
|
|
|
*
|
|
|
|
* Atomically increases the reference count of the given @object by one.
|
|
|
|
* This is an internal function that does not do any run-time type check.
|
|
|
|
*
|
|
|
|
* Returns: The same @object argument
|
|
|
|
*/
|
|
|
|
static inline GstVaapiMiniObject *
|
|
|
|
gst_vaapi_mini_object_ref_internal (GstVaapiMiniObject * object)
|
|
|
|
{
|
|
|
|
g_atomic_int_inc (&object->ref_count);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2012-11-30 16:25:07 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_ref:
|
|
|
|
* @object: a #GstVaapiMiniObject
|
|
|
|
*
|
|
|
|
* Atomically increases the reference count of the given @object by one.
|
|
|
|
*
|
|
|
|
* Returns: The same @object argument
|
|
|
|
*/
|
|
|
|
GstVaapiMiniObject *
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_ref (GstVaapiMiniObject * object)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
g_return_val_if_fail (object != NULL, NULL);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 15:51:20 +00:00
|
|
|
return gst_vaapi_mini_object_ref_internal (object);
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:22:42 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_unref_internal:
|
|
|
|
* @object: a #GstVaapiMiniObject
|
|
|
|
*
|
|
|
|
* Atomically decreases the reference count of the @object by one. If
|
|
|
|
* the reference count reaches zero, the object will be free'd.
|
|
|
|
*
|
|
|
|
* This is an internal function that does not do any run-time type check.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
gst_vaapi_mini_object_unref_internal (GstVaapiMiniObject * object)
|
|
|
|
{
|
|
|
|
if (g_atomic_int_dec_and_test (&object->ref_count))
|
|
|
|
gst_vaapi_mini_object_free (object);
|
|
|
|
}
|
|
|
|
|
2012-11-30 16:25:07 +00:00
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_unref:
|
|
|
|
* @object: a #GstVaapiMiniObject
|
|
|
|
*
|
|
|
|
* Atomically decreases the reference count of the @object by one. If
|
|
|
|
* the reference count reaches zero, the object will be free'd.
|
|
|
|
*/
|
|
|
|
void
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_unref (GstVaapiMiniObject * object)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
g_return_if_fail (object != NULL);
|
|
|
|
g_return_if_fail (object->ref_count > 0);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 15:51:20 +00:00
|
|
|
gst_vaapi_mini_object_unref_internal (object);
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vaapi_mini_object_replace:
|
|
|
|
* @old_object_ptr: a pointer to a #GstVaapiMiniObject
|
|
|
|
* @new_object: a #GstVaapiMiniObject
|
|
|
|
*
|
|
|
|
* Atomically replaces the object held in @old_object_ptr with
|
|
|
|
* @new_object. This means that @old_object_ptr shall reference a
|
|
|
|
* valid object. However, @new_object can be NULL.
|
|
|
|
*/
|
|
|
|
void
|
2014-12-02 13:15:49 +00:00
|
|
|
gst_vaapi_mini_object_replace (GstVaapiMiniObject ** old_object_ptr,
|
|
|
|
GstVaapiMiniObject * new_object)
|
2012-11-30 16:25:07 +00:00
|
|
|
{
|
2014-12-02 13:15:49 +00:00
|
|
|
GstVaapiMiniObject *old_object;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
g_return_if_fail (old_object_ptr != NULL);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
old_object = g_atomic_pointer_get ((gpointer *) old_object_ptr);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (old_object == new_object)
|
|
|
|
return;
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (new_object)
|
2014-12-02 15:51:20 +00:00
|
|
|
gst_vaapi_mini_object_ref_internal (new_object);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
while (!g_atomic_pointer_compare_and_exchange ((gpointer *) old_object_ptr,
|
2020-08-04 18:15:01 +00:00
|
|
|
(gpointer) old_object, new_object))
|
2014-12-02 13:15:49 +00:00
|
|
|
old_object = g_atomic_pointer_get ((gpointer *) old_object_ptr);
|
2012-11-30 16:25:07 +00:00
|
|
|
|
2014-12-02 13:15:49 +00:00
|
|
|
if (old_object)
|
2014-12-02 15:51:20 +00:00
|
|
|
gst_vaapi_mini_object_unref_internal (old_object);
|
2012-11-30 16:25:07 +00:00
|
|
|
}
|