From: WANG Cong <xiyou.wangcong@gmail.com>
To: Tejun Heo <htejun@gmail.com>
Cc: sam@ravnborg.org, Linux Kernel <linux-kernel@vger.kernel.org>,
notting@redhat.com, rusty@rustcorp.com.au, kay.sievers@vrfy.org,
greg@kroah.com
Subject: Re: [PATCH] kbuild: implement modules.order
Date: Tue, 4 Dec 2007 23:07:34 +0800 [thread overview]
Message-ID: <20071204150734.GE6113@hacking> (raw)
In-Reply-To: <47555AF1.8090304@gmail.com>
{snip}
Comments on your C code below.
>--- /dev/null
>+++ b/scripts/remove-dup.c
>@@ -0,0 +1,98 @@
>+/*
>+ * remove-dup - Drop duplicate lines from unsorted input files
>+ *
>+ * Dec 2007 Tejun Heo <teheo@suse.de>
>+ *
>+ * This software is released under GPLv2.
>+ */
>+
>+#include <stdio.h>
>+#include <string.h>
>+#include <stdlib.h>
>+
>+struct hash_ent {
>+ struct hash_ent *next;
>+ char str[];
>+};
>+
>+#define fatal(fmt, args...) do { \
>+ fprintf(stderr, fmt , ##args); \
>+ exit(1); \
>+ } while (0)
>+
>+static inline unsigned int sdb_hash(const char *str)
>+{
>+ unsigned int hash = 0;
>+ int c;
>+
>+ while ((c = *str++))
Maybe ` while ((c = *str++) != '\0') ` is better. ;)
>+ hash = c + (hash << 6) + (hash << 16) - hash;
>+
>+ return hash;
>+}
>+
>+int main(int argc, char **argv)
>+{
>+ unsigned int nr_entries = 0;
>+ struct hash_ent **hash_tbl;
>+ char line[10240];
Needs to #define the magic number?
>+ int i;
>+
>+ /* first pass, count lines */
>+ for (i = 1; i < argc; i++) {
>+ FILE *fp = fopen(argv[i], "r");
>+
>+ if (!fp)
>+ fatal("failed to open %s for reading\n", argv[i]);
>+
>+ while (fgets(line, sizeof(line), fp))
>+ nr_entries++;
>+
>+ fclose(fp);
>+ }
>+
>+ nr_entries = nr_entries * 10 / 8;
>+ hash_tbl = calloc(nr_entries, sizeof(struct hash_ent *));
>+ if (!hash_tbl)
>+ fatal("failed to allocate hash table for %u entries\n",
>+ nr_entries);
>+
>+ /* second pass, hash and print unique lines */
>+ for (i = 1; i < argc; i++) {
>+ FILE *fp = fopen(argv[i], "r");
>+
>+ if (!fp)
>+ fatal("failed to open %s for reading\n", argv[i]);
>+
>+ while (fgets(line, sizeof(line), fp)) {
>+ int len = strlen(line);
strlen returns 'size_t', which is unsigned.
>+ struct hash_ent **ppos, *new_ent;
>+
>+ if (line[len - 1] == '\n')
>+ line[--len] = '\0';
>+
>+ ppos = hash_tbl + (sdb_hash(line) % nr_entries);
>+ while (*ppos) {
>+ if (strcmp((*ppos)->str, line) == 0)
>+ break;
>+ ppos = &(*ppos)->next;
>+ }
>+ if (*ppos)
>+ continue;
>+
>+ new_ent = malloc(sizeof(struct hash_ent) + len + 1);
>+ if (!new_ent)
>+ fatal("failed to allocate hash entry\n");
>+ new_ent->next = NULL;
>+ memcpy(new_ent->str, line, len + 1);
>+
>+ *ppos = new_ent;
>+
>+ printf("%s\n", line);
>+ }
>+
>+ fclose(fp);
>+ }
>+
I think, you forgot to free(3) the memory you calloc(3)'ed and
malloc(3)'ed above.
>+ return 0;
>+}
Thanks!
Cong
next prev parent reply other threads:[~2007-12-04 15:11 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-04 13:49 [PATCH] kbuild: implement modules.order Tejun Heo
2007-12-04 13:55 ` [PATCH] depmod: sort output according to modules.order Tejun Heo
2007-12-05 7:25 ` Sam Ravnborg
2007-12-05 7:33 ` Tejun Heo
2007-12-05 7:34 ` Tejun Heo
2007-12-05 19:06 ` Sam Ravnborg
2007-12-05 23:28 ` Tejun Heo
2007-12-06 22:37 ` Sam Ravnborg
2007-12-07 0:59 ` Tejun Heo
2007-12-07 5:14 ` Sam Ravnborg
2007-12-08 8:09 ` Jon Masters
2007-12-08 12:39 ` Alan Cox
2007-12-08 8:03 ` Jon Masters
2007-12-08 8:19 ` Jon Masters
2007-12-09 5:48 ` Tejun Heo
2007-12-04 15:07 ` WANG Cong [this message]
2007-12-04 15:21 ` [PATCH] kbuild: implement modules.order Tejun Heo
2007-12-05 7:01 ` WANG Cong
2007-12-05 7:11 ` Tejun Heo
2007-12-05 7:22 ` Li Zefan
2007-12-06 3:02 ` Rusty Russell
2007-12-07 17:48 ` Adrian Bunk
2007-12-07 23:59 ` Tejun Heo
2007-12-08 14:28 ` Adrian Bunk
2007-12-09 5:44 ` Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071204150734.GE6113@hacking \
--to=xiyou.wangcong@gmail.com \
--cc=greg@kroah.com \
--cc=htejun@gmail.com \
--cc=kay.sievers@vrfy.org \
--cc=linux-kernel@vger.kernel.org \
--cc=notting@redhat.com \
--cc=rusty@rustcorp.com.au \
--cc=sam@ravnborg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.