libs: refine GstVaapiMiniObject.

Drop support for user-defined data since this capability was not used
so far and GstVaapiMiniObject represents the smallest reference counted
object type. Add missing GST_VAAPI_MINI_OBJECT_CLASS() helper macro.

Besides, since GstVaapiMiniObject is a libgstvaapi internal object, it
is also possible to further simplify the layout of the object. i.e. merge
GstVaapiMiniObjectBase into GstVaapiMiniObject.
This commit is contained in:
Gwenole Beauchesne 2013-04-30 10:28:30 +02:00
parent da2493f731
commit 6439169db5
6 changed files with 32 additions and 115 deletions

View file

@ -42,7 +42,7 @@ const GstVaapiCodecObjectClass *
gst_vaapi_codec_object_get_class(GstVaapiCodecObject *object) gst_vaapi_codec_object_get_class(GstVaapiCodecObject *object)
{ {
return (const GstVaapiCodecObjectClass *) return (const GstVaapiCodecObjectClass *)
gst_vaapi_mini_object_get_class(GST_VAAPI_MINI_OBJECT(object)); GST_VAAPI_MINI_OBJECT_GET_CLASS(object);
} }
static gboolean static gboolean

View file

@ -29,8 +29,7 @@
((GstVaapiDpbClass *)(klass)) ((GstVaapiDpbClass *)(klass))
#define GST_VAAPI_DPB_GET_CLASS(obj) \ #define GST_VAAPI_DPB_GET_CLASS(obj) \
GST_VAAPI_DPB_CLASS(gst_vaapi_mini_object_get_class( \ GST_VAAPI_DPB_CLASS(GST_VAAPI_MINI_OBJECT_GET_CLASS(obj))
GST_VAAPI_MINI_OBJECT(obj)))
/** /**
* GstVaapiDpb: * GstVaapiDpb:

View file

@ -57,6 +57,7 @@ typedef struct _GstVaapiPictureH264 GstVaapiPictureH264;
((GstVaapiParserInfoH264 *)(obj)) ((GstVaapiParserInfoH264 *)(obj))
struct _GstVaapiParserInfoH264 { struct _GstVaapiParserInfoH264 {
GstVaapiMiniObject parent_instance;
GstH264NalUnit nalu; GstH264NalUnit nalu;
union { union {
GstH264SPS sps; GstH264SPS sps;

View file

@ -258,6 +258,7 @@ struct _GstMpegVideoSliceHdr {
typedef struct _GstVaapiParserInfoMpeg2 GstVaapiParserInfoMpeg2; typedef struct _GstVaapiParserInfoMpeg2 GstVaapiParserInfoMpeg2;
struct _GstVaapiParserInfoMpeg2 { struct _GstVaapiParserInfoMpeg2 {
GstVaapiMiniObject parent_instance;
GstMpegVideoPacket packet; GstMpegVideoPacket packet;
guint8 extension_type; /* for Extension packets */ guint8 extension_type; /* for Extension packets */
union { union {

View file

@ -22,51 +22,18 @@
#include <string.h> #include <string.h>
#include "gstvaapiminiobject.h" #include "gstvaapiminiobject.h"
typedef struct _GstVaapiMiniObjectBase GstVaapiMiniObjectBase;
struct _GstVaapiMiniObjectBase {
gconstpointer object_class;
gint ref_count;
GDestroyNotify user_data_destroy_notify;
};
static inline GstVaapiMiniObjectBase *
object2base(GstVaapiMiniObject *object)
{
return GSIZE_TO_POINTER(GPOINTER_TO_SIZE(object) -
sizeof(GstVaapiMiniObjectBase));
}
static inline GstVaapiMiniObject *
base2object(GstVaapiMiniObjectBase *base_object)
{
return GSIZE_TO_POINTER(GPOINTER_TO_SIZE(base_object) +
sizeof(GstVaapiMiniObjectBase));
}
static void static void
gst_vaapi_mini_object_free(GstVaapiMiniObject *object) gst_vaapi_mini_object_free(GstVaapiMiniObject *object)
{ {
GstVaapiMiniObjectBase * const base_object = object2base(object); const GstVaapiMiniObjectClass * const klass = object->object_class;
const GstVaapiMiniObjectClass * const klass = base_object->object_class;
g_atomic_int_inc(&base_object->ref_count); g_atomic_int_inc(&object->ref_count);
if (klass->finalize) if (klass->finalize)
klass->finalize(object); klass->finalize(object);
if (G_LIKELY(g_atomic_int_dec_and_test(&base_object->ref_count))) { if (G_LIKELY(g_atomic_int_dec_and_test(&object->ref_count)))
if (object->user_data && base_object->user_data_destroy_notify) g_slice_free1(klass->size, object);
base_object->user_data_destroy_notify(object->user_data);
g_slice_free1(sizeof(*base_object) + klass->size, base_object);
}
}
const GstVaapiMiniObjectClass *
gst_vaapi_mini_object_get_class(GstVaapiMiniObject *object)
{
g_return_val_if_fail(object != NULL, NULL);
return object2base(object)->object_class;
} }
/** /**
@ -87,28 +54,23 @@ GstVaapiMiniObject *
gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class) gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class)
{ {
GstVaapiMiniObject *object; GstVaapiMiniObject *object;
GstVaapiMiniObjectBase *base_object;
static const GstVaapiMiniObjectClass default_object_class = { static const GstVaapiMiniObjectClass default_object_class = {
.size = sizeof(GstVaapiMiniObject), .size = sizeof(GstVaapiMiniObject),
}; };
if (!object_class) if (G_UNLIKELY(!object_class))
object_class = &default_object_class; object_class = &default_object_class;
g_return_val_if_fail(object_class->size >= sizeof(*object), NULL); g_return_val_if_fail(object_class->size >= sizeof(*object), NULL);
base_object = g_slice_alloc(sizeof(*base_object) + object_class->size); object = g_slice_alloc(object_class->size);
if (!base_object) if (!object)
return NULL; return NULL;
object = base2object(base_object); object->object_class = object_class;
object->ref_count = 1;
object->flags = 0; object->flags = 0;
object->user_data = 0;
base_object->object_class = object_class;
base_object->ref_count = 1;
base_object->user_data_destroy_notify = NULL;
return object; return object;
} }
@ -132,7 +94,7 @@ gst_vaapi_mini_object_new0(const GstVaapiMiniObjectClass *object_class)
if (!object) if (!object)
return NULL; return NULL;
object_class = object2base(object)->object_class; object_class = object->object_class;
sub_size = object_class->size - sizeof(*object); sub_size = object_class->size - sizeof(*object);
if (sub_size > 0) if (sub_size > 0)
@ -153,7 +115,7 @@ gst_vaapi_mini_object_ref(GstVaapiMiniObject *object)
{ {
g_return_val_if_fail(object != NULL, NULL); g_return_val_if_fail(object != NULL, NULL);
g_atomic_int_inc(&object2base(object)->ref_count); g_atomic_int_inc(&object->ref_count);
return object; return object;
} }
@ -168,9 +130,9 @@ void
gst_vaapi_mini_object_unref(GstVaapiMiniObject *object) gst_vaapi_mini_object_unref(GstVaapiMiniObject *object)
{ {
g_return_if_fail(object != NULL); g_return_if_fail(object != NULL);
g_return_if_fail(object2base(object)->ref_count > 0); g_return_if_fail(object->ref_count > 0);
if (g_atomic_int_dec_and_test(&object2base(object)->ref_count)) if (g_atomic_int_dec_and_test(&object->ref_count))
gst_vaapi_mini_object_free(object); gst_vaapi_mini_object_free(object);
} }
@ -206,48 +168,3 @@ gst_vaapi_mini_object_replace(GstVaapiMiniObject **old_object_ptr,
if (old_object) if (old_object)
gst_vaapi_mini_object_unref(old_object); gst_vaapi_mini_object_unref(old_object);
} }
/**
* gst_vaapi_mini_object_get_user_data:
* @object: a #GstVaapiMiniObject
*
* Gets user-provided data set on the object via a previous call to
* gst_vaapi_mini_object_set_user_data().
*
* Returns: (transfer none): The previously set user_data
*/
gpointer
gst_vaapi_mini_object_get_user_data(GstVaapiMiniObject *object)
{
g_return_val_if_fail(object != NULL, NULL);
return object->user_data;
}
/**
* gst_vaapi_mini_object_set_user_data:
* @object: a #GstVaapiMiniObject
* @user_data: user-provided data
* @destroy_notify: (closure user_data): a #GDestroyNotify
*
* Sets @user_data on the object and the #GDestroyNotify that will be
* called when the data is freed.
*
* If some @user_data was previously set, then the former @destroy_notify
* function will be called before the @user_data is replaced.
*/
void
gst_vaapi_mini_object_set_user_data(GstVaapiMiniObject *object,
gpointer user_data, GDestroyNotify destroy_notify)
{
GstVaapiMiniObjectBase *base_object;
g_return_if_fail(object != NULL);
base_object = object2base(object);
if (object->user_data && base_object->user_data_destroy_notify)
base_object->user_data_destroy_notify(object->user_data);
object->user_data = user_data;
base_object->user_data_destroy_notify = destroy_notify;
}

View file

@ -38,6 +38,15 @@ typedef struct _GstVaapiMiniObjectClass GstVaapiMiniObjectClass;
#define GST_VAAPI_MINI_OBJECT(object) \ #define GST_VAAPI_MINI_OBJECT(object) \
((GstVaapiMiniObject *)(object)) ((GstVaapiMiniObject *)(object))
/**
* GST_VAAPI_MINI_OBJECT_CLASS:
* @klass: a #GstVaapiMiniObjectClass
*
* Casts the @klass to a #GstVaapiMiniObjectClass
*/
#define GST_VAAPI_MINI_OBJECT_CLASS(klass) \
((GstVaapiMiniObjectClass *)(klass))
/** /**
* GST_VAAPI_MINI_OBJECT_GET_CLASS: * GST_VAAPI_MINI_OBJECT_GET_CLASS:
* @object: a #GstVaapiMiniObject * @object: a #GstVaapiMiniObject
@ -45,7 +54,7 @@ typedef struct _GstVaapiMiniObjectClass GstVaapiMiniObjectClass;
* Retrieves the #GstVaapiMiniObjectClass associated with the @object * Retrieves the #GstVaapiMiniObjectClass associated with the @object
*/ */
#define GST_VAAPI_MINI_OBJECT_GET_CLASS(object) \ #define GST_VAAPI_MINI_OBJECT_GET_CLASS(object) \
gst_vaapi_mini_object_get_class(GST_VAAPI_MINI_OBJECT(object)) (GST_VAAPI_MINI_OBJECT(object)->object_class)
/** /**
* GST_VAAPI_MINI_OBJECT_FLAGS: * GST_VAAPI_MINI_OBJECT_FLAGS:
@ -88,17 +97,20 @@ typedef struct _GstVaapiMiniObjectClass GstVaapiMiniObjectClass;
/** /**
* GstVaapiMiniObject: * GstVaapiMiniObject:
* @object_class: the #GstVaapiMiniObjectClass
* @ref_count: the object reference count that should be manipulated
* through gst_vaapi_mini_object_ref() et al. helpers
* @flags: set of flags that should be manipulated through * @flags: set of flags that should be manipulated through
* GST_VAAPI_MINI_OBJECT_FLAG_*() functions * GST_VAAPI_MINI_OBJECT_FLAG_*() functions
* @user_data: user-provided data from gst_vaapi_mini_object_set_user_data()
* *
* A #GstVaapiMiniObject represents a minimal reference counted data * A #GstVaapiMiniObject represents a minimal reference counted data
* structure that can hold a set of flags and user-provided data. * structure that can hold a set of flags and user-provided data.
*/ */
struct _GstVaapiMiniObject { struct _GstVaapiMiniObject {
/*< private >*/ /*< private >*/
gconstpointer object_class;
volatile gint ref_count;
guint flags; guint flags;
gpointer user_data;
}; };
/** /**
@ -116,10 +128,6 @@ struct _GstVaapiMiniObjectClass {
GDestroyNotify finalize; GDestroyNotify finalize;
}; };
G_GNUC_INTERNAL
const GstVaapiMiniObjectClass *
gst_vaapi_mini_object_get_class(GstVaapiMiniObject *object) G_GNUC_CONST;
G_GNUC_INTERNAL G_GNUC_INTERNAL
GstVaapiMiniObject * GstVaapiMiniObject *
gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class); gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class);
@ -141,15 +149,6 @@ void
gst_vaapi_mini_object_replace(GstVaapiMiniObject **old_object_ptr, gst_vaapi_mini_object_replace(GstVaapiMiniObject **old_object_ptr,
GstVaapiMiniObject *new_object); GstVaapiMiniObject *new_object);
G_GNUC_INTERNAL
gpointer
gst_vaapi_mini_object_get_user_data(GstVaapiMiniObject *object);
G_GNUC_INTERNAL
void
gst_vaapi_mini_object_set_user_data(GstVaapiMiniObject *object,
gpointer user_data, GDestroyNotify destroy_notify);
G_END_DECLS G_END_DECLS
#endif /* GST_VAAPI_MINI_OBJECT_H */ #endif /* GST_VAAPI_MINI_OBJECT_H */