From patchwork Thu Jul 4 07:35:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 809911 Delivered-To: patch@linaro.org Received: by 2002:adf:a199:0:b0:367:895a:4699 with SMTP id u25csp1042096wru; Thu, 4 Jul 2024 00:36:53 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVu4x1dsQ8hXoqSKBhG+ef3n3AkWcEz1FNTiFEH6ewJFf9I82Y7+GGMKCcsIQCzn7qcCqOsx9apbn0t/Ocp15Nj X-Google-Smtp-Source: AGHT+IHaG64P7vmJzsjGbwUGmcHEUuwfgzPeVkX6mOM7I5GUnSqTcv0IHaFsL3CZg2C4GMWRYamx X-Received: by 2002:a17:906:30d0:b0:a72:80b8:ba5c with SMTP id a640c23a62f3a-a77ba460c2bmr51497766b.6.1720078613543; Thu, 04 Jul 2024 00:36:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720078613; cv=none; d=google.com; s=arc-20160816; b=BMwnOvsj402UJlkFObMhf2/tGIiPbmU0twyqeuwV1LHGdF2YM4vhXzbggxJmYe92wM DhS3pqqeFplx6whWd2NmsyJ+jEwTsMSu9uEehwSpUCmrps35JHfMmmlbwkMF3vA6fYVP k8d/oIDgOJNsxjqteG3gGS9pxN5fpmcqPRrf2m4qvbudmosbTdnEpOv2Fogi6dYf5yfw fOn9NqSboiVBoEi6vtu81o1EjcDnkZtMgqfLesCfpMzmgXVpYaG9QT1pEAqpb6ipyvvR 7t81R3tczL2Wa1eFkkhGcwWNBDdtz/3KMRptz9wm7Tku2aRIM45/tqusss9FhjQVzRbd j5pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=hOPMR99OqOllNHOIxIzHHALN42oSIyV3oL+kSiRbxRg=; fh=ez1zzjhC0XWyqTp7tWutqrA/nFdALDnQKrnQnXYhk5Q=; b=UOWCZXO7xpMBiergDyMsGGnk5E+ws2TQ+QDRlbh60Ek91n/2dKwycMIDLzc3nuoIgE ISyuczVxqIRBTh/DNqap6cQuTL5fggdxYg6DjN8SbJ7MhjFl3XsR4d7U/Y0wHGp3+iXO GbQwBCOPiOlxY+hLBBTHZO6HqDAdzg9HTHBXlLd+aP9TJQAwMPHtQSK3cvXoL+0U8e1g VS9bqNVB3gktgBPxpc9OgQSv6XaohCfjn51ixmFQZhsXSVWU+ODNqcdw7j/s568k1ht2 l1BfvXGhnt+7AUXX2frTc4CYaHs4K/YxZ2+om53r+EOZUN3uYELHRsTa+SzaJeFZuvPd +KWA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id a640c23a62f3a-a77a1f19455si152284766b.152.2024.07.04.00.36.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 00:36:53 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id ABB37888B3; Thu, 4 Jul 2024 09:36:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 81731888BA; Thu, 4 Jul 2024 09:36:21 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 82C6E888A7 for ; Thu, 4 Jul 2024 09:36:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E1782367; Thu, 4 Jul 2024 00:36:43 -0700 (PDT) Received: from a079122.blr.arm.com (a079122.arm.com [10.162.17.48]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BD2CF3F762; Thu, 4 Jul 2024 00:36:15 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Tom Rini , Ilias Apalodimas , Heinrich Schuchardt , Simon Glass , Marek Vasut , Mark Kettenis , Fabio Estevam , Michal Simek , Sughosh Ganu Subject: [RFC PATCH v2 04/48] lib: Convert str_to_list() to use alist Date: Thu, 4 Jul 2024 13:05:00 +0530 Message-Id: <20240704073544.670249-5-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240704073544.670249-1-sughosh.ganu@linaro.org> References: <20240704073544.670249-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: Simon Glass Use this new data structure in the utility function. Signed-off-by: Simon Glass Signed-off-by: Sughosh Ganu --- lib/strto.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/strto.c b/lib/strto.c index f83ac67c66..f059408755 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -9,6 +9,7 @@ * Wirzenius wrote this portably, Torvalds fucked it up :-) */ +#include #include #include #include @@ -226,37 +227,39 @@ void str_to_upper(const char *in, char *out, size_t len) const char **str_to_list(const char *instr) { - const char **ptr; - char *str, *p; - int count, i; + struct alist alist; + char *str, *p, *start; /* don't allocate if the string is empty */ str = *instr ? strdup(instr) : (char *)instr; if (!str) return NULL; - /* count the number of space-separated strings */ - for (count = 0, p = str; *p; p++) { + alist_init_struct(&alist, char *); + + if (*str) + alist_add(&alist, &str, char *); + for (start = str, p = str; *p; p++) { if (*p == ' ') { - count++; *p = '\0'; + start = p + 1; + if (*start) + alist_add(&alist, &start, char *); } } - if (p != str && p[-1]) - count++; - /* allocate the pointer array, allowing for a NULL terminator */ - ptr = calloc(count + 1, sizeof(char *)); - if (!ptr) { - if (*str) + /* terminate list */ + p = NULL; + alist_add(&alist, &p, char *); + if (alist_err(&alist)) { + alist_uninit(&alist); + + if (*instr) free(str); return NULL; } - for (i = 0, p = str; i < count; p += strlen(p) + 1, i++) - ptr[i] = p; - - return ptr; + return alist_uninit_move(&alist, NULL, const char *); } void str_free_list(const char **ptr)