linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] HID: Use krealloc
Date: Wed,  1 Mar 2017 11:19:09 -0800	[thread overview]
Message-ID: <4048f6d655d55b19fdaecab4e61f6883e12f72f0.1488395879.git.joe@perches.com> (raw)
In-Reply-To: <cover.1488395879.git.joe@perches.com>

This kmalloc/memcpy is equivalent to krealloc, so use krealloc
to avoid a possibly unnecessary memcpy.

Miscellanea:

o Add temporary variables osize and nsize to improve readability
o krealloc has a defect and does not support GFP_ZERO
  so the memset must be kept

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/hid/hid-core.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index e9e87d337446..b1ebf53ca879 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -131,21 +131,19 @@ static int open_collection(struct hid_parser *parser, unsigned type)
 	}
 
 	if (parser->device->maxcollection == parser->device->collection_size) {
-		collection = kmalloc(sizeof(struct hid_collection) *
-				parser->device->collection_size * 2, GFP_KERNEL);
-		if (collection == NULL) {
-			hid_err(parser->device, "failed to reallocate collection array\n");
+		unsigned int osize = parser->device->collection_size;
+		unsigned int nsize = osize * 2;
+
+		collection = krealloc(parser->device->collection,
+				      nsize * sizeof(struct hid_collection),
+				      GFP_KERNEL);
+		if (!collection)
 			return -ENOMEM;
-		}
-		memcpy(collection, parser->device->collection,
-			sizeof(struct hid_collection) *
-			parser->device->collection_size);
-		memset(collection + parser->device->collection_size, 0,
-			sizeof(struct hid_collection) *
-			parser->device->collection_size);
-		kfree(parser->device->collection);
+
+		memset(collection + osize, 0,
+		       sizeof(struct hid_collection) * osize);
 		parser->device->collection = collection;
-		parser->device->collection_size *= 2;
+		parser->device->collection_size = nsize;
 	}
 
 	parser->collection_stack[parser->collection_stack_ptr++] =
-- 
2.10.0.rc2.1.g053435c


  reply	other threads:[~2017-03-01 19:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-01 19:19 [PATCH 0/2] HID: allocation neatening and code size reduction Joe Perches
2017-03-01 19:19 ` Joe Perches [this message]
2017-03-01 19:19 ` [PATCH 2/2] HID: Remove unnecessary OOM messages Joe Perches
     [not found]   ` <0fbe692283802d36df389af7cbda9a4bff44db5e.1488395879.git.joe-6d6DIl74uiNBDgjK7y7TUQ@public.gmane.org>
2017-03-02  6:51     ` Michel Hermier
2017-03-02  6:54       ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4048f6d655d55b19fdaecab4e61f6883e12f72f0.1488395879.git.joe@perches.com \
    --to=joe@perches.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).