From nobody Tue May 13 18:24:18 2025 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; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153048145270559.57173838187259; Sun, 1 Jul 2018 14:44:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 26E9330E684F; Sun, 1 Jul 2018 21:44:11 +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 BF6E017CC8; Sun, 1 Jul 2018 21:44:10 +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 5EAAF18037ED; Sun, 1 Jul 2018 21:44:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w61Li6kl009008 for ; Sun, 1 Jul 2018 17:44:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id EDDD04B7; Sun, 1 Jul 2018 21:44:06 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E3B1F5C541 for ; Sun, 1 Jul 2018 21:44:04 +0000 (UTC) Received: from mail-pg0-f66.google.com (mail-pg0-f66.google.com [74.125.83.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B6EEB87623 for ; Sun, 1 Jul 2018 21:43:51 +0000 (UTC) Received: by mail-pg0-f66.google.com with SMTP id i7-v6so6209772pgp.2 for ; Sun, 01 Jul 2018 14:43:51 -0700 (PDT) Received: from centos.localdomain ([157.37.164.55]) by smtp.gmail.com with ESMTPSA id v68-v6sm15006933pfv.143.2018.07.01.14.43.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Jul 2018 14:43:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=m2YZK9zMymqrh8B/U/U/iI9+/hISBuxIBgBHbSYgPt4=; b=ei2pTXf35CvbXcdB7OKO16kmSIa78H/yoo14EyvKBB2IHQtDibiKQpfNfUt/CEBw8G +HXdfIzd08guk44KZpYNLiRns/mBrnVqkPANPUvJwbzm+E5swxluh81jdTR8k8R8+pTk rDpchJy7GdDyES6tDHR0FkSikUKL8+FvNtBrn9bJ795ayvvO8wEPv1gFqFoIKBd4TjNv gryhal+VG5SSh8tfxa93c2RJGbdcKw6ngHr4AVfIWExziL7Kktp9CjeuCLo+PtDICw4f 3V4mog2O4amac5FrOjBPxJNFfji5tV8BIBW4livtNYodZd8YzG7Eb60N/TeIfJ4DKD7u Xy/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=m2YZK9zMymqrh8B/U/U/iI9+/hISBuxIBgBHbSYgPt4=; b=njtkQUnL0NYfVo9WhFqbvuaTSpOZl3AEomdkIz5dZorkngE1DoUBIUU3FDv2/bhPbF 3Mzuaa691KcOsc4ayzJs1wCfP3Pf7CKVeu7n9ylLxCUTOPJnfTWQhCVomSe5xsK90utk wELGK5tWW7ENZ0uAcxkeUXcDxWRDsPgaQOZuAMIMKrUFgFMm8tZyYQGf00Y3hbFPdPIE WDgqW6eHMWHuwvyTzh6uSVrMEUgaQzYeOZh48CnP+FOYiOSQOEaSeiTYVUSyHDN21vkD ouoM9jlv10QhjmKZvb0xKMNY41RHeyYIDw1E/I7l0xRxMnc69OicZ35XU64xtx2q8Zb1 ufwA== X-Gm-Message-State: APt69E0i1E9VR12F1C9CzF9a53YmDfSolSKwnQhhYbbSmNe/AOtnyNHY OKpeNWoWC/rB4GBsqIUeL6hujtZ9 X-Google-Smtp-Source: AAOMgpcP1lpR4YD8x1JxuadI3r0lZuR7OqmPjY9MCBQxu6P889IeeM9QbnZu/UmZcU9T32Z9wioLdg== X-Received: by 2002:a62:4b48:: with SMTP id y69-v6mr11596293pfa.93.1530481430901; Sun, 01 Jul 2018 14:43:50 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Sat, 30 Jun 2018 14:30:05 +0530 Message-Id: <1530349239-22049-2-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1530349239-22049-1-git-send-email-skrtbhtngr@gmail.com> References: <1530349239-22049-1-git-send-email-skrtbhtngr@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Sun, 01 Jul 2018 21:43:51 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Sun, 01 Jul 2018 21:43:51 +0000 (UTC) for IP:'74.125.83.66' DOMAIN:'mail-pg0-f66.google.com' HELO:'mail-pg0-f66.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.766 * (DATE_IN_PAST_24_48, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_DKIM_INVALID) 74.125.83.66 mail-pg0-f66.google.com 74.125.83.66 mail-pg0-f66.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 01/35] util: alloc: add macros for implementing automatic cleanup functionality 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: , MIME-Version: 1.0 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Sun, 01 Jul 2018 21:44:11 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" New macros are introduced which help in adding GNU C's cleanup attribute to variable declarations. Variables declared with these macros will have their allocated memory freed automatically when they go out of scope. Signed-off-by: Sukrit Bhatnagar --- src/util/viralloc.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/util/viralloc.h b/src/util/viralloc.h index 69d0f90..5c1d0d5 100644 --- a/src/util/viralloc.h +++ b/src/util/viralloc.h @@ -596,4 +596,48 @@ void virAllocTestInit(void); int virAllocTestCount(void); void virAllocTestOOM(int n, int m); void virAllocTestHook(void (*func)(int, void*), void *data); + +# define VIR_AUTOPTR_TYPE_NAME(type) type##AutoPtr +# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree + +/** + * VIR_DEFINE_AUTOPTR_FUNC: + * @type: type of the variable to be freed automatically + * @func: cleanup function to be automatically called + * + * This macro defines a function for automatic freeing of + * resources allocated to a variable of type @type. This newly + * defined function works as a necessary wrapper around @func. + */ +# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \ + typedef type *VIR_AUTOPTR_TYPE_NAME(type); \ + static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \ + { \ + if (*_ptr) \ + (func)(*_ptr); \ + *_ptr =3D NULL; \ + } \ + +/** + * VIR_AUTOFREE: + * @type: type of the variable to be freed automatically + * + * Macro to automatically free the memory allocated to + * the variable declared with it by calling virFree + * when the variable goes out of scope. + */ +# define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type + +/** + * VIR_AUTOPTR: + * @type: type of the variable to be freed automatically + * + * Macro to automatically free the memory allocated to + * the variable declared with it by calling the function + * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable + * goes out of scope. + */ +# define VIR_AUTOPTR(type) \ + __attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) VIR_AUTOPTR_TYPE= _NAME(type) + #endif /* __VIR_MEMORY_H_ */ --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list