mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Li kunyu <kunyu@nfschina.com>
To: krisman@collabora.com
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Li kunyu <kunyu@nfschina.com>
Subject: [PATCH] unicode: mkutf8data: Add unicode_data_remalloc function
Date: Mon, 24 Oct 2022 18:02:46 +0800	[thread overview]
Message-ID: <20221024100246.180280-1-kunyu@nfschina.com> (raw)

Add unicode_data_remalloc function, used to simplify unicode_data member
assignment use.

Signed-off-by: Li kunyu <kunyu@nfschina.com>
---
 fs/unicode/mkutf8data.c | 58 ++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 30 deletions(-)

diff --git a/fs/unicode/mkutf8data.c b/fs/unicode/mkutf8data.c
index bc1a7c8b5c8d..366de944fed3 100644
--- a/fs/unicode/mkutf8data.c
+++ b/fs/unicode/mkutf8data.c
@@ -2230,13 +2230,25 @@ static void nfdicf_init(void)
 		file_fail(fold_name);
 }
 
+static void unicode_data_remalloc(void **dst, size_t len, int free)
+{
+	unsigned int *um = malloc(len);
+
+	if (free == 1)
+		free(*dst);
+
+	if (um)
+		*um = 0
+
+	*dst = um;
+}
+
 static void ignore_init(void)
 {
 	FILE *file;
 	unsigned int unichar;
 	unsigned int first;
 	unsigned int last;
-	unsigned int *um;
 	int count;
 	int ret;
 
@@ -2255,14 +2267,10 @@ static void ignore_init(void)
 			if (!utf32valid(first) || !utf32valid(last))
 				line_fail(prop_name, line);
 			for (unichar = first; unichar <= last; unichar++) {
-				free(unicode_data[unichar].utf32nfdi);
-				um = malloc(sizeof(unsigned int));
-				*um = 0;
-				unicode_data[unichar].utf32nfdi = um;
-				free(unicode_data[unichar].utf32nfdicf);
-				um = malloc(sizeof(unsigned int));
-				*um = 0;
-				unicode_data[unichar].utf32nfdicf = um;
+				unicode_data_remalloc(&unicode_data[unichar].utf32nfdi,
+						sizeof(unsigned int), 1);
+				unicode_data_remalloc(&unicode_data[unichar].utf32nfdicf,
+						sizeof(unsigned int), 1);
 				count++;
 			}
 			if (verbose > 1)
@@ -2276,14 +2284,11 @@ static void ignore_init(void)
 				continue;
 			if (!utf32valid(unichar))
 				line_fail(prop_name, line);
-			free(unicode_data[unichar].utf32nfdi);
-			um = malloc(sizeof(unsigned int));
-			*um = 0;
-			unicode_data[unichar].utf32nfdi = um;
-			free(unicode_data[unichar].utf32nfdicf);
-			um = malloc(sizeof(unsigned int));
-			*um = 0;
-			unicode_data[unichar].utf32nfdicf = um;
+
+			unicode_data_remalloc(&unicode_data[unichar].utf32nfdi,
+					sizeof(unsigned int), 1);
+			unicode_data_remalloc(&unicode_data[unichar].utf32nfdicf,
+					sizeof(unsigned int), 1);
 			if (verbose > 1)
 				printf(" %X Default_Ignorable_Code_Point\n",
 					unichar);
@@ -2490,7 +2495,6 @@ static void nfdi_decompose(void)
 {
 	unsigned int unichar;
 	unsigned int mapping[19]; /* Magic - guaranteed not to be exceeded. */
-	unsigned int *um;
 	unsigned int *dc;
 	int count;
 	int i;
@@ -2522,16 +2526,13 @@ static void nfdi_decompose(void)
 			mapping[i++] = 0;
 			if (ret)
 				break;
-			free(unicode_data[unichar].utf32nfdi);
-			um = malloc(i * sizeof(unsigned int));
-			memcpy(um, mapping, i * sizeof(unsigned int));
-			unicode_data[unichar].utf32nfdi = um;
+			unicode_data_remalloc(&unicode_data[unichar].utf32nfdi,
+					i * sizeof(unsigned int), 1);
 		}
 		/* Add this decomposition to nfdicf if there is no entry. */
 		if (!unicode_data[unichar].utf32nfdicf) {
-			um = malloc(i * sizeof(unsigned int));
-			memcpy(um, mapping, i * sizeof(unsigned int));
-			unicode_data[unichar].utf32nfdicf = um;
+			unicode_data_remalloc(&unicode_data[unichar].utf32nfdicf,
+					i * sizeof(unsigned int), 0);
 		}
 		if (verbose > 1)
 			print_utf32nfdi(unichar);
@@ -2545,7 +2546,6 @@ static void nfdicf_decompose(void)
 {
 	unsigned int unichar;
 	unsigned int mapping[19]; /* Magic - guaranteed not to be exceeded. */
-	unsigned int *um;
 	unsigned int *dc;
 	int count;
 	int i;
@@ -2576,10 +2576,8 @@ static void nfdicf_decompose(void)
 			mapping[i++] = 0;
 			if (ret)
 				break;
-			free(unicode_data[unichar].utf32nfdicf);
-			um = malloc(i * sizeof(unsigned int));
-			memcpy(um, mapping, i * sizeof(unsigned int));
-			unicode_data[unichar].utf32nfdicf = um;
+			unicode_data_remalloc(&unicode_data[unichar].utf32nfdicf,
+					i * sizeof(unsigned int), 1);
 		}
 		if (verbose > 1)
 			print_utf32nfdicf(unichar);
-- 
2.18.2


                 reply	other threads:[~2022-10-24 10:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20221024100246.180280-1-kunyu@nfschina.com \
    --to=kunyu@nfschina.com \
    --cc=krisman@collabora.com \
    --cc=linux-fsdevel@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

all inboxes | Powered by JetHome®