From patchwork Thu Sep 26 11:20:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11162547 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 DA79E1800 for ; Thu, 26 Sep 2019 11:23:06 +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 B581521A4A for ; Thu, 26 Sep 2019 11:23:06 +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="qvERueVU" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B581521A4A 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 1iDRpp-0008Pv-JN; Thu, 26 Sep 2019 11:21:21 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iDRpn-0008O3-VI for xen-devel@lists.xenproject.org; Thu, 26 Sep 2019 11:21:19 +0000 X-Inumbo-ID: b6a54ffc-e04f-11e9-8628-bc764e2007e4 Received: from mail-lj1-x244.google.com (unknown [2a00:1450:4864:20::244]) by localhost (Halon) with ESMTPS id b6a54ffc-e04f-11e9-8628-bc764e2007e4; Thu, 26 Sep 2019 11:20:58 +0000 (UTC) Received: by mail-lj1-x244.google.com with SMTP id m13so1729609ljj.11 for ; Thu, 26 Sep 2019 04:20:58 -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=dAXIdOzd5atuBvsXDzswi8p3w2Qe44gCP0DnzcVW2iI=; b=qvERueVUWGcvRjU9PHVaWcsnxxOxknWatng+s3KqSo26O+qAYin25nioWtvm0DmdHk V4yF7vI+z6G+nZY1DHG12ql4Ebanf90ooSgJd7EOO178byZi1R0L26E+Sl5oa69w0loq +CAvPdB9j4Xm7sWeXr9C8BH/rFEFiMGvnlQGua3nNnzlrzmjIiXPOFCoM9hEIESy7NGQ diXjqNpxE5Q6wJMtgmBLBXU4u3CLS7C0rpDwXLLKHrjOoAcr0qMzmfDoI4aZZoSM+d77 xIddAw+FCrEQSEZkjVra6/qw1VOm8Ur399G58574SiRpVVjIrZk13f7lCGsesdCZvZrO fEsw== 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=dAXIdOzd5atuBvsXDzswi8p3w2Qe44gCP0DnzcVW2iI=; b=MpKF8OqfAxUPCIReBcTJ3BEnG+HN+W8M0IY8rH49tS3lsrQhGR6+V+wHPQrmJdFIdn DIsNcORnlxQp0CCy1WTc/+LzoiHonwIX7lVyeIs5BV/wZCv3t/IBuROUySruu6Pv+OrK vfKw64UP6YRQ69dZ72xTOZXvZZbs+hCzshUpPX7TAXpLrnGTMOYOx1t1C6v8wxnBaxPM YcuP3E0gpuWpNnSheuLPqzA9RegwnRva+Oe3ij2duNRoYn+l+vnm3OdoB7SgmlAZpY3Q UfODaSev0kW95ANz8Euh9ZUGN+6ew4FL52nEQunflCpWVbibfEf1rVD+RAKmsv7hRsYZ NV0A== X-Gm-Message-State: APjAAAUTN/hhkqOsLwBVI1hHREbS4Q9Dkc5799w8V1h6QUlbTSc7L2Jz vt7CPPFIycKQ4Z1/kd/No/dSJcXT++0= X-Google-Smtp-Source: APXvYqzsvs6tapShkjo34DjDboU6SSqraxPjEWYzvfEeupYOqPTZAWd/NIoG3eRsdvTIeaKDg7RW8w== X-Received: by 2002:a2e:2e17:: with SMTP id u23mr522736lju.26.1569496857662; Thu, 26 Sep 2019 04:20:57 -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 n4sm526810lfi.63.2019.09.26.04.20.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Sep 2019 04:20:57 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Thu, 26 Sep 2019 14:20:30 +0300 Message-Id: <1569496834-7796-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> References: <1569496834-7796-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V6 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 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 Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Jan Beulich CC: Andrew Cooper CC: George Dunlap CC: Ian Jackson CC: Julien Grall CC: Konrad Rzeszutek Wilk CC: Stefano Stabellini CC: Tim Deegan CC: Wei Liu --- 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)