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

Eduardo Lima (Etrunko) eblima at linux.vnet.ibm.com
Wed Feb 1 23:59:17 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>
>>
>> Signed-off-by: Eduardo Lima (Etrunko) <eblima at br.ibm.com>
>> ---
>>  libxkutil/Makefile.am |   51 +++++++---
>>  libxkutil/list_util.c |  254 +++++++++++++++++++++++++++++++++++++++++++++++++
>>  libxkutil/list_util.h |   73 ++++++++++++++
>>  3 files changed, 364 insertions(+), 14 deletions(-)
>>  create mode 100644 libxkutil/list_util.c
>>  create mode 100644 libxkutil/list_util.h
>>
>> diff --git a/libxkutil/Makefile.am b/libxkutil/Makefile.am
>> index f1adc03..8d436ad 100644
>> --- a/libxkutil/Makefile.am
>> +++ b/libxkutil/Makefile.am
>> @@ -1,21 +1,44 @@
>>  # Copyright IBM Corp. 2007
>> +AM_CFLAGS = \
>> +	$(CFLAGS_STRICT) \
>> +	-DLIBVIRTCIM_CONF=\"@sysconfdir@/@PACKAGE at .conf\"
>>
>> -AM_CFLAGS = $(CFLAGS_STRICT) \
>> -            -DLIBVIRTCIM_CONF=\"@sysconfdir@/@PACKAGE at .conf\"
>> +noinst_HEADERS = \
>> +	cs_util.h \
>> +	misc_util.h \
>> +	device_parsing.h \
>> +	xmlgen.h \
>> +	infostore.h \
>> +	pool_parsing.h \
>> +	acl_parsing.h \
>> +	list_util.h
>>
>> -noinst_HEADERS = cs_util.h misc_util.h device_parsing.h xmlgen.h infostore.h \
>> -                 pool_parsing.h acl_parsing.h
>> +lib_LTLIBRARIES = \
>> +	libxkutil.la
>>
>> -lib_LTLIBRARIES = libxkutil.la
>> +libxkutil_la_SOURCES = \
>> +	cs_util_instance.c \
>> +	misc_util.c \
>> +	device_parsing.c \
>> +	xmlgen.c \
>> +	infostore.c \
>> +	pool_parsing.c \
>> +	acl_parsing.c \
>> +	list_util.c
>>
>> -libxkutil_la_SOURCES = cs_util_instance.c misc_util.c device_parsing.c \
>> -                       xmlgen.c infostore.c pool_parsing.c acl_parsing.c
>> -libxkutil_la_LDFLAGS = -version-info @VERSION_INFO@
>> -libxkutil_la_LIBADD = @LIBVIRT_LIBS@ \
>> -		      @LIBUUID_LIBS@
>> +libxkutil_la_LDFLAGS = \
>> +	-version-info @VERSION_INFO@
>>
>> -noinst_PROGRAMS = xml_parse_test
>> +libxkutil_la_LIBADD = \
>> +	@LIBVIRT_LIBS@ \
>> +	@LIBUUID_LIBS@
>>
>> -xml_parse_test_SOURCES = xml_parse_test.c
>> -xml_parse_test_LDADD = libxkutil.la \
>> -		       @LIBVIRT_LIBS@
>> +noinst_PROGRAMS = \
>> +	xml_parse_test
>> +
>> +xml_parse_test_SOURCES = \
>> +	xml_parse_test.c
>> +
>> +xml_parse_test_LDADD = \
>> +	libxkutil.la \
>> +	@LIBVIRT_LIBS@
>> diff --git a/libxkutil/list_util.c b/libxkutil/list_util.c
>> new file mode 100644
>> index 0000000..84b2ba0
>> --- /dev/null
>> +++ b/libxkutil/list_util.c
>> @@ -0,0 +1,254 @@
>> +/*
>> + * Copyright IBM Corp. 2012
>> + *
>> + * Authors:
>> + *  Eduardo Lima (Etrunko) <eblima at br.ibm.com>
>> + *
>> + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
>> + */
>> +#ifdef HAVE_CONFIG_H
>> +# include "config.h"
>> +#endif
>> +
>> +#include <stdlib.h>
>> +
>> +#include "list_util.h"
>> +
>> +struct _list_node_t {
>> +        list_node_t *prev;
>> +        list_node_t *next;
>> +        void *data;
>> +};
>> +
>> +struct _list_t {
>> +        unsigned int count;
>> +        list_node_t *head;
>> +        list_data_free_cb free_cb;
>> +        list_data_cmp_cb cmp_cb;
>> +};
>> +
>> +list_t *list_new(list_data_free_cb free_cb, list_data_cmp_cb cmp_cb)
>> +{
>> +        list_t *l = calloc(1, sizeof(*l));
>> +        if (l == NULL)
>> +                return NULL;
>> +
>> +        l->free_cb = free_cb;
>> +        l->cmp_cb = cmp_cb;
>> +        return l;
>> +}
>> +
>> +void list_free(list_t *list)
>> +{
>> +        list_node_t *n, *next;
>> +
>> +        if (list == NULL || list->head == NULL)
>> +                return;
>> +
>> +        n = list->head;
>> +
>> +        do {
>> +                if (list->free_cb)
>> +                        list->free_cb(n->data);
>> +
>> +                next = n->next;
>> +                free(n);
>> +                n = next;
>> +        } while (n != list->head);
>> +
>> +        free(list);
>> +}
>> +
>> +void list_append(list_t *list, void *data)
>> +{
>> +        list_node_t *n;
>> +
>> +        if (list == NULL)
>> +                return;
>> +
>> +        n = calloc(1, sizeof(*n));
>> +
>> +        if (n == NULL)
>> +                return;
>> +
>> +        n->data = data;
>> +
>> +        if (list->head == NULL) { /* empty list */
>> +                n->next = n->prev = n;
>> +                list->head = n;
> 
> shouldn't you be setting the list->count to "0" here ?
> 

Sorry, missed this comment. Yes it could be set to 0 but I think it is a
bit too zealous. There are only two cases where list->head will be NULL.

First is when the list_new() function is called. It will calloc a new
list struct which will then assign list->count to 0. The other case is
when all nodes are removed with list_remove_node(), which will decrement
the counter until 0.


-- 
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