From patchwork Tue Sep 24 15:30:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 11159123 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 9FC4F1668 for ; Tue, 24 Sep 2019 15:32:29 +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 7B847207FD for ; Tue, 24 Sep 2019 15:32:29 +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="c9rxzKKS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7B847207FD 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 1iCmmV-0007ts-TI; Tue, 24 Sep 2019 15:31:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iCmmU-0007tR-Vr for xen-devel@lists.xenproject.org; Tue, 24 Sep 2019 15:31:11 +0000 X-Inumbo-ID: 4c5337d8-dee0-11e9-97fb-bc764e2007e4 Received: from mail-lf1-x142.google.com (unknown [2a00:1450:4864:20::142]) by localhost (Halon) with ESMTPS id 4c5337d8-dee0-11e9-97fb-bc764e2007e4; Tue, 24 Sep 2019 15:30:55 +0000 (UTC) Received: by mail-lf1-x142.google.com with SMTP id q11so1739458lfc.11 for ; Tue, 24 Sep 2019 08:30:55 -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=c9rxzKKSk0cCCaf04TMT7q8FmKtllw9FcvMS0MsrP2lO/cH92sh3wuI4ogVvd8fntN g+eKwqaDkxNnPUBPvQHu8MhtP4LgmF5Wp7wTaWC8uHf39Wu21KzoKm4Rt9IubIQ4mfpS M7Kt+v1UVA1m6pzDWPt1HXxPWs25rybA1BHtYiB+JHRHB9fmWRnqUdOW8fTboa5nQ6Dl 5Wp86FTv7tMefJ8sAGHRSpAnIE692bzk5Ia0+IxY25oR9pOa9lTl8mLj+4rsA+TW08i+ 4U6HrsGLbdNn0Ybv9zrYLjBYOteQsUo1oGn4eQ3ZvC7Ba1dKzh/nHjvPie9ysAPCUoU1 TuAQ== 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=m4znBmj0gSML6a+PtX1qAClpufr48L73rR6EJ0ZaURVOgmmbuyfaaizQc2J+JXZECr SPpApIiJUoI95sawUEizattpMzRBhPaChctPjP8mO+8d9NJzhDbAAiS/E6u02L1Vj81x 7SV6dilBoAAc+dxET4A6UXawibbqiSDtzaRGX+zw3k+whe6H//afYpIxIMp5+/wFz+Cu 8lTpw/IaIR1VqKbgXCfg5gpTMOdTXleuaE5ryT3WZ4KnmRGT6IEqaulet/b9nDTGUDw9 Tz1S2Yr77EJCGpjlhcT8e0jwz/E3a1EXy6wFfprG8DdVrzV+MQIRyhTHdI0IBrX8g3ZN o4NQ== X-Gm-Message-State: APjAAAVpkClwLE4QhHlKmJjiurTE2lei/N9r7OQISFHZdgTu+odV0RC2 ZIpEzOK9u1smtxQZGAZwIX6zERlS28s= X-Google-Smtp-Source: APXvYqyTopsiIiak5OUxOOlpbxeMPwSJ/QGpMYAc5YuXNte2/WoJY0PWJEINJqb+/gUKPk3/I7pfpg== X-Received: by 2002:ac2:5dd0:: with SMTP id x16mr2354993lfq.38.1569339053076; Tue, 24 Sep 2019 08:30:53 -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 z128sm525818lfa.1.2019.09.24.08.30.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Sep 2019 08:30:52 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org Date: Tue, 24 Sep 2019 18:30:23 +0300 Message-Id: <1569339027-19484-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1569339027-19484-1-git-send-email-olekstysh@gmail.com> References: <1569339027-19484-1-git-send-email-olekstysh@gmail.com> Subject: [Xen-devel] [PATCH V5 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)