[Libvirt-cim] [PATCH 06/10] libxkutil: Use virConnectListAllDomains() to fetch domains

Wenchao Xia xiawenc at linux.vnet.ibm.com
Fri Mar 15 08:20:45 UTC 2013


于 2013-3-15 6:55, John Ferlan 写道:
> This is an optimization over using the multistep approach to get a count,
> get some memory, and get the list of domains (active and defined). Followed
> other examples to ensure only building the code if the libvirt version is
> correct. The API was added in 0.9.13.
> ---
>   libxkutil/cs_util_instance.c | 23 +++++++++++++++++++++++
>   1 file changed, 23 insertions(+)
>
> diff --git a/libxkutil/cs_util_instance.c b/libxkutil/cs_util_instance.c
> index a383147..e34c05d 100644
> --- a/libxkutil/cs_util_instance.c
> +++ b/libxkutil/cs_util_instance.c
> @@ -34,6 +34,28 @@
>   #include <libcmpiutil/libcmpiutil.h>
>
>   int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
> +#if LIBVIR_VERSION_NUMBER >= 9013
> +{
> +        virDomainPtr *nameList = NULL;
> +        int n_names;
> +        int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE |
> +                    VIR_CONNECT_LIST_DOMAINS_INACTIVE;
> +
> +        n_names = virConnectListAllDomains(conn,
> +                                           &nameList,
> +                                           flags);
> +        if (n_names > 0) {
> +                *_list = nameList;
> +        } else if (n_names == 0) {
> +                /* Since there are no elements, no domain ptrs to free
> +                 * but still must free the nameList returned
> +                 */
> +                free(nameList);
> +        }
> +
> +        return n_names;
> +}
> +#else
>   {
>           char **names = NULL;
>           int n_names;
> @@ -113,6 +135,7 @@ int get_domain_list(virConnectPtr conn, virDomainPtr **_list)
>
>           return idx;
>   }
> +#endif /* LIBVIR_VERSION_NUMBER >= 0913 */
>
>   void set_instance_class_name(CMPIInstance *instance, char *name)
>   {
>

  Code seems good, +1. Please reduce version condition macro in function
as much as possible, if there are more functions in same file, I think
two mirrored functions in two condition will make code easier to read
in future:

#if LIBVIRT_VERSION_NUMER >= XXX
void function1(void)
{
}
void function2(void)
{
}
#else
void function1(void)
{
}
void function2(void)
{
}
#endif

-- 
Best Regards

Wenchao Xia




More information about the Libvirt-cim mailing list