[Libvirt-cim] [PATCH 1/3] libxkutil: Linked list helper

Eduardo Lima (Etrunko) eblima at linux.vnet.ibm.com
Wed Feb 1 23:46:51 UTC 2012


On 02/01/2012 08:11 PM, Sharad Mishra wrote:
> On Tue, 2012-01-31 at 19:58 -0200, Eduardo Lima (Etrunko) wrote:
>> From: "Eduardo Lima (Etrunko)" <eblima at br.ibm.com>
>>
[snip]

>> +
>> +bool list_foreach(list_t *list, list_foreach_cb cb, void *user_data)
> 
> What is the idea of this function. Isn't list_find_node doing something
> very similar?
> 

The idea here is to execute a function callback for each node of the
list without needing to know which is the first node and the condition
to stop walking he list.

Of course the user can actually do all this manually if necessary, with
functions like list_first_node() list_last_node(), list_node_next() and
list_node_prev().

In the ACL patch there are cases of use of both list_foreach() and the
manual process. The disadvantage of using foreach is that you need to
have and agreement between the caller of the user_data parameter.

This is similar to glib list/slist implementation.

>> +{
>> +        list_node_t *node;
>> +
>> +        if (list == NULL || list->head == NULL)
>> +                return true; /* nothing to do */
>> +
>> +        node = list->head;
>> +        do {
>> +                if (cb(node->data, user_data) == false)
>> +                        return false;
>> +
>> +                node = node->next;
>> +        } while (node != list->head);
>> +
>> +        return true;
>> +}
>> +

-- 
Eduardo de Barros Lima
Software Engineer, Open Virtualization
Linux Technology Center - IBM/Brazil
eblima at br.ibm.com




More information about the Libvirt-cim mailing list