From nobody Mon Feb 9 09:16:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550851501081365.84943067284144; Fri, 22 Feb 2019 08:05:01 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 25974307FBCB; Fri, 22 Feb 2019 16:04:59 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D61EF6017E; Fri, 22 Feb 2019 16:04:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7F9D7181A00D; Fri, 22 Feb 2019 16:04:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x1MG4kAI015187 for ; Fri, 22 Feb 2019 11:04:46 -0500 Received: by smtp.corp.redhat.com (Postfix) id 5721A5D9D5; Fri, 22 Feb 2019 16:04:46 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id D0DB45D9CA for ; Fri, 22 Feb 2019 16:04:45 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Fri, 22 Feb 2019 17:04:38 +0100 Message-Id: <2aefd47b2267bd76720c7e24e2ae440e428ecdac.1550851418.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/4] util: alloc: Introduce macro for self-freeing NULL-terminated lists X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Fri, 22 Feb 2019 16:04:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" VIR_AUTOLISTPTR allows you to define a pointer to a NULL-terminated list of elements which will be auto-freed when destroying the scope. This is done so that we can avoid using VIR_AUTOPTR in those cases as it worked only for virStringList-related stuff. Signed-off-by: Peter Krempa --- src/util/viralloc.h | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/util/viralloc.h b/src/util/viralloc.h index 50a07d4fa3..983a6e83d1 100644 --- a/src/util/viralloc.h +++ b/src/util/viralloc.h @@ -631,6 +631,54 @@ void virAllocTestHook(void (*func)(int, void*), void *= data); (func)(_ptr); \ } + +# define VIR_AUTOLISTPTR_FUNC_NAME(type) type##AutoListPtrFree + +/** + * VIR_DEFINE_AUTOLISTPTR_FUNC: + * @type: type of the element of the list + * @func: freeing function for a single element of the list + * + * This macro defines the freeing function used by VIR_AUTOLISTPTR for @ty= pe. + * @func must free one of the elements of @type. + * + * Note that the function is not inline due to size and thus + * VIR_DECLARE_AUTOLISTPTR_FUNC needs to be used in the appropriate header= file. + * + * VIR_DEFINE_AUTOLISTPTR_FUNC is mutually exclusive with + * VIR_DEFINE_AUTOLISTPTR_FUNC_DIRECT. + */ +# define VIR_DECLARE_AUTOLISTPTR_FUNC(type) \ + void VIR_AUTOLISTPTR_FUNC_NAME(type)(type **_ptr); +# define VIR_DEFINE_AUTOLISTPTR_FUNC(type, func) \ + void VIR_AUTOLISTPTR_FUNC_NAME(type)(type **_ptr) \ + { \ + type *n;\ + for (n =3D *_ptr; n && *n; n++) \ + (func)(n); \ + VIR_FREE(*_ptr);\ + } + +/** + * VIR_DEFINE_AUTOLISTPTR_FUNC_DIRECT: + * @type: type of the element of the list + * @func: freeing function for the whole NULL-terminated list of @type + * + * This macro defines the freeing function used by VIR_AUTOLISTPTR for @ty= pe. + * @func must free a NULL terminated dense list of @type and also the list + * itself. This is a convenience option if @type has already such function. + * + * VIR_DEFINE_AUTOLISTPTR_FUNC_DIRECT is mutually exclusive with + * VIR_DEFINE_AUTOLISTPTR_FUNC. + */ +# define VIR_DEFINE_AUTOLISTPTR_FUNC_DIRECT(type, func) \ + static inline void VIR_AUTOLISTPTR_FUNC_NAME(type)(type **_ptr) \ + { \ + if (*_ptr) \ + (func)(*_ptr); \ + } + + /** * VIR_AUTOFREE: * @type: type of the variable to be freed automatically @@ -665,6 +713,19 @@ void virAllocTestHook(void (*func)(int, void*), void *= data); # define VIR_AUTOCLEAN(type) \ __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type +/** + * VIR_AUTOLISTPTR: + * @type: type of the members of the self-freeing NULL-terminated list to = be defined + * + * This macro defines a pointer to a NULL-terminated list of @type members= . The + * list is automatically freed when it goes out of scope including elements + * themselves. + * + * The freeing function is registered by VIR_DEFINE_AUTOLISTPTR_FUNC or + * VIR_DEFINE_AUTOLISTPTR_FUNC_DIRECT macro for the given type. + */ +# define VIR_AUTOLISTPTR(type) \ + __attribute__((cleanup(VIR_AUTOLISTPTR_FUNC_NAME(type)))) type * /** * VIR_AUTOUNREF: --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list