From patchwork Tue Jan 9 08:30:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 123810 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3750237qgn; Tue, 9 Jan 2018 00:32:45 -0800 (PST) X-Google-Smtp-Source: ACJfBova1jh4rLH3ZJbDAk2w/KDi3se1OjdXSKGH12P9UuGNChzXCyQt+J29FFeCGIbkqypCs++Z X-Received: by 10.101.82.205 with SMTP id z13mr11588079pgp.403.1515486765640; Tue, 09 Jan 2018 00:32:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515486765; cv=none; d=google.com; s=arc-20160816; b=zRKfydHIOFmGgmi0aZBv/uXL8nVXV+jILvVgySkDnEoEfg70+Qv0mqtDePu2lBAFAC kpKwkiam81AjXXWAG0ogUHv4ugszEw4ue7fxtJhGefFy1ufxOxb79peEhbWj3kXfuRyP lfoArdoI6NAwHF+uE7fQmdpJmpX+3jFcsuTaWDlXmiZv+ArgFL1sigTUbAuge0TaU6rw u8SgarRf9zaopDUv4ljWexafglGijAWPWJxnKrqkshkgaO5U/krXTdyftX+XKqUQbMAt dLD2kuwC5ympIm46r6+1TBMc+ZF0SPbVCvwHfG3I70N53rT7SY1BYKxz+SMNy7ZC39Ng 3yhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter:arc-authentication-results; bh=eII+qy4k8i367HbCbA4Q0X4SPtggcoLkPrjaGg5QK9M=; b=je4i8C7FXFyRVSbVf35pPegvACCzvAXLtwnSw+674WIDtoEUxobkazXDWemBTsE45+ oRbHKdm/bGuMrMm5ItGDUwCSyKjX5L5IYhtzTWzh0RHAa8wz6qc/0W5Le4vsVQqfx6YH uw/5GJ5bbbw8t547STGZDgR9gKHB22iZZYRYjrPQH01sW3muy0pxf0IFp2rVXqhnrUwL UF9w3xmXbv5qpRxjM2eujnUheCM/K10xM2pec7syKoVtPlfhA0dLDsYbyMcA+biTlmEk Ep8um389Mzz9xGW56n/TIcRXWcO1p+sXrseROUBwdaU0NBKzq5vK3m9k5JoWgIf45Fyv QYVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=UNKteju4; 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 j12si8715185pgq.184.2018.01.09.00.32.45; Tue, 09 Jan 2018 00:32:45 -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=UNKteju4; 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 S1751438AbeAIIcC (ORCPT + 28 others); Tue, 9 Jan 2018 03:32:02 -0500 Received: from conuserg-10.nifty.com ([210.131.2.77]:27299 "EHLO conuserg-10.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751127AbeAIIb7 (ORCPT ); Tue, 9 Jan 2018 03:31:59 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id w098Uugs024960; Tue, 9 Jan 2018 17:30:56 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com w098Uugs024960 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1515486656; bh=eII+qy4k8i367HbCbA4Q0X4SPtggcoLkPrjaGg5QK9M=; h=From:To:Cc:Subject:Date:From; b=UNKteju4bWDu9YGtQhjHM7qI/FGGF2ZDBT9ZiPS/+S7t3oHjwko73VC2tMWyHzGnV SRi2dYjq5VL9kx4kaz6gykYEx+nO4fUrzja9OL/isVDDFI1qj7HbrI11Dqih5xJlNU 5zy4xrF/6gO+WSSe5QBp/QgIvSv/shk594CoQ1LdGSoctlpGR4aE4Nj+TzMdt+W7Mn FaNWPKYysljlu66LQs5ulXJOB/1nMtqtUNL2fS39kLvGGLd7pbPoRrkhPIOgP7LOyR p38hout0wI3M/QV3toodtLxVKsCHpnJDJehOb2sEZm1IpblPawPB7Krd0WyU9NATEJ G8HVDxsvQZo/A== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: Sam Ravnborg , Michal Marek , Lukas Bulwahn , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH 1/6] fixdep: use malloc() and read() to load dep_file to buffer Date: Tue, 9 Jan 2018 17:30:45 +0900 Message-Id: <1515486650-1141-1-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 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 load files in which CONFIG options are searched. It used malloc() and read() instead of mmap() because it needed to zero-terminate the buffer 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, map 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 --- scripts/basic/fixdep.c | 55 +++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) -- 2.7.4 diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index 86a61d6..7efbeb7 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -309,24 +309,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 */ @@ -374,6 +377,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. @@ -392,40 +399,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[]) @@ -441,7 +450,7 @@ int main(int argc, char *argv[]) cmdline = argv[3]; print_cmdline(); - print_deps(); + print_deps(depfile); return 0; }