From nobody Wed May 14 01:31:14 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 1527627641538616.6091789496231; Tue, 29 May 2018 14:00:41 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 253CDC0E724E; Tue, 29 May 2018 21:00:39 +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 DA6032010CE6; Tue, 29 May 2018 21:00:38 +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 1D7271800C9C; Tue, 29 May 2018 21:00:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4TL0ZDq023405 for ; Tue, 29 May 2018 17:00:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id D8C39308BDA2; Tue, 29 May 2018 21:00:35 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D14FA308BDA1 for ; Tue, 29 May 2018 21:00:35 +0000 (UTC) Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) (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 A17133CBA3C for ; Tue, 29 May 2018 21:00:24 +0000 (UTC) Received: by mail-pf0-f195.google.com with SMTP id a14-v6so7868034pfi.1 for ; Tue, 29 May 2018 14:00:24 -0700 (PDT) Received: from centos.localdomain ([49.32.89.162]) by smtp.gmail.com with ESMTPSA id v6-v6sm52176763pfm.120.2018.05.29.14.00.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 May 2018 14:00:23 -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=H2fnOrC9YwXdM8JadFS3aQszpXsD4XpV+485xvaCiOw=; b=gurzvVDeCkX+u/npuXweRLfE00LgKT26nRZthmEnT/3s8Ef1GPwkaeYel0sRtcqTL1 7W0DkvnzRKrMDSaWDVe8kKQL3vu1CWD4T8FatF0O45TtNL/IDcs70D0bZE7U6675Dy+V goDLiA7o8Xo8TMWZw1KI346rT14Amm20QSrf/e9TGTv6/jPEgI/ggU8o8T6LTeZL975C Mk0OoQp6TcKHAlvWhflH87c7Mhh5HEFB8hySeniq4rn92X2mSwPOYAGirBjKIG2UsYYA cedPWpnqpEV4bnD+ZsxgkCNYWwSutqLbc4vv8UoIAu0uoPmcrhsBn6mbnbBJp1+uLGDC YBAA== 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=H2fnOrC9YwXdM8JadFS3aQszpXsD4XpV+485xvaCiOw=; b=sx71TWXsSkr/xs6r3KMOYIlaNhsy6kw5R9gEUSTx3QI5oKx9Jl+3NegTA+F0lDn3JB 2hgf13ECrgs7qedBBycGyXLLvV5BuiIMToRuEESlUooMTqNlwMGmISSRsVpg28Hf4auZ HPTmekL95H0u83imsp8A3dvcAl6cDU0N6NGUDLv82EGkVUbhD3bvQBV+Jyyot4lmQGLU 3OPHFiCT2nOpQGGZRIrSq93JQiajamdj8DFoF0UndSCjjzT6gYqho/XD7ddU5EPJlF1d EP9mt82apmeGxwkaRy0UG7qRC1OB3CB4lkqFvRwjCPa3uIHuTCL6NrQGZGQueuyXrCQV eWyQ== X-Gm-Message-State: ALKqPwcOdF5sHsiIvUf7qMyd9qTg9Sdhzk9qDXfja8zLE0NrJL5z79E1 eYPlgfd7PTG7rzes3QzSAwavbAsP X-Google-Smtp-Source: ADUXVKLQUaMd5qc/AB4LLMy2siM5zwJu02BcbyWCasddRCckI4Xk7KKgYlKUmNrb/ZEuWGyiUVb3+w== X-Received: by 2002:a63:6501:: with SMTP id z1-v6mr9096pgb.452.1527627623914; Tue, 29 May 2018 14:00:23 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Wed, 30 May 2018 02:30:04 +0530 Message-Id: <1527627607-15698-2-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1527627607-15698-1-git-send-email-skrtbhtngr@gmail.com> References: <1527627607-15698-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.29]); Tue, 29 May 2018 21:00:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 29 May 2018 21:00:24 +0000 (UTC) for IP:'209.85.192.195' DOMAIN:'mail-pf0-f195.google.com' HELO:'mail-pf0-f195.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.396 (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.195 mail-pf0-f195.google.com 209.85.192.195 mail-pf0-f195.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-loop: libvir-list@redhat.com Subject: [libvirt] [RFC 1/4] 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.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 29 May 2018 21:00:40 +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 cleanup attribute to variable declarations. Signed-off-by: Sukrit Bhatnagar --- src/util/viralloc.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 69 insertions(+) diff --git a/src/util/viralloc.h b/src/util/viralloc.h index 69d0f90..e1c31c3 100644 --- a/src/util/viralloc.h +++ b/src/util/viralloc.h @@ -596,4 +596,73 @@ 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 variables(s) to free automatically + * @func: cleanup function to be automatically called + * + * Thos macro defines a function for automatically freeing + * resources allocated to a variable of type @type. The newly + * defined function calls the corresponding pre-defined + * function @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 variables(s) to free automatically + * @func: cleanup function to be automatically called + * + * This macro defines a function for automatically clearing + * a variable of type @type. The newly deifined function calls + * the corresponding pre-defined function @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 variables(s) to free automatically + * + * Macro to automatically free the memory allocated to + * the variable(s) 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 variables(s) to free automatically + * + * Macro to automatically free the memory allocated to + * the variable(s) 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 variables(s) to free automatically + * + * Macro to automatically clear the variable(s) declared + * with it by calling the function defined by + * VIR_DEFINE_AUTOCLEAR_FUNC when the variabl* 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