From nobody Tue May 13 22:20:03 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 1528400112771988.1855175968517; Thu, 7 Jun 2018 12:35:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 41B3DC04BD25; Thu, 7 Jun 2018 19:35: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 12A063001A69; Thu, 7 Jun 2018 19:35:11 +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 B23C81800FC1; Thu, 7 Jun 2018 19:35:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w57JZ6RY018879 for ; Thu, 7 Jun 2018 15:35:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id E2BB11001647; Thu, 7 Jun 2018 19:35:06 +0000 (UTC) Received: from mx1.redhat.com (ext-mx17.extmail.prod.ext.phx2.redhat.com [10.5.110.46]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D86E81001646 for ; Thu, 7 Jun 2018 19:35:05 +0000 (UTC) Received: from mail-pf0-f172.google.com (mail-pf0-f172.google.com [209.85.192.172]) (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 0CC2130C10E6 for ; Thu, 7 Jun 2018 19:34:54 +0000 (UTC) Received: by mail-pf0-f172.google.com with SMTP id y5-v6so4490072pfn.4 for ; Thu, 07 Jun 2018 12:34:54 -0700 (PDT) Received: from centos.localdomain ([49.32.106.159]) by smtp.gmail.com with ESMTPSA id d23-v6sm86686407pfn.3.2018.06.07.12.34.50 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jun 2018 12:34:52 -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=5rsqeDUw9zBlRzrg/IAbyv1MJVR4LCifzCnt2ns8OEQ=; b=sst1CKgJWQFPuaOpVwfo/YAKcI6Da0seyrzHrH4/L0K9nHiyI5ZOtgtLwhnikZ2jCH TNdld3z5T6SA23xP/UlpE/qCx8TxW4b8juBrGM5Ec0418wAX8uku4tujZ/VB23XEfx9L 0nRhzfeGS/XHMwVetON+aNf1vEeYTB/5BLfD9u0Ac75B/A85wI0Hx6YL4JxonXruXUfG uw49Ot+NEsl1u0CryH8mkExnCPon/rkS1A/LDHr0FT67PtyaEt7O4phAaixO2Or8JnxX Hi13qvP+aeHHhoVpcZvxeK9G81UQqzA9X1n0DGyQDNy1j6oD1QWUetLODLZhQguRDtsS zl8Q== 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=5rsqeDUw9zBlRzrg/IAbyv1MJVR4LCifzCnt2ns8OEQ=; b=gY2tOzkEr08CU4gZFQCJu2CVOrP101sOiELGc6rnj+QY1gPH+QCBunWFWdzzfFcLr3 3KPOrdr99BaIZeDZ/qOKPGfcNuC0JudpAzST2LTV/4wP0wi2AszMuI1m9qmbmH63OGjx vfSQZsK7fAF/M/VX5XVr9GQ58MNRhb5cIlyAArEbIdx2J31CeYNfIRpPcgKlbqS7EDCj NAOeES8Z925Oy5F1hI9+fpQmM+AhHDPhNSSzdgksXHlLe/Yt6s9ADU4eWHn/8q/BmWAj 1bWPu4tEcGcv4FfTRJtmg4tQQH3ABSh13cDPvT1vvgI4GTuimODEfFzOuwIPP2RdjBjN VCzQ== X-Gm-Message-State: APt69E18bLD/Fi6hpPfKrqATlR++9GXZ+esIVUpTgigM0C1gIZtB9Sue S0emhoqaMlztSkq7tTjFP5iWQPeg X-Google-Smtp-Source: ADUXVKJVrRBGpC+k3i7txecMhtVgAaV8appJjc+az2NtS47Lm7JstU9lByYzObkWsyEaOggXlwshzg== X-Received: by 2002:a62:62c2:: with SMTP id w185-v6mr2975090pfb.78.1528400093197; Thu, 07 Jun 2018 12:34:53 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Fri, 8 Jun 2018 01:04:23 +0530 Message-Id: <1528400083-6788-2-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1528400083-6788-1-git-send-email-skrtbhtngr@gmail.com> References: <1528400083-6788-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.46]); Thu, 07 Jun 2018 19:34:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Thu, 07 Jun 2018 19:34:54 +0000 (UTC) for IP:'209.85.192.172' DOMAIN:'mail-pf0-f172.google.com' HELO:'mail-pf0-f172.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.511 (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS, T_DKIM_INVALID) 209.85.192.172 mail-pf0-f172.google.com 209.85.192.172 mail-pf0-f172.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.46 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v1 01/21] 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.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 07 Jun 2018 19:35: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 added to src/util/viralloc.h which help in adding GCC's cleanup attribute to variable declarations. --- src/util/viralloc.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 68 insertions(+) diff --git a/src/util/viralloc.h b/src/util/viralloc.h index 69d0f90..5804ca9 100644 --- a/src/util/viralloc.h +++ b/src/util/viralloc.h @@ -596,4 +596,72 @@ void virAllocTestInit(void); int virAllocTestCount(void); void virAllocTestOOM(int n, int m); void virAllocTestHook(void (*func)(int, void*), void *data); + +# define VIR_AUTOPTR_FUNC_NAME(type) virAutoPtr##type +# define VIR_AUTOCLEAR_FUNC_NAME(type) virAutoClear##type + +/** + * 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) \ + static inline void VIR_AUTOPTR_FUNC_NAME(type)(type *_ptr) \ + { \ + (func)(*_ptr); \ + } \ + +/** + * VIR_DEFINE_AUTOCLEAR_FUNC: + * @type: type of the variable to be freed automatically + * @func: cleanup function to be automatically called + * + * This macro defines a function for automatic clearing of + * a variable of type @type. This newly defined function + * works as a necessary wrapper around @func. + */ +# define VIR_DEFINE_AUTOCLEAR_FUNC(type, func) \ + static inline void VIR_AUTOCLEAR_FUNC_NAME(type)(type *_ptr) \ + { \ + (func)(_ptr); \ + } \ + +/** + * 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)))) type + +/** + * VIR_AUTOCLEAR: + * @type: type of the variable to be freed automatically + * + * Macro to automatically clear the variable declared + * with it by calling the function defined by + * VIR_DEFINE_AUTOCLEAR_FUNC when the variable goes out + * of scope. + */ +# define VIR_AUTOCLEAR(type) \ + __attribute__((cleanup(VIR_AUTOCLEAR_FUNC_NAME(type)))) 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