2022-10-24 05:05:41

by Li kunyu

[permalink] [raw]
Subject: [PATCH 1/2] unicode: mkutf8data: Add compound malloc function

The patch has the following modifications.
1. Add unicode_data_malloc function, which realizes the combined use of
malloc and memcpy, and assigns values to dst.
2. Add unicode_data_remalloc function assigns 0 to the data in the
allocated memory pointer. When the integer free parameter specifies 1,
execute free (* dst), and finally assign the value to dst.
3. Remove the original um pointer related code and replace it with these
two functions.

Signed-off-by: Li kunyu <[email protected]>
---
fs/unicode/mkutf8data.c | 99 +++++++++++++++++++----------------------
1 file changed, 47 insertions(+), 52 deletions(-)

diff --git a/fs/unicode/mkutf8data.c b/fs/unicode/mkutf8data.c
index bc1a7c8b5c8d..adcf018f7985 100644
--- a/fs/unicode/mkutf8data.c
+++ b/fs/unicode/mkutf8data.c
@@ -2113,6 +2113,29 @@ static int ignore_compatibility_form(char *type)
return 0;
}

+static void unicode_data_malloc(void *src, void **dst, size_t len)
+{
+ unsigned int *um = malloc(len);
+
+ if (um)
+ memcpy(um, src, len);
+
+ *dst = um;
+}
+
+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 nfdi_init(void)
{
FILE *file;
@@ -2120,7 +2143,6 @@ static void nfdi_init(void)
unsigned int mapping[19]; /* Magic - guaranteed not to be exceeded. */
char *s;
char *type;
- unsigned int *um;
int count;
int i;
int ret;
@@ -2159,10 +2181,8 @@ static void nfdi_init(void)
}
mapping[i++] = 0;

- um = malloc(i * sizeof(unsigned int));
- memcpy(um, mapping, i * sizeof(unsigned int));
- unicode_data[unichar].utf32nfdi = um;
-
+ unicode_data_malloc(mapping, &unicode_data[unichar].utf32nfdi,
+ i * sizeof(unsigned int));
if (verbose > 1)
print_utf32nfdi(unichar);
count++;
@@ -2181,7 +2201,6 @@ static void nfdicf_init(void)
unsigned int mapping[19]; /* Magic - guaranteed not to be exceeded. */
char status;
char *s;
- unsigned int *um;
int i;
int count;
int ret;
@@ -2215,10 +2234,8 @@ static void nfdicf_init(void)
}
mapping[i++] = 0;

- um = malloc(i * sizeof(unsigned int));
- memcpy(um, mapping, i * sizeof(unsigned int));
- unicode_data[unichar].utf32nfdicf = um;
-
+ unicode_data_malloc(mapping, &unicode_data[unichar].utf32nfdicf,
+ i * sizeof(unsigned int));
if (verbose > 1)
print_utf32nfdicf(unichar);
count++;
@@ -2236,7 +2253,6 @@ static void ignore_init(void)
unsigned int unichar;
unsigned int first;
unsigned int last;
- unsigned int *um;
int count;
int ret;

@@ -2255,14 +2271,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 +2288,10 @@ 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);
@@ -2307,7 +2315,6 @@ static void corrections_init(void)
unsigned int minor;
unsigned int revision;
unsigned int age;
- unsigned int *um;
unsigned int mapping[19]; /* Magic - guaranteed not to be exceeded. */
char *s;
int i;
@@ -2359,10 +2366,8 @@ static void corrections_init(void)
}
mapping[i++] = 0;

- um = malloc(i * sizeof(unsigned int));
- memcpy(um, mapping, i * sizeof(unsigned int));
- corrections[count].utf32nfdi = um;
-
+ unicode_data_malloc(mapping, &corrections[count].utf32nfdi,
+ i * sizeof(unsigned int));
if (verbose > 1)
printf(" %X -> %s -> %s V%d_%d_%d\n",
unichar, buf0, buf1, major, minor, revision);
@@ -2437,7 +2442,6 @@ static void hangul_decompose(void)
/* unsigned int sc = (lc * nc); */
unsigned int unichar;
unsigned int mapping[4];
- unsigned int *um;
int count;
int i;

@@ -2459,14 +2463,12 @@ static void hangul_decompose(void)
mapping[i++] = 0;

assert(!unicode_data[unichar].utf32nfdi);
- um = malloc(i * sizeof(unsigned int));
- memcpy(um, mapping, i * sizeof(unsigned int));
- unicode_data[unichar].utf32nfdi = um;
+ unicode_data_malloc(mapping, &unicode_data[unichar].utf32nfdi,
+ i * sizeof(unsigned int));

assert(!unicode_data[unichar].utf32nfdicf);
- um = malloc(i * sizeof(unsigned int));
- memcpy(um, mapping, i * sizeof(unsigned int));
- unicode_data[unichar].utf32nfdicf = um;
+ unicode_data_malloc(mapping, &unicode_data[unichar].utf32nfdicf,
+ i * sizeof(unsigned int));

/*
* Add a cookie as a reminder that the hangul syllable
@@ -2490,7 +2492,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 +2523,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 +2543,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 +2573,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


2022-10-24 08:16:27

by Bagas Sanjaya

[permalink] [raw]
Subject: Re: [PATCH 1/2] unicode: mkutf8data: Add compound malloc function

On Mon, Oct 24, 2022 at 12:50:30PM +0800, Li kunyu wrote:
> The patch has the following modifications.
> 1. Add unicode_data_malloc function, which realizes the combined use of
> malloc and memcpy, and assigns values to dst.
> 2. Add unicode_data_remalloc function assigns 0 to the data in the
> allocated memory pointer. When the integer free parameter specifies 1,
> execute free (* dst), and finally assign the value to dst.
> 3. Remove the original um pointer related code and replace it with these
> two functions.
>

These changes should be split into separate patches.

Also, please take a time to review the patch description because what I
read above is somewhat confusing.

And the last, please learn how to use git-format-patch(1) and
git-send-email(1) correctly.

Thanks.

--
An old man doll... just what I always wanted! - Clara


Attachments:
(No filename) (867.00 B)
signature.asc (235.00 B)
Download all attachments

2022-10-24 09:13:39

by Li kunyu

[permalink] [raw]
Subject: Re: [PATCH 1/2] unicode: mkutf8data: Add compound malloc function


I send the 1/2 and 2/2 patches separately, and divide the two functions and related modifications in the 2/2 patch into two patches.

thanks,
kunyu

2022-10-24 09:52:33

by Bagas Sanjaya

[permalink] [raw]
Subject: Re: [PATCH 1/2] unicode: mkutf8data: Add compound malloc function

On 10/24/22 15:26, Li kunyu wrote:
>
> I send the 1/2 and 2/2 patches separately, and divide the two functions and related modifications in the 2/2 patch into two patches.
>

No, not that way.

Here's the recipe for submitting patch series (a set of two or more patches),
assuming that you do the work on a branch which is based on mainline (master):

1. First, create directory which to store the patches.
2. Determine the base commit for your branch. Most of the times `git merge-base
master <your branch>` can be used, but sometimes you need to determine that
manually by seeing the commit log with `git log`.
3. Generate the patch series (preferably with cover letter) by `git format-patch
-o <directory> --cover-letter --base=<base commit> <base commit>`. You can
now write the description about the series in the cover letter (which will
be 0000-cover-letter.patch).
4. Find the maintainers which will review your series with
`scripts/get_maintainer.pl /path/to/directory/*.patch`. Ignore your email
address if it exists.
5. Send the series with `git send-email <--to and/or --cc maintainers>
/path/to/directory/*.patch`. All patches will be sent as reply to the
cover letter.

Thanks.

--
An old man doll... just what I always wanted! - Clara