From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Anholt Subject: Re: [PATCH] drm/vc4: Use struct_size() in kzalloc() Date: Thu, 07 Feb 2019 14:59:02 -0800 Message-ID: <87r2cjmci1.fsf@anholt.net> References: <20190131010015.GA32272@embeddedor> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Return-path: In-Reply-To: <20190131010015.GA32272@embeddedor> Sender: linux-kernel-owner@vger.kernel.org To: David Airlie , Daniel Vetter Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" List-Id: dri-devel@lists.freedesktop.org --=-=-= Content-Type: text/plain "Gustavo A. R. Silva" writes: > One of the more common cases of allocation size calculations is finding > the size of a structure that has a zero-sized array at the end, along > with memory for some number of elements for that array. For example: > > struct foo { > int stuff; > void *entry[]; > }; > > instance = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL); > > Instead of leaving these open-coded and prone to type mistakes, we can > now use the new struct_size() helper: > > instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL); > > This code was detected with the help of Coccinelle. > > Signed-off-by: Gustavo A. R. Silva Reviewed and pushed to drm-misc-next. Thanks! --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEE/JuuFDWp9/ZkuCBXtdYpNtH8nugFAlxcuDYACgkQtdYpNtH8 nuhg7BAAqcbA9jWn2TWP8xHFGijD0r/gZEejmEsOwdKb6G5U+TTrlz8g5Z6cpX7J ArnkfJG41gE5nxonAJZEGKE/+aN2sTJjSq3MFTpU+igJSHhdlkTIZadiLNSZNgSy 1zmHm6Gufcpq6IJsBlKya3nd+p+sVIPBkEF51iMm98A8g6JL31kDioY8+Zq5mCjt rUZNHjgTStPaX+CZg0N7mZAUIdQCLVn6cz0ibDAJ/kgjekyk5N8YPp1ZaJmGnjSW Ov7fDcydL68bvA4IWDxZaUh9OPfiu1RyPWFCCEGc/OYgFrdHvUIzAAInrfPb+On9 yesrQq7+6ELGwsicgSm2Ks7dixudCeQpN17PjcF4rt1PBnSjRXw40MlNHe3/vdGW 6yelgdc4Gn0rQ7P6KTEZe7AC+QOUpe2bQEZeS8XMFKaSvpCbw5gdIa8mFVP6jDwI h1MFIFserjbEkn4TJYlYz+Y/PCaPXLLM/GBvyAI8wt3gXzKvBQAxrIekkNrzm4Mt MtQWVb0FqwiQeJg0KAEP2BgbLp9I0ZmNCoAA2JNUsvyHhSYjX49zwy5LHiI4noje Ez/k84p6bShWbOu/Lhu0NJhPZnnKt80MoWqZcyU2W0PpPtg2AZtjBt6XiLpFtwXO nV4z+M6HBfzaSVOqRyQa/GkHZLGcaShlIJR/r93lZIG0ihHXpjA= =vCJb -----END PGP SIGNATURE----- --=-=-=--