From patchwork Mon Oct 24 04:50:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li kunyu X-Patchwork-Id: 8160 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp255006wru; Sun, 23 Oct 2022 21:56:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6QRx/P/lSy/LoLck5wVQECCiEzNxqtuuDNoOuBjFo+EHxPR6WKibXrHzNQz3fh5yfmBd/+ X-Received: by 2002:a63:1748:0:b0:46f:18be:4880 with SMTP id 8-20020a631748000000b0046f18be4880mr656905pgx.128.1666587419067; Sun, 23 Oct 2022 21:56:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666587419; cv=none; d=google.com; s=arc-20160816; b=gsInZ/XsaorAj8ZWzueanteaTO9FSQFKQLLNytcSaR7eVYsnz7+77blpUAXkIhu0I5 8nSq4YAf2cZ0+pSctU+9rWI4WhFrAVrLw1Pn5BOp1OWJ/7nvsh9wCo5M6BrNDFuveNmX 5Ndkzyc4CrSsO7eReHp1PpJVRSC/ZSxyEwdr3pBWc/1Q91Kd7DZYYJ5+M/5PEyW0waX6 TmRU8E+sGlLEp99+Rr0r5KTFYUCsjK9FWhse1CD0jb/QrXs4zjR7FjIMyf8nhBLNvC+S p7OSENGQ2qDBsuN9zgLjo1YOoa6y3JRc9z6o6SiOK9ZSc8Qi98yor67izqLnccfemg8P 65cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=katTJEIwfqGocA61iEvtPbpPXcUQ6ZU6p/3uEl2ml6Y=; b=J/zx0BV34AFyEsMpfpPJi0IMyzXMXja+9LCHXdMAmhVc7LptBrLmOAqiCg7QWqlNOw kqvJstLSDD6A/72GD7Uw/oHuXDa48EVP1ATou9KmDxPhLsicXXeTLfJ1thnU9ndwjJUO D64BSKdnwiAPELHlfWGSJ0CkYMG1FCVvivp6cfLKLq3WtWSNo2swAHcpUcLVd0r8zBX4 palZnAWlpAVakYNpGH3I1sqHZCOmxoXIP6bFbKlTmjvM5doZKLsIRv5on0gZDQwWg67H m0mRUO951EkIMKfau6128hkiwd3GUotGqdB/3SKz7ugheaZSMkJ17JLAwRCyLltVQ7/+ PR3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a170902da8700b001768b832a41si38817313plx.584.2022.10.23.21.56.46; Sun, 23 Oct 2022 21:56:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229939AbiJXEur (ORCPT + 99 others); Mon, 24 Oct 2022 00:50:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbiJXEuo (ORCPT ); Mon, 24 Oct 2022 00:50:44 -0400 Received: from mail.nfschina.com (unknown [124.16.136.209]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CF3084E421; Sun, 23 Oct 2022 21:50:41 -0700 (PDT) Received: from localhost (unknown [127.0.0.1]) by mail.nfschina.com (Postfix) with ESMTP id 97FC41E80D74; Mon, 24 Oct 2022 12:49:24 +0800 (CST) X-Virus-Scanned: amavisd-new at test.com Received: from mail.nfschina.com ([127.0.0.1]) by localhost (mail.nfschina.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Os0l2vBq3zp6; Mon, 24 Oct 2022 12:49:21 +0800 (CST) Received: from localhost.localdomain (unknown [219.141.250.2]) (Authenticated sender: kunyu@nfschina.com) by mail.nfschina.com (Postfix) with ESMTPA id 8B8B41E80CA5; Mon, 24 Oct 2022 12:49:21 +0800 (CST) From: Li kunyu To: krisman@collabora.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Li kunyu Subject: [PATCH 1/2] unicode: mkutf8data: Add compound malloc function Date: Mon, 24 Oct 2022 12:50:30 +0800 Message-Id: <20221024045030.177438-1-kunyu@nfschina.com> X-Mailer: git-send-email 2.18.2 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747543569394811677?= X-GMAIL-MSGID: =?utf-8?q?1747543569394811677?= 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 --- 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);