From patchwork Fri Sep 13 15:35:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11144951 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 055AF14E5 for ; Fri, 13 Sep 2019 15:38:09 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D50CE20717 for ; Fri, 13 Sep 2019 15:38:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bCZJe4pM" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D50CE20717 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i8ncY-00028c-DY; Fri, 13 Sep 2019 15:36:26 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i8ncX-00028I-JI for xen-devel@lists.xenproject.org; Fri, 13 Sep 2019 15:36:25 +0000 X-Inumbo-ID: 322d91fc-d63c-11e9-978d-bc764e2007e4 Received: from mail-lj1-x244.google.com (unknown [2a00:1450:4864:20::244]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 322d91fc-d63c-11e9-978d-bc764e2007e4; Fri, 13 Sep 2019 15:36:04 +0000 (UTC) Received: by mail-lj1-x244.google.com with SMTP id h2so21033104ljk.1 for ; Fri, 13 Sep 2019 08:36:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JiZ5P+lF28hqjSEly5GNv4UGoQtY/Pu7vhNcliJxRi8=; b=bCZJe4pMMv3hl6r7iW0+Wlt9jT/n6cT+CxOCFy0YDcL0q9waze1HlfcfDv/4ImtPS+ FUBXC9t2vKML11LDHff/j7hcUwLWdpJua8MAvoLLgrIN5kwhv3dEY9b/Y6znvYig+WUm OQ+1agAyiDrbyVLvg57qKV3/zxzV67B2xM1NDUeo4JgQW0jYsm/1KQ1ecg6M/m5wvjdZ uBRKCYfDq79mOKLf6M5jH/tWcFcAXR6i8XmaHFsmYJHXTu2/uYlwB7SCpY8YIlgei2Kh WhB0pMa5rPFWEfVZ09XlS/jC0CDuvN0KxeeEXmEywcaBYboAol6qiikI3kKv+i5d28se 42mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JiZ5P+lF28hqjSEly5GNv4UGoQtY/Pu7vhNcliJxRi8=; b=bV8+a+VBoTc9DMSR/6xo+P7Wsk4wad2s1LkXAP6LxC9mkl1vVJdqhW6qecR35N7qbd 2XAoHB0qqF3HLxW22ZXntuE7bmBi8HMXFckAeRN6f+oFxYTVC8ngpDHojB/RN3mdVb4Y ptgzwy70+ZIOol4/PMNNU4fKVtRdm/dpiWh1joToGZvt619GZ/3TmAHenf0+KXLzlvxi pTF4B5MmfnTapX+VWb7hB9Ova7M36ve09bKqWnuTSo76ScrjAvGWzPTfx8odP7SJBqO3 /ihYaQ7eRfgMRdSYqxRv15kcio+YYL+2R7DTlW6c6nwEDIPK718xOeMZpmMOQRr8MPR9 +HZA== X-Gm-Message-State: APjAAAWay5RGXL5Q0WxzlVuZO2knf4JST2ReKjtS5At899OoJGneVfxi 4QWfpbHn7Bx91ONqDWAMxilO6ERalPo= X-Google-Smtp-Source: APXvYqzC4cJ4lY+I3sCNAs4M75TO7a6llfjKfPdvLq+yDtTTmfCY8yT1ShsfYVpnqHQc2RObXbN7bw== X-Received: by 2002:a2e:a168:: with SMTP id u8mr30398304ljl.170.1568388963428; Fri, 13 Sep 2019 08:36:03 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-74.141.223.85.sovam.net.ua. [85.223.141.74]) by smtp.gmail.com with ESMTPSA id c16sm6969765lfj.8.2019.09.13.08.36.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 13 Sep 2019 08:36:02 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Fri, 13 Sep 2019 18:35:14 +0300 Message-Id: <1568388917-7287-6-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1568388917-7287-1-git-send-email-olekstysh@gmail.com> References: <1568388917-7287-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V4 5/8] xen/common: Introduce xrealloc_flex_struct() helper macros X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: sstabellini@kernel.org, Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Oleksandr Tyshchenko , julien.grall@arm.com, Paul Durrant , Jan Beulich , Volodymyr_Babchuk@epam.com MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Oleksandr Tyshchenko 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 an array with a single element: struct arrlen { size_t len; int data[1]; }; We can use the proposed macros in the following way: new_ptr = realloc_flex_struct(old_ptr, data, nr_elem); Subsequent patch will use this macros. Also, while here, introduce xmalloc_flex_struct() to allocate space for a structure with a flexible array of typed objects. Suggested-by: Volodymyr Babchuk Signed-off-by: Oleksandr Tyshchenko CC: Andrew Cooper CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Julien Grall CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Tim Deegan CC: Wei Liu CC: Paul Durrant Reviewed-by: Jan Beulich --- 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 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xen/include/xen/xmalloc.h b/xen/include/xen/xmalloc.h index 831152f..f0736ce 100644 --- a/xen/include/xen/xmalloc.h +++ b/xen/include/xen/xmalloc.h @@ -35,6 +35,15 @@ #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 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)