From patchwork Tue Aug 20 18:09:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11104469 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 039C1112C for ; Tue, 20 Aug 2019 18:12: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 D2A2B20644 for ; Tue, 20 Aug 2019 18:12: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="Jrb2Dpaz" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D2A2B20644 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 1i08aP-000664-D1; Tue, 20 Aug 2019 18:10:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i08aO-00065T-1E for xen-devel@lists.xenproject.org; Tue, 20 Aug 2019 18:10:24 +0000 X-Inumbo-ID: c06f55ba-c375-11e9-92eb-bc764e2007e4 Received: from mail-lf1-x144.google.com (unknown [2a00:1450:4864:20::144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c06f55ba-c375-11e9-92eb-bc764e2007e4; Tue, 20 Aug 2019 18:10:12 +0000 (UTC) Received: by mail-lf1-x144.google.com with SMTP id h28so4835786lfj.5 for ; Tue, 20 Aug 2019 11:10:12 -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=kZfdOq/+AGxkg2VwkO+D5NSs5HyNMq9JZ1kdANT+Bnk=; b=Jrb2DpazIWO88b8zrS0b/B+vEYrMcfRKrSJxbA/qKv3hcCTLsaqT7oTeNsfM9sZ1qe NeTMVoDfXZpm36IC3H721epT3qwE78+SfF/bDAR70TpsY8iZ/E0Ixfv0Q43bvT44kWej 0TJVFMM75DzM5KkCx/hVNJsS6YdirrI+I0dlrh/AKS5jwIr5UxXKrBnWyaEfELmcf+pR dn1Xx8222XwSuoCYv+uOuwRA3JWbOm6GRcvcMXy92Q3qmodmbHobWwIBuR4F0uyrVQNi 1G3quZaaBYDeQyEbXFF9mJ0I6aKdcobuVWPkD3IkJjcJA2508zE2XHYLwgi2udx/m9HX W/EQ== 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=kZfdOq/+AGxkg2VwkO+D5NSs5HyNMq9JZ1kdANT+Bnk=; b=AZ9xd8OvoBn8m0rjK69Pw8EOU++rLuLCRByqZW1XnDLPPrV3JwAKoOiOmrZe32tShp xOaS3ShpGHoUkeW4ljb7Ssznp+6aXklLE2DlwljrhRL76w9jASyqP3cseHTshexvr3Ox j3A/88tiveOjVDalehq/reHM3hVuuXMOr+W8teHVqHLPQLCngrfycAgIkMzMfh/f94tE L9NPjTdGcWruWesueZVb3pMgYI+tRyPX5mL9Y2Phw9OucoOVRzj6E3GvfgFo3BNv8xeh 6iN0GtqA7yVaE7SkOMWouA1vhx557dJXMNvd6sTEPH1Sp1Ak9KgN3fekc8hUdAtA30Ue 0wEg== X-Gm-Message-State: APjAAAVTy2O1+s6BBBx9zGWfy01bV+9PXYYtXv2ItbU+EJ+Iq6jQgnRZ WAKpVZ0dkfqj/YyttfUZud3zIvCJ X-Google-Smtp-Source: APXvYqzsR+0iryHKEfyRBzm7bowhHAc9PeG/t72fd1UwYtnM1rZpvp5996FpJoFVfnfzYDdJFMUPWA== X-Received: by 2002:ac2:4289:: with SMTP id m9mr17484739lfh.49.1566324611260; Tue, 20 Aug 2019 11:10:11 -0700 (PDT) Received: from otyshchenko.kyiv.epam.com (ll-22.209.223.85.sovam.net.ua. [85.223.209.22]) by smtp.gmail.com with ESMTPSA id f6sm2900642lja.16.2019.08.20.11.10.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 20 Aug 2019 11:10:10 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Tue, 20 Aug 2019 21:09:43 +0300 Message-Id: <1566324587-3442-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1566324587-3442-1-git-send-email-olekstysh@gmail.com> References: <1566324587-3442-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V3 4/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, 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, struct arrlen, data, num_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 --- 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..2eb88a8 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)) +/* Re-allocate space for a structure with a flexible array of typed objects. */ +#define xrealloc_flex_struct(_ptr, _type, _field, _len) ({ \ + /* type checking: make sure that incoming pointer is of correct type */ \ + (void)((typeof(_ptr)) 0 == (_type *) 0); \ + ((_type *)_xrealloc(_ptr, offsetof(_type, _field[_len]), \ + __alignof__(_type))); \ +}) + +/* Allocate space for a structure with a flexible array of typed objects. */ +#define xmalloc_flex_struct(_type, _field, _len) \ + ((_type *)_xmalloc(offsetof(_type, _field[_len]), __alignof__(_type))) + /* Allocate untyped storage. */ #define xmalloc_bytes(_bytes) _xmalloc(_bytes, SMP_CACHE_BYTES) #define xzalloc_bytes(_bytes) _xzalloc(_bytes, SMP_CACHE_BYTES)