iterator: Refactor GstIterator to be more binding friendly and have saner refcounting

Fixes bug #638987.
This commit is contained in:
Sebastian Dröge 2011-03-16 10:50:39 +01:00
parent 62c7339847
commit 348563bc19
3 changed files with 231 additions and 168 deletions

View file

@ -1,5 +1,6 @@
/* GStreamer /* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com> * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
* Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
* *
* gstiterator.h: Base class for iterating datastructures. * gstiterator.h: Base class for iterating datastructures.
* *
@ -46,7 +47,7 @@
* switch (gst_iterator_next (it, &amp;item)) { * switch (gst_iterator_next (it, &amp;item)) {
* case GST_ITERATOR_OK: * case GST_ITERATOR_OK:
* ... use/change item here... * ... use/change item here...
* gst_object_unref (item); * g_value_reset (&amp;item);
* break; * break;
* case GST_ITERATOR_RESYNC: * case GST_ITERATOR_RESYNC:
* ...rollback changes to items... * ...rollback changes to items...
@ -61,6 +62,7 @@
* break; * break;
* } * }
* } * }
* g_value_unset (&amp;item);
* gst_iterator_free (it); * gst_iterator_free (it);
* </programlisting> * </programlisting>
* </example> * </example>
@ -71,10 +73,16 @@
#include "gst_private.h" #include "gst_private.h"
#include <gst/gstiterator.h> #include <gst/gstiterator.h>
static GstIterator * GstIterator *
gst_iterator_copy (GstIterator * it) gst_iterator_copy (const GstIterator * it)
{ {
return g_slice_copy (it->size, it); GstIterator *copy;
copy = g_slice_copy (it->size, it);
if (it->copy)
it->copy (it, copy);
return copy;
} }
GType GType
@ -94,6 +102,7 @@ gst_iterator_init (GstIterator * it,
GType type, GType type,
GMutex * lock, GMutex * lock,
guint32 * master_cookie, guint32 * master_cookie,
GstIteratorCopyFunction copy,
GstIteratorNextFunction next, GstIteratorNextFunction next,
GstIteratorItemFunction item, GstIteratorItemFunction item,
GstIteratorResyncFunction resync, GstIteratorFreeFunction free) GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
@ -103,6 +112,7 @@ gst_iterator_init (GstIterator * it,
it->lock = lock; it->lock = lock;
it->master_cookie = master_cookie; it->master_cookie = master_cookie;
it->cookie = *master_cookie; it->cookie = *master_cookie;
it->copy = copy;
it->next = next; it->next = next;
it->item = item; it->item = item;
it->resync = resync; it->resync = resync;
@ -117,6 +127,7 @@ gst_iterator_init (GstIterator * it,
* @lock: pointer to a #GMutex. * @lock: pointer to a #GMutex.
* @master_cookie: pointer to a guint32 that is changed when the items in the * @master_cookie: pointer to a guint32 that is changed when the items in the
* iterator changed. * iterator changed.
* @copy: copy function
* @next: function to get next item * @next: function to get next item
* @item: function to call on each item retrieved * @item: function to call on each item retrieved
* @resync: function to resync the iterator * @resync: function to resync the iterator
@ -137,6 +148,7 @@ gst_iterator_new (guint size,
GType type, GType type,
GMutex * lock, GMutex * lock,
guint32 * master_cookie, guint32 * master_cookie,
GstIteratorCopyFunction copy,
GstIteratorNextFunction next, GstIteratorNextFunction next,
GstIteratorItemFunction item, GstIteratorItemFunction item,
GstIteratorResyncFunction resync, GstIteratorFreeFunction free) GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
@ -150,8 +162,8 @@ gst_iterator_new (guint size,
g_return_val_if_fail (resync != NULL, NULL); g_return_val_if_fail (resync != NULL, NULL);
g_return_val_if_fail (free != NULL, NULL); g_return_val_if_fail (free != NULL, NULL);
result = g_slice_alloc (size); result = g_slice_alloc0 (size);
gst_iterator_init (result, size, type, lock, master_cookie, next, item, gst_iterator_init (result, size, type, lock, master_cookie, copy, next, item,
resync, free); resync, free);
return result; return result;
@ -163,21 +175,33 @@ gst_iterator_new (guint size,
typedef struct _GstListIterator typedef struct _GstListIterator
{ {
GstIterator iterator; GstIterator iterator;
gpointer owner; GObject *owner;
GList **orig; GList **orig;
GList *list; /* pointer in list */ GList *list; /* pointer in list */
GstIteratorDisposeFunction freefunc;
void (*set_value) (GValue * value, gpointer item);
} GstListIterator; } GstListIterator;
static GstIteratorResult static void
gst_list_iterator_next (GstListIterator * it, gpointer * elem) gst_list_iterator_copy (const GstListIterator * it, GstListIterator * copy)
{ {
if (copy->owner)
g_object_ref (copy->owner);
}
static GstIteratorResult
gst_list_iterator_next (GstListIterator * it, GValue * elem)
{
gpointer data;
if (it->list == NULL) if (it->list == NULL)
return GST_ITERATOR_DONE; return GST_ITERATOR_DONE;
*elem = it->list->data; data = it->list->data;
it->list = g_list_next (it->list); it->list = g_list_next (it->list);
it->set_value (elem, data);
return GST_ITERATOR_OK; return GST_ITERATOR_OK;
} }
@ -190,9 +214,8 @@ gst_list_iterator_resync (GstListIterator * it)
static void static void
gst_list_iterator_free (GstListIterator * it) gst_list_iterator_free (GstListIterator * it)
{ {
if (it->freefunc) { if (it->owner)
it->freefunc (it->owner); g_object_unref (it->owner);
}
} }
/** /**
@ -203,8 +226,7 @@ gst_list_iterator_free (GstListIterator * it)
* is changed. * is changed.
* @list: pointer to the list * @list: pointer to the list
* @owner: object owning the list * @owner: object owning the list
* @item: function to call for each item * @item: function to call on each item retrieved
* @free: function to call when the iterator is freed
* *
* Create a new iterator designed for iterating @list. * Create a new iterator designed for iterating @list.
* *
@ -214,46 +236,52 @@ gst_list_iterator_free (GstListIterator * it)
* The iterator will use @lock to retrieve the next item of the list and it * The iterator will use @lock to retrieve the next item of the list and it
* will then call the @item function before releasing @lock again. * will then call the @item function before releasing @lock again.
* *
* The @item function usualy makes sure that the item remains alive while
* @lock is released and the application is using the item. The application is
* responsible for freeing/unreffing the item after usage as explained in
* gst_iterator_next().
*
* When a concurrent update to the list is performed, usually by @owner while * When a concurrent update to the list is performed, usually by @owner while
* holding @lock, @master_cookie will be updated. The iterator implementation * holding @lock, @master_cookie will be updated. The iterator implementation
* will notice the update of the cookie and will return %GST_ITERATOR_RESYNC to * will notice the update of the cookie and will return %GST_ITERATOR_RESYNC to
* the user of the iterator in the next call to gst_iterator_next(). * the user of the iterator in the next call to gst_iterator_next().
* *
* @owner will be passed to the @free function when the iterator is freed.
*
* Returns: the new #GstIterator for @list. * Returns: the new #GstIterator for @list.
* *
* MT safe. * MT safe.
*/ */
GstIterator * GstIterator *
gst_iterator_new_list (GType type, gst_iterator_new_list (GType type,
GMutex * lock, GMutex * lock, guint32 * master_cookie, GList ** list, GObject * owner,
guint32 * master_cookie, GstIteratorItemFunction item)
GList ** list,
gpointer owner,
GstIteratorItemFunction item, GstIteratorDisposeFunction free)
{ {
GstListIterator *result; GstListIterator *result;
gpointer set_value;
if (g_type_is_a (type, G_TYPE_OBJECT)) {
set_value = g_value_set_object;
} else if (g_type_is_a (type, G_TYPE_BOXED)) {
set_value = g_value_set_boxed;
} else if (g_type_is_a (type, G_TYPE_POINTER)) {
set_value = g_value_set_pointer;
} else if (g_type_is_a (type, G_TYPE_STRING)) {
set_value = g_value_set_string;
} else {
g_critical ("List iterators can only be created for lists containing "
"instances of GObject, boxed types, pointer types and strings");
return NULL;
}
/* no need to lock, nothing can change here */ /* no need to lock, nothing can change here */
result = (GstListIterator *) gst_iterator_new (sizeof (GstListIterator), result = (GstListIterator *) gst_iterator_new (sizeof (GstListIterator),
type, type,
lock, lock,
master_cookie, master_cookie,
(GstIteratorCopyFunction) gst_list_iterator_copy,
(GstIteratorNextFunction) gst_list_iterator_next, (GstIteratorNextFunction) gst_list_iterator_next,
(GstIteratorItemFunction) item, (GstIteratorItemFunction) item,
(GstIteratorResyncFunction) gst_list_iterator_resync, (GstIteratorResyncFunction) gst_list_iterator_resync,
(GstIteratorFreeFunction) gst_list_iterator_free); (GstIteratorFreeFunction) gst_list_iterator_free);
result->owner = owner; result->owner = owner ? g_object_ref (owner) : NULL;
result->orig = list; result->orig = list;
result->list = *list; result->list = *list;
result->freefunc = free; result->set_value = set_value;
return GST_ITERATOR (result); return GST_ITERATOR (result);
} }
@ -275,9 +303,10 @@ gst_iterator_pop (GstIterator * it)
* Get the next item from the iterator in @elem. * Get the next item from the iterator in @elem.
* *
* Only when this function returns %GST_ITERATOR_OK, @elem will contain a valid * Only when this function returns %GST_ITERATOR_OK, @elem will contain a valid
* value. For iterators that return refcounted objects, the returned object * value. @elem must have been initialized to the type of the iterator or
* will have its refcount increased and should therefore be unreffed after * initialized to zeroes with g_value_unset(). The caller is responsible for
* usage. * unsetting or resetting @elem with g_value_unset() or g_value_reset()
* after usage.
* *
* When this function returns %GST_ITERATOR_DONE, no more elements can be * When this function returns %GST_ITERATOR_DONE, no more elements can be
* retrieved from @it. * retrieved from @it.
@ -288,18 +317,22 @@ gst_iterator_pop (GstIterator * it)
* *
* A return value of %GST_ITERATOR_ERROR indicates an unrecoverable fatal error. * A return value of %GST_ITERATOR_ERROR indicates an unrecoverable fatal error.
* *
* Returns: The result of the iteration. Unref @elem after usage if this * Returns: The result of the iteration. Unset @elem after usage.
* is a refcounted object.
* *
* MT safe. * MT safe.
*/ */
GstIteratorResult GstIteratorResult
gst_iterator_next (GstIterator * it, gpointer * elem) gst_iterator_next (GstIterator * it, GValue * elem)
{ {
GstIteratorResult result; GstIteratorResult result;
g_return_val_if_fail (it != NULL, GST_ITERATOR_ERROR); g_return_val_if_fail (it != NULL, GST_ITERATOR_ERROR);
g_return_val_if_fail (elem != NULL, GST_ITERATOR_ERROR); g_return_val_if_fail (elem != NULL, GST_ITERATOR_ERROR);
g_return_val_if_fail (G_VALUE_TYPE (elem) == G_TYPE_INVALID
|| G_VALUE_HOLDS (elem, it->type), GST_ITERATOR_ERROR);
if (G_VALUE_TYPE (elem) == G_TYPE_INVALID)
g_value_init (elem, it->type);
restart: restart:
if (it->pushed) { if (it->pushed) {
@ -325,14 +358,16 @@ restart:
if (result == GST_ITERATOR_OK && it->item) { if (result == GST_ITERATOR_OK && it->item) {
GstIteratorItem itemres; GstIteratorItem itemres;
itemres = it->item (it, *elem); itemres = it->item (it, elem);
switch (itemres) { switch (itemres) {
case GST_ITERATOR_ITEM_SKIP: case GST_ITERATOR_ITEM_SKIP:
if (G_LIKELY (it->lock)) if (G_LIKELY (it->lock))
g_mutex_unlock (it->lock); g_mutex_unlock (it->lock);
g_value_reset (elem);
goto restart; goto restart;
case GST_ITERATOR_ITEM_END: case GST_ITERATOR_ITEM_END:
result = GST_ITERATOR_DONE; result = GST_ITERATOR_DONE;
g_value_reset (elem);
break; break;
case GST_ITERATOR_ITEM_PASS: case GST_ITERATOR_ITEM_PASS:
break; break;
@ -425,29 +460,28 @@ typedef struct _GstIteratorFilter
GstIterator *slave; GstIterator *slave;
GCompareFunc func; GCompareFunc func;
gpointer user_data; GValue user_data;
gboolean have_user_data;
} GstIteratorFilter; } GstIteratorFilter;
static GstIteratorResult static GstIteratorResult
filter_next (GstIteratorFilter * it, gpointer * elem) filter_next (GstIteratorFilter * it, GValue * elem)
{ {
GstIteratorResult result = GST_ITERATOR_ERROR; GstIteratorResult result = GST_ITERATOR_ERROR;
gboolean done = FALSE; gboolean done = FALSE;
GValue item = { 0, };
*elem = NULL;
while (G_LIKELY (!done)) { while (G_LIKELY (!done)) {
gpointer item;
result = gst_iterator_next (it->slave, &item); result = gst_iterator_next (it->slave, &item);
switch (result) { switch (result) {
case GST_ITERATOR_OK: case GST_ITERATOR_OK:
if (G_LIKELY (GST_ITERATOR (it)->lock)) if (G_LIKELY (GST_ITERATOR (it)->lock))
g_mutex_unlock (GST_ITERATOR (it)->lock); g_mutex_unlock (GST_ITERATOR (it)->lock);
if (it->func (item, it->user_data) == 0) { if (it->func (&item, &it->user_data) == 0) {
*elem = item; g_value_copy (&item, elem);
done = TRUE; done = TRUE;
} }
g_value_reset (&item);
if (G_LIKELY (GST_ITERATOR (it)->lock)) if (G_LIKELY (GST_ITERATOR (it)->lock))
g_mutex_lock (GST_ITERATOR (it)->lock); g_mutex_lock (GST_ITERATOR (it)->lock);
break; break;
@ -460,25 +494,33 @@ filter_next (GstIteratorFilter * it, gpointer * elem)
break; break;
} }
} }
g_value_unset (&item);
return result; return result;
} }
static void
filter_copy (const GstIteratorFilter * it, GstIteratorFilter * copy)
{
copy->slave = gst_iterator_copy (it->slave);
if (it->have_user_data) {
memset (&copy->user_data, 0, sizeof (copy->user_data));
g_value_init (&copy->user_data, G_VALUE_TYPE (&it->user_data));
g_value_copy (&it->user_data, &copy->user_data);
}
}
static void static void
filter_resync (GstIteratorFilter * it) filter_resync (GstIteratorFilter * it)
{ {
gst_iterator_resync (it->slave); gst_iterator_resync (it->slave);
} }
static void
filter_uninit (GstIteratorFilter * it)
{
it->slave->lock = GST_ITERATOR (it)->lock;
}
static void static void
filter_free (GstIteratorFilter * it) filter_free (GstIteratorFilter * it)
{ {
filter_uninit (it); if (it->have_user_data)
g_value_unset (&it->user_data);
gst_iterator_free (it->slave); gst_iterator_free (it->slave);
} }
@ -490,8 +532,9 @@ filter_free (GstIteratorFilter * it)
* *
* Create a new iterator from an existing iterator. The new iterator * Create a new iterator from an existing iterator. The new iterator
* will only return those elements that match the given compare function @func. * will only return those elements that match the given compare function @func.
* @func should return 0 for elements that should be included * The first parameter that is passed to @func is the #GValue of the current
* in the iterator. * iterator element and the second parameter is @user_data. @func should
* return 0 for elements that should be included in the filtered iterator.
* *
* When this iterator is freed, @it will also be freed. * When this iterator is freed, @it will also be freed.
* *
@ -500,7 +543,8 @@ filter_free (GstIteratorFilter * it)
* MT safe. * MT safe.
*/ */
GstIterator * GstIterator *
gst_iterator_filter (GstIterator * it, GCompareFunc func, gpointer user_data) gst_iterator_filter (GstIterator * it, GCompareFunc func,
const GValue * user_data)
{ {
GstIteratorFilter *result; GstIteratorFilter *result;
@ -509,13 +553,21 @@ gst_iterator_filter (GstIterator * it, GCompareFunc func, gpointer user_data)
result = (GstIteratorFilter *) gst_iterator_new (sizeof (GstIteratorFilter), result = (GstIteratorFilter *) gst_iterator_new (sizeof (GstIteratorFilter),
it->type, it->lock, it->master_cookie, it->type, it->lock, it->master_cookie,
(GstIteratorCopyFunction) filter_copy,
(GstIteratorNextFunction) filter_next, (GstIteratorNextFunction) filter_next,
(GstIteratorItemFunction) NULL, (GstIteratorItemFunction) NULL,
(GstIteratorResyncFunction) filter_resync, (GstIteratorResyncFunction) filter_resync,
(GstIteratorFreeFunction) filter_free); (GstIteratorFreeFunction) filter_free);
it->lock = NULL; it->lock = NULL;
result->func = func; result->func = func;
result->user_data = user_data; if (user_data) {
g_value_init (&result->user_data, G_VALUE_TYPE (user_data));
g_value_copy (user_data, &result->user_data);
result->have_user_data = TRUE;
} else {
result->have_user_data = FALSE;
}
result->slave = it; result->slave = it;
return GST_ITERATOR (result); return GST_ITERATOR (result);
@ -531,8 +583,7 @@ gst_iterator_filter (GstIterator * it, GCompareFunc func, gpointer user_data)
* Folds @func over the elements of @iter. That is to say, @func will be called * Folds @func over the elements of @iter. That is to say, @func will be called
* as @func (object, @ret, @user_data) for each object in @it. The normal use * as @func (object, @ret, @user_data) for each object in @it. The normal use
* of this procedure is to accumulate the results of operating on the objects in * of this procedure is to accumulate the results of operating on the objects in
* @ret. If object is a refcounted object its refcount will be increased * @ret.
* before @func is called, and it should be unrefed after use in @func.
* *
* This procedure can be used (and is used internally) to implement the * This procedure can be used (and is used internally) to implement the
* gst_iterator_foreach() and gst_iterator_find_custom() operations. * gst_iterator_foreach() and gst_iterator_find_custom() operations.
@ -553,16 +604,17 @@ GstIteratorResult
gst_iterator_fold (GstIterator * it, GstIteratorFoldFunction func, gst_iterator_fold (GstIterator * it, GstIteratorFoldFunction func,
GValue * ret, gpointer user_data) GValue * ret, gpointer user_data)
{ {
gpointer item; GValue item = { 0, };
GstIteratorResult result; GstIteratorResult result;
while (1) { while (1) {
result = gst_iterator_next (it, &item); result = gst_iterator_next (it, &item);
switch (result) { switch (result) {
case GST_ITERATOR_OK: case GST_ITERATOR_OK:
if (!func (item, ret, user_data)) if (!func (&item, ret, user_data))
goto fold_done; goto fold_done;
else
g_value_reset (&item);
break; break;
case GST_ITERATOR_RESYNC: case GST_ITERATOR_RESYNC:
case GST_ITERATOR_ERROR: case GST_ITERATOR_ERROR:
@ -573,17 +625,19 @@ gst_iterator_fold (GstIterator * it, GstIteratorFoldFunction func,
} }
fold_done: fold_done:
g_value_unset (&item);
return result; return result;
} }
typedef struct typedef struct
{ {
GFunc func; GstIteratorForeachFunction func;
gpointer user_data; gpointer user_data;
} ForeachFoldData; } ForeachFoldData;
static gboolean static gboolean
foreach_fold_func (gpointer item, GValue * unused, ForeachFoldData * data) foreach_fold_func (const GValue * item, GValue * unused, ForeachFoldData * data)
{ {
data->func (item, data->user_data); data->func (item, data->user_data);
return TRUE; return TRUE;
@ -596,9 +650,7 @@ foreach_fold_func (gpointer item, GValue * unused, ForeachFoldData * data)
* @user_data: (closure): user data passed to the function * @user_data: (closure): user data passed to the function
* *
* Iterate over all element of @it and call the given function @func for * Iterate over all element of @it and call the given function @func for
* each element. As in gst_iterator_fold(), the refcount of a refcounted * each element.
* object will be increased before @func is called, and should be unrefed
* after use.
* *
* Returns: the result call to gst_iterator_fold(). The iterator will not be * Returns: the result call to gst_iterator_fold(). The iterator will not be
* freed. * freed.
@ -606,7 +658,8 @@ foreach_fold_func (gpointer item, GValue * unused, ForeachFoldData * data)
* MT safe. * MT safe.
*/ */
GstIteratorResult GstIteratorResult
gst_iterator_foreach (GstIterator * it, GFunc func, gpointer user_data) gst_iterator_foreach (GstIterator * it, GstIteratorForeachFunction func,
gpointer user_data)
{ {
ForeachFoldData data; ForeachFoldData data;
@ -621,26 +674,22 @@ typedef struct
{ {
GCompareFunc func; GCompareFunc func;
gpointer user_data; gpointer user_data;
gboolean found;
} FindCustomFoldData; } FindCustomFoldData;
static gboolean static gboolean
find_custom_fold_func (gpointer item, GValue * ret, FindCustomFoldData * data) find_custom_fold_func (const GValue * item, GValue * ret,
FindCustomFoldData * data)
{ {
if (data->func (item, data->user_data) == 0) { if (data->func (item, data->user_data) == 0) {
g_value_set_pointer (ret, item); data->found = TRUE;
g_value_copy (item, ret);
return FALSE; return FALSE;
} else { } else {
return TRUE; return TRUE;
} }
} }
/* FIXME 0.11:
* We should store ref/unref (or copy/free) functions for the type
* in GstIterator. The unref but only if it's not a match behaviour
* of find_custom() is very bad for bindings. The ref/unref functions
* are also useful for the fold and filter cases.
*/
/** /**
* gst_iterator_find_custom: * gst_iterator_find_custom:
* @it: The #GstIterator to iterate * @it: The #GstIterator to iterate
@ -648,66 +697,81 @@ find_custom_fold_func (gpointer item, GValue * ret, FindCustomFoldData * data)
* @user_data: (closure): user data passed to the compare function * @user_data: (closure): user data passed to the compare function
* *
* Find the first element in @it that matches the compare function @func. * Find the first element in @it that matches the compare function @func.
* @func should return 0 when the element is found. As in gst_iterator_fold(), * @func should return 0 when the element is found. The first parameter
* the refcount of a refcounted object will be increased before @func is * to @func will be the current element of the iterator and the
* called, and should be unrefed after use in @func unless it is the matching * second parameter will be @user_data.
* element. * The result will be stored in @elem if a result is found.
* *
* The iterator will not be freed. * The iterator will not be freed.
* *
* This function will return NULL if an error happened to the iterator. * This function will return FALSE if an error happened to the iterator
* or if the element wasn't found.
* *
* Returns: (transfer full): The element in the iterator that matches the compare * Returns: Returns TRUE if the element was found, else FALSE.
* function or NULL when no element matched.
* *
* MT safe. * MT safe.
*/ */
gpointer gboolean
gst_iterator_find_custom (GstIterator * it, GCompareFunc func, gst_iterator_find_custom (GstIterator * it, GCompareFunc func,
gpointer user_data) GValue * elem, gpointer user_data)
{ {
GValue ret = { 0, };
GstIteratorResult res; GstIteratorResult res;
FindCustomFoldData data; FindCustomFoldData data;
g_value_init (&ret, G_TYPE_POINTER); g_return_val_if_fail (G_VALUE_TYPE (elem) == G_TYPE_INVALID
|| G_VALUE_HOLDS (elem, it->type), GST_ITERATOR_ERROR);
if (G_VALUE_TYPE (elem) == G_TYPE_INVALID)
g_value_init (elem, it->type);
data.func = func; data.func = func;
data.user_data = user_data; data.user_data = user_data;
data.found = FALSE;
do { do {
res = res =
gst_iterator_fold (it, (GstIteratorFoldFunction) find_custom_fold_func, gst_iterator_fold (it, (GstIteratorFoldFunction) find_custom_fold_func,
&ret, &data); elem, &data);
if (res == GST_ITERATOR_RESYNC) if (res == GST_ITERATOR_RESYNC)
gst_iterator_resync (it); gst_iterator_resync (it);
} while (res == GST_ITERATOR_RESYNC); } while (res == GST_ITERATOR_RESYNC);
/* no need to unset, it's just a pointer */ if (!data.found)
return g_value_get_pointer (&ret); g_value_unset (elem);
return data.found;
} }
typedef struct typedef struct
{ {
GstIterator parent; GstIterator parent;
gpointer object; GValue object;
GstCopyFunction copy;
GFreeFunc free;
gboolean visited; gboolean visited;
gboolean empty;
} GstSingleObjectIterator; } GstSingleObjectIterator;
static guint32 _single_object_dummy_cookie = 0; static guint32 _single_object_dummy_cookie = 0;
static GstIteratorResult static void
gst_single_object_iterator_iterator_next (GstSingleObjectIterator * it, gst_single_object_iterator_copy (const GstSingleObjectIterator * it,
gpointer * result) GstSingleObjectIterator * copy)
{ {
if (it->visited || !it->object) { if (!it->empty) {
*result = NULL; memset (&copy->object, 0, sizeof (copy->object));
return GST_ITERATOR_DONE; g_value_init (&copy->object, it->parent.type);
g_value_copy (&it->object, &copy->object);
} }
}
*result = it->copy (it->object); static GstIteratorResult
gst_single_object_iterator_next (GstSingleObjectIterator * it, GValue * result)
{
if (it->visited || it->empty)
return GST_ITERATOR_DONE;
g_value_copy (&it->object, result);
it->visited = TRUE; it->visited = TRUE;
return GST_ITERATOR_OK; return GST_ITERATOR_OK;
} }
@ -720,16 +784,14 @@ gst_single_object_iterator_resync (GstSingleObjectIterator * it)
static void static void
gst_single_object_iterator_free (GstSingleObjectIterator * it) gst_single_object_iterator_free (GstSingleObjectIterator * it)
{ {
if (it->object) if (!it->empty)
it->free (it->object); g_value_unset (&it->object);
} }
/** /**
* gst_iterator_new_single: * gst_iterator_new_single:
* @type: #GType of the passed object * @type: #GType of the passed object
* @object: object that this iterator should return * @object: object that this iterator should return
* @copy: Function that returns a copy of @object or increases its refcount
* @free: Function to be called for freeing @object
* *
* This #GstIterator is a convenient iterator for the common * This #GstIterator is a convenient iterator for the common
* case where a #GstIterator needs to be returned but only * case where a #GstIterator needs to be returned but only
@ -741,25 +803,27 @@ gst_single_object_iterator_free (GstSingleObjectIterator * it)
* Since: 0.10.25 * Since: 0.10.25
*/ */
GstIterator * GstIterator *
gst_iterator_new_single (GType type, gpointer object, GstCopyFunction copy, gst_iterator_new_single (GType type, const GValue * object)
GFreeFunc free)
{ {
GstSingleObjectIterator *result; GstSingleObjectIterator *result;
g_return_val_if_fail (copy != NULL, NULL);
g_return_val_if_fail (free != NULL, NULL);
result = (GstSingleObjectIterator *) result = (GstSingleObjectIterator *)
gst_iterator_new (sizeof (GstSingleObjectIterator), gst_iterator_new (sizeof (GstSingleObjectIterator),
type, NULL, &_single_object_dummy_cookie, type, NULL, &_single_object_dummy_cookie,
(GstIteratorNextFunction) gst_single_object_iterator_iterator_next, NULL, (GstIteratorCopyFunction) gst_single_object_iterator_copy,
(GstIteratorNextFunction) gst_single_object_iterator_next,
(GstIteratorItemFunction) NULL,
(GstIteratorResyncFunction) gst_single_object_iterator_resync, (GstIteratorResyncFunction) gst_single_object_iterator_resync,
(GstIteratorFreeFunction) gst_single_object_iterator_free); (GstIteratorFreeFunction) gst_single_object_iterator_free);
result->object = (object) ? copy (object) : NULL; if (object) {
result->copy = copy; g_value_init (&result->object, type);
result->free = free; g_value_copy (object, &result->object);
result->empty = FALSE;
} else {
result->empty = TRUE;
}
result->visited = FALSE; result->visited = FALSE;
return (GstIterator *) result; return GST_ITERATOR (result);
} }

View file

@ -1,5 +1,6 @@
/* GStreamer /* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com> * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
* Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
* *
* gstiterator.h: Header for GstIterator * gstiterator.h: Header for GstIterator
* *
@ -62,13 +63,30 @@ typedef enum {
} GstIteratorItem; } GstIteratorItem;
/** /**
* GstIteratorDisposeFunction: * GstIteratorCopyFunction:
* @owner: the owner of the iterator * @it: The original iterator
* @copy: The copied iterator
* *
* The function that will be called when a #GList iterator is freed. The * This function will be called when creating a copy of @it and should
* owner of the #GList iterator can then clean up its resources. * create a copy of all custom iterator fields or increase their
* reference counts.
*/ */
typedef void (*GstIteratorDisposeFunction) (gpointer owner); typedef void (*GstIteratorCopyFunction) (const GstIterator *it, GstIterator *copy);
/**
* GstIteratorItemFunction:
* @it: the iterator
* @item: the item being retrieved.
*
* The function that will be called after the next item of the iterator
* has been retrieved. This function can be used to skip items or stop
* the iterator.
*
* The function will be called with the iterator lock held.
*
* Returns: the result of the operation.
*/
typedef GstIteratorItem (*GstIteratorItemFunction) (GstIterator *it, const GValue * item);
/** /**
* GstIteratorNextFunction: * GstIteratorNextFunction:
@ -84,23 +102,7 @@ typedef void (*GstIteratorDisposeFunction) (gpointer owner);
* *
* Returns: the result of the operation. * Returns: the result of the operation.
*/ */
typedef GstIteratorResult (*GstIteratorNextFunction) (GstIterator *it, gpointer *result); typedef GstIteratorResult (*GstIteratorNextFunction) (GstIterator *it, GValue *result);
/**
* GstIteratorItemFunction:
* @it: the iterator
* @item: the item being retrieved.
*
* The function that will be called after the next item of the iterator
* has been retrieved. This function will typically increase the refcount
* of the item or make a copy.
*
* Implementors of a #GstIterator should implement this
* function and pass it to the constructor of the custom iterator.
* The function will be called with the iterator lock held.
*
* Returns: the result of the operation.
*/
typedef GstIteratorItem (*GstIteratorItemFunction) (GstIterator *it, gpointer item);
/** /**
* GstIteratorResyncFunction: * GstIteratorResyncFunction:
* @it: the iterator * @it: the iterator
@ -127,6 +129,15 @@ typedef void (*GstIteratorResyncFunction) (GstIterator *it);
*/ */
typedef void (*GstIteratorFreeFunction) (GstIterator *it); typedef void (*GstIteratorFreeFunction) (GstIterator *it);
/**
* GstIteratorForeachFunction:
* @item: The item
* @user_data: User data
*
* A function that is called by gst_iterator_foreach() for every element.
*/
typedef void (*GstIteratorForeachFunction) (const GValue * item, gpointer user_data);
/** /**
* GstIteratorFoldFunction: * GstIteratorFoldFunction:
* @item: the item to fold * @item: the item to fold
@ -137,20 +148,7 @@ typedef void (*GstIteratorFreeFunction) (GstIterator *it);
* *
* Returns: TRUE if the fold should continue, FALSE if it should stop. * Returns: TRUE if the fold should continue, FALSE if it should stop.
*/ */
typedef gboolean (*GstIteratorFoldFunction) (gpointer item, GValue *ret, gpointer user_data); typedef gboolean (*GstIteratorFoldFunction) (const GValue * item, GValue * ret, gpointer user_data);
/**
* GstCopyFunction:
* @object: The object to copy
*
* A function to create a copy of some object or
* increase its reference count.
*
* Returns: a copy of the object or the same object with increased reference count
*
* Since: 0.10.25
*/
typedef gpointer (*GstCopyFunction) (gpointer object);
/** /**
* GST_ITERATOR: * GST_ITERATOR:
@ -206,6 +204,7 @@ typedef gpointer (*GstCopyFunction) (gpointer object);
*/ */
struct _GstIterator { struct _GstIterator {
/*< protected >*/ /*< protected >*/
GstIteratorCopyFunction copy;
GstIteratorNextFunction next; GstIteratorNextFunction next;
GstIteratorItemFunction item; GstIteratorItemFunction item;
GstIteratorResyncFunction resync; GstIteratorResyncFunction resync;
@ -221,7 +220,7 @@ struct _GstIterator {
guint size; guint size;
/*< private >*/ /*< private >*/
gpointer _gst_reserved[GST_PADDING-1]; gpointer _gst_reserved[GST_PADDING];
}; };
GType gst_iterator_get_type (void); GType gst_iterator_get_type (void);
@ -231,6 +230,7 @@ GstIterator* gst_iterator_new (guint size,
GType type, GType type,
GMutex *lock, GMutex *lock,
guint32 *master_cookie, guint32 *master_cookie,
GstIteratorCopyFunction copy,
GstIteratorNextFunction next, GstIteratorNextFunction next,
GstIteratorItemFunction item, GstIteratorItemFunction item,
GstIteratorResyncFunction resync, GstIteratorResyncFunction resync,
@ -240,17 +240,16 @@ GstIterator* gst_iterator_new_list (GType type,
GMutex *lock, GMutex *lock,
guint32 *master_cookie, guint32 *master_cookie,
GList **list, GList **list,
gpointer owner, GObject * owner,
GstIteratorItemFunction item, GstIteratorItemFunction item);
GstIteratorDisposeFunction free);
GstIterator* gst_iterator_new_single (GType type, GstIterator* gst_iterator_new_single (GType type,
gpointer object, const GValue * object);
GstCopyFunction copy,
GFreeFunc free); GstIterator* gst_iterator_copy (const GstIterator *it);
/* using iterators */ /* using iterators */
GstIteratorResult gst_iterator_next (GstIterator *it, gpointer *elem); GstIteratorResult gst_iterator_next (GstIterator *it, GValue * elem);
void gst_iterator_resync (GstIterator *it); void gst_iterator_resync (GstIterator *it);
void gst_iterator_free (GstIterator *it); void gst_iterator_free (GstIterator *it);
@ -258,14 +257,14 @@ void gst_iterator_push (GstIterator *it, GstIterator *other);
/* higher-order functions that operate on iterators */ /* higher-order functions that operate on iterators */
GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func, GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func,
gpointer user_data); const GValue * user_data);
GstIteratorResult gst_iterator_fold (GstIterator *it, GstIteratorResult gst_iterator_fold (GstIterator *it,
GstIteratorFoldFunction func, GstIteratorFoldFunction func,
GValue *ret, gpointer user_data); GValue *ret, gpointer user_data);
GstIteratorResult gst_iterator_foreach (GstIterator *it, GstIteratorResult gst_iterator_foreach (GstIterator *it,
GFunc func, gpointer user_data); GstIteratorForeachFunction func, gpointer user_data);
gpointer gst_iterator_find_custom (GstIterator *it, GCompareFunc func, gboolean gst_iterator_find_custom (GstIterator *it, GCompareFunc func,
gpointer user_data); GValue *elem, gpointer user_data);
G_END_DECLS G_END_DECLS

View file

@ -1538,7 +1538,7 @@ gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
GDir *dir; GDir *dir;
guint hash = 0; guint hash = 0;
recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE); recurse_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
dir = g_dir_open (path, 0, &err); dir = g_dir_open (path, 0, &err);
if (dir == NULL) { if (dir == NULL) {
@ -1600,8 +1600,8 @@ gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
if (filenames == NULL || *filenames == NULL) if (filenames == NULL || *filenames == NULL)
filenames = empty_filenames; filenames = empty_filenames;
recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE); recurse_into_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX); partial_names = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
/* if we can construct the exact paths to check with the data we have, just /* if we can construct the exact paths to check with the data we have, just
* stat them one by one; this is more efficient than opening the directory * stat them one by one; this is more efficient than opening the directory