From patchwork Thu Jan 11 13:05:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 124196 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp653854qgn; Thu, 11 Jan 2018 05:07:18 -0800 (PST) X-Google-Smtp-Source: ACJfBouukSMsXJDC8Swie/5L7fa9G2rakooReg3G04BYfWxky3LhXEaZVYmUyjUEYkwEmAwYza/F X-Received: by 10.98.36.69 with SMTP id r66mr19696325pfj.96.1515676037895; Thu, 11 Jan 2018 05:07:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515676037; cv=none; d=google.com; s=arc-20160816; b=MUn5VV22g1py1HQlNruwdEGcAGfg8+yH8oDHGcLPHSzflP0Lq2wGf5fEpllNv3oQXK OP66au1zKDxnvc34qECljQknpUEHuOPszg+xZ18H8Mm6kdWf9PFuew0o0BaUh0swQMWV JJFyPwg7Xb4ILYQPHldYMUTxg6liCiFHaGP06oMwGrczkMhu8CaJRVF4WwX2JS94VP8C tg0dDXZAaTuf38yIMCoyUcU19VEJALPd8Un6peOGwECTHcKJ6CS1E07Lhk1dfqq1XsnY r55tioc+gB9vrnciTkHYavwpdb8tLvSU4QIBPMJeCGSBdFx708NpvYMFdOYK+fLkz9ix 1IRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter :arc-authentication-results; bh=lfrnL9KTtg/tfbAR4dy6BJvs4PqANmmIxPoFeDsu3ho=; b=kkl6bKJUQlQ2Xh4k+SUVo7DRTZcxOQHPN77uIRPk3l083y22iqnZZzU1gH7qTYQBAe W7K6g3nNOnztOU/zWzw2Jplr+LTWgyxSTb6mjrUeAkKaYAbwwGH+u1bHzBLQgoL8BiSO +0hVFK/ehCCDmNR/D7QzHhXzY6ZmaR+jeQuMNX5QQHLRkc+1bH0EK/QcC5/GkOQY93eL tSVEjXmruxli4eiB1O/nEainfm1jnF/uh2Pg5yjzQ1/tGjQanzOpNpOU20UbJJY16FXR jUpcLigLSORo9i9O8CjqMULp9jyeC4+C0kEt/mxynh+4HoHGZivfjYilNlBDdDAi6oop VBiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=yRX9Ouj+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m81si3393054pfk.168.2018.01.11.05.07.17; Thu, 11 Jan 2018 05:07:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=yRX9Ouj+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934464AbeAKNHP (ORCPT + 28 others); Thu, 11 Jan 2018 08:07:15 -0500 Received: from conuserg-12.nifty.com ([210.131.2.79]:60688 "EHLO conuserg-12.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934394AbeAKNGb (ORCPT ); Thu, 11 Jan 2018 08:06:31 -0500 Received: from grover.sesame (FL1-125-199-20-195.osk.mesh.ad.jp [125.199.20.195]) (authenticated) by conuserg-12.nifty.com with ESMTP id w0BD5mX2031475; Thu, 11 Jan 2018 22:05:49 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-12.nifty.com w0BD5mX2031475 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1515675949; bh=lfrnL9KTtg/tfbAR4dy6BJvs4PqANmmIxPoFeDsu3ho=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yRX9Ouj+FG6U3kdn6PiANggR1lh3esCd99qJ5zI7twH+9tDe/zAKYgnZV73y9mNND pmjOidU1GY1c5/QV48qVbFFUkRf2UOW4ToA2559C8YA8XmN+w7ZNmglblcgVmRdL5q LiX6JV7sGYOWnpqxW8CGS1xijq9GQUJAvKWstoWf6oV6MfHIR2h9B9nNB9o4rkJfVd 4B83UT1uCX8/aF8seIyKqFgUh3mJFx3td1ZdhqyVsGUhjrd4qsJrRlEBkaro/qMh2q owjXr4Bdp4NHhtxdn7iOVKsjX7Bnp35G2J0RuJozQDZRBthBMk3o50lhxVR9BA7sdq AfkhWsXIlhJvQ== X-Nifty-SrcIP: [125.199.20.195] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Michal Marek , Sam Ravnborg , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/7] fixdep: use malloc() and read() to load dep_file to buffer Date: Thu, 11 Jan 2018 22:05:41 +0900 Message-Id: <1515675946-7514-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515675946-7514-1-git-send-email-yamada.masahiro@socionext.com> References: <1515675946-7514-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit dee81e988674 ("fixdep: faster CONFIG_ search") changed how to read files in which CONFIG options are searched. It used malloc() and read() instead of mmap() because it needed to zero-terminate the buffer in order to use strstr(). print_deps() was left untouched since there was no reason to change it. Now, I have two motivations to change it in the same way. - do_config_file() and print_deps() do quite similar things; they open a file, load it onto memory, and pass it to a parser function. If we use malloc() and read() for print_deps() too, we can factor out the common code. (I will do this in the next commit.) - parse_dep_file() copies each token to a temporary buffer because it needs to zero-terminate it to be passed to printf(). It is not possible to modify the buffer directly because it is mmap'ed with O_RDONLY. If we load the file content into a malloc'ed buffer, we can insert '\0' after each token, and save memcpy(). (I will do this in the commit after next.) Signed-off-by: Masahiro Yamada --- Changes in v2: - Remove #include scripts/basic/fixdep.c | 56 ++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 24 deletions(-) -- 2.7.4 diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index b9b4bbf..91bb4c1 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -104,7 +104,6 @@ #include #include -#include #include #include #include @@ -308,24 +307,27 @@ static void do_config_file(const char *filename) * assignments are parsed not only by make, but also by the rather simple * parser in scripts/mod/sumversion.c. */ -static void parse_dep_file(void *map, size_t len) +static void parse_dep_file(char *m) { - char *m = map; - char *end = m + len; char *p; char s[PATH_MAX]; - int is_target; + int is_last, is_target; int saw_any_target = 0; int is_first_dep = 0; - while (m < end) { + while (1) { /* Skip any "white space" */ - while (m < end && (*m == ' ' || *m == '\\' || *m == '\n')) + while (*m == ' ' || *m == '\\' || *m == '\n') m++; + + if (!*m) + break; + /* Find next "white space" */ p = m; - while (p < end && *p != ' ' && *p != '\\' && *p != '\n') + while (*p && *p != ' ' && *p != '\\' && *p != '\n') p++; + is_last = (*p == '\0'); /* Is the token we found a target name? */ is_target = (*(p-1) == ':'); /* Don't write any target names into the dependency file */ @@ -373,6 +375,10 @@ static void parse_dep_file(void *map, size_t len) do_config_file(s); } } + + if (is_last) + break; + /* * Start searching for next token immediately after the first * "whitespace" character that follows this token. @@ -391,40 +397,42 @@ static void parse_dep_file(void *map, size_t len) printf("$(deps_%s):\n", target); } -static void print_deps(void) +static void print_deps(const char *filename) { struct stat st; int fd; - void *map; + char *buf; - fd = open(depfile, O_RDONLY); + fd = open(filename, O_RDONLY); if (fd < 0) { fprintf(stderr, "fixdep: error opening depfile: "); - perror(depfile); + perror(filename); exit(2); } if (fstat(fd, &st) < 0) { fprintf(stderr, "fixdep: error fstat'ing depfile: "); - perror(depfile); + perror(filename); exit(2); } if (st.st_size == 0) { - fprintf(stderr,"fixdep: %s is empty\n",depfile); close(fd); return; } - map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if ((long) map == -1) { - perror("fixdep: mmap"); - close(fd); - return; + buf = malloc(st.st_size + 1); + if (!buf) { + perror("fixdep: malloc"); + exit(2); } + if (read(fd, buf, st.st_size) != st.st_size) { + perror("fixdep: read"); + exit(2); + } + buf[st.st_size] = '\0'; + close(fd); - parse_dep_file(map, st.st_size); - - munmap(map, st.st_size); + parse_dep_file(buf); - close(fd); + free(buf); } int main(int argc, char *argv[]) @@ -440,7 +448,7 @@ int main(int argc, char *argv[]) cmdline = argv[3]; print_cmdline(); - print_deps(); + print_deps(depfile); return 0; }