All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleksandr Tyshchenko <olekstysh@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: sstabellini@kernel.org, Wei Liu <wl@xen.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	George Dunlap <George.Dunlap@eu.citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>, Tim Deegan <tim@xen.org>,
	Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>,
	julien.grall@arm.com, Jan Beulich <jbeulich@suse.com>,
	volodymyr_babchuk@epam.com
Subject: [Xen-devel] [PATCH V5 4/8] xen/common: Introduce xrealloc_flex_struct() helper macros
Date: Tue, 24 Sep 2019 18:30:23 +0300	[thread overview]
Message-ID: <1569339027-19484-5-git-send-email-olekstysh@gmail.com> (raw)
In-Reply-To: <1569339027-19484-1-git-send-email-olekstysh@gmail.com>

From: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>

This patch introduces type-safe helper macros to re-allocate space
for a structure with a flexible array of typed objects.

For example, if we need to re-size the "data" array:

   struct arrlen
   {
      size_t len;
      int data[];
   };

We can use the proposed macros in the following way:

   new_ptr = realloc_flex_struct(old_ptr, data, nr_elem);

where nr_elem is the desired number of elements.

Subsequent patch will use this macros.

Also, while here, introduce xmalloc(xzalloc)_flex_struct() to
allocate space for a structure with a flexible array of typed objects.

Suggested-by: Volodymyr Babchuk <Volodymyr_Babchuk@epam.com>
Signed-off-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: George Dunlap <George.Dunlap@eu.citrix.com>
CC: Ian Jackson <ian.jackson@eu.citrix.com>
CC: Julien Grall <julien.grall@arm.com>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Tim Deegan <tim@xen.org>
CC: Wei Liu <wl@xen.org>

---
Changes V4 -> V5:
    - clarified patch description (data[] instead of data[1])
    - introduced xzalloc_flex_struct()
    - added Jan's R-b
    - added missing parentheses around the entire constructs

Changes V3 -> V4:
    - clarified patch description
    - modified to not use leading underscores
    - removed unnecessary pair of outermost parentheses
    - modified to use "nr" instead of "len"
    - placed xmalloc_flex_struct before xrealloc_flex_struct
    - simplified xrealloc_flex_struct macros
---
 xen/include/xen/xmalloc.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h
index 831152f..f515cee 100644
--- a/xen/include/xen/xmalloc.h
+++ b/xen/include/xen/xmalloc.h
@@ -35,6 +35,18 @@
 #define xzalloc_array(_type, _num) \
     ((_type *)_xzalloc_array(sizeof(_type), __alignof__(_type), _num))
 
+/* Allocate space for a structure with a flexible array of typed objects. */
+#define xzalloc_flex_struct(type, field, nr) \
+    ((type *)_xzalloc(offsetof(type, field[nr]), __alignof__(type)))
+
+#define xmalloc_flex_struct(type, field, nr) \
+    ((type *)_xmalloc(offsetof(type, field[nr]), __alignof__(type)))
+
+/* Re-allocate space for a structure with a flexible array of typed objects. */
+#define xrealloc_flex_struct(ptr, field, nr)                           \
+    ((typeof(ptr))_xrealloc(ptr, offsetof(typeof(*(ptr)), field[nr]),  \
+                            __alignof__(typeof(*(ptr)))))
+
 /* Allocate untyped storage. */
 #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES)
 #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)
-- 
2.7.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-09-24 15:31 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-24 15:30 [Xen-devel] [PATCH V5 0/8] iommu/arm: Add Renesas IPMMU-VMSA support + Linux's iommu_fwspec Oleksandr Tyshchenko
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 1/8] iommu/arm: Add iommu_helpers.c file to keep common for IOMMUs stuff Oleksandr Tyshchenko
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 2/8] iommu/arm: Add ability to handle deferred probing request Oleksandr Tyshchenko
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 3/8] xen/common: Introduce _xrealloc function Oleksandr Tyshchenko
2019-09-24 15:51   ` Jan Beulich
2019-09-24 17:14     ` Oleksandr
2019-09-24 15:30 ` Oleksandr Tyshchenko [this message]
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 5/8] iommu/arm: Add lightweight iommu_fwspec support Oleksandr Tyshchenko
2019-09-24 15:42   ` Julien Grall
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 6/8] iommu: Order the headers alphabetically in device_tree.c Oleksandr Tyshchenko
2019-09-24 15:43   ` Julien Grall
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 7/8] iommu/arm: Introduce iommu_add_dt_device API Oleksandr Tyshchenko
2019-09-24 15:57   ` Julien Grall
2019-09-24 16:22     ` Oleksandr
2019-09-24 17:21       ` Julien Grall
2019-09-24 17:30         ` Oleksandr
2019-09-24 15:30 ` [Xen-devel] [PATCH V5 8/8] iommu/arm: Add Renesas IPMMU-VMSA support Oleksandr Tyshchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1569339027-19484-5-git-send-email-olekstysh@gmail.com \
    --to=olekstysh@gmail.com \
    --cc=George.Dunlap@eu.citrix.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien.grall@arm.com \
    --cc=konrad.wilk@oracle.com \
    --cc=oleksandr_tyshchenko@epam.com \
    --cc=sstabellini@kernel.org \
    --cc=tim@xen.org \
    --cc=volodymyr_babchuk@epam.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.