PERFORCE change 174343 for review
Gabor Kovesdan
gabor at FreeBSD.org
Fri Feb 5 04:51:30 UTC 2010
http://p4web.freebsd.org/chv.cgi?CH=174343
Change 174343 by gabor at gabor_aspire on 2010/02/05 04:51:22
- Remove custom merge sort; use qsort() instead of reinventing the
wheel
- Remove swapping to disk; we have to choose between performance or
memory consumption and we prefer the former
- Make it WARNS=6 clean
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/newsort/Makefile#2 edit
.. //depot/projects/soc2008/gabor_textproc/newsort/coll.c#11 edit
.. //depot/projects/soc2008/gabor_textproc/newsort/file.c#4 edit
.. //depot/projects/soc2008/gabor_textproc/newsort/msort.c#3 delete
.. //depot/projects/soc2008/gabor_textproc/newsort/sort.c#5 edit
.. //depot/projects/soc2008/gabor_textproc/newsort/sort.h#3 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/newsort/Makefile#2 (text+ko) ====
@@ -1,7 +1,9 @@
PROG= sort
-SRCS= coll.c file.c mem.c msort.c sort.c
+SRCS= coll.c file.c mem.c sort.c
+
+WARNS= 6
-CFLAGS+= -std=c99 -Wall -pedantic
+CFLAGS+= --param max-inline-insns-single=64
.if !defined(WITHOUT_NLS)
NLS+= hu_HU.ISO8859-2
==== //depot/projects/soc2008/gabor_textproc/newsort/coll.c#11 (text+ko) ====
@@ -107,7 +107,7 @@
*preproc(const wchar_t *s) {
wchar_t *ret, *sp, *ep;
size_t len;
- int i;
+ unsigned int i;
if (kflag && (sfield > 1)) {
sp = wcschr(s, field_sep);
@@ -150,11 +150,13 @@
* the underlying collate functions, which done the actual comparison.
*/
int
-coll(const wchar_t *s1, const wchar_t *s2) {
- wchar_t *ps1, *ps2;
+coll(const void *s1, const void *s2) {
+ const wchar_t *const *ss1 = (const wchar_t * const *)s1;
+ const wchar_t *const *ss2 = (const wchar_t * const *)s2;
+ wchar_t *ps1, *ps2;
- ps1 = preproc(s1);
- ps2 = preproc(s2);
+ ps1 = preproc(*ss1);
+ ps2 = preproc(*ss2);
if (rflag) {
wchar_t *tmp = ps1;
@@ -324,7 +326,7 @@
len = wcslen(s);
ls = sort_malloc(sizeof(wint_t) * (len + 1));
- for (int i = 0; i < len; i++)
+ for (unsigned int i = 0; i < len; i++)
ls[i] = towlower(s[i]);
if (months == NULL) {
@@ -340,7 +342,7 @@
continue;
}
m[len] = L'\0';
- for (int j = 0; j < len; j++)
+ for (unsigned int j = 0; j < len; j++)
m[j] = towlower(m[j]);
months[i] = m;
}
==== //depot/projects/soc2008/gabor_textproc/newsort/file.c#4 (text+ko) ====
@@ -37,27 +37,11 @@
#include "sort.h"
/*
- * Prints the internal buffer to a file or to stdout if
- * "-" is given as output filename.
- */
-void
-print(char *fn) {
- FILE *file = NULL;
-
- file = openfile(fn, "w");
-
- for (int i = 0; i < count; i++) {
- fprintf(file, "%ls%s", list[i], zflag ? "\0" : "\n");
- }
- fclose(file);
-}
-
-/*
* Checks if the given file is sorted. Stops at the first disorder,
* prints the disordered line and returns 1.
*/
int
-check(char *fn) {
+check(const char *fn) {
int pos = 1;
wchar_t *s1, *s2, *tmp;
FILE *file;
@@ -94,19 +78,10 @@
* opened.
*/
FILE
-*openfile(char *fn, char *mode) {
+*openfile(const char *fn, const char *mode) {
FILE *file;
- int len, fd;
- if (fn == NULL) {
- tempfiles = sort_realloc(tempfiles, sizeof(char *) * ++tempno);
- len = strlen(tmpdir) + strlen(TMPPAT);
- tempfiles[tempno - 1] = sort_malloc(sizeof(char) * (len + 1));
- sprintf(tempfiles[tempno - 1], "%s" TMPPAT, tmpdir);
- fd = mkstemp(tempfiles[tempno - 1]);
- if ((file = fdopen(fd, "w")) == NULL)
- err(2, NULL);
- } else if (strcmp(fn, "-") == 0) {
+ if (strcmp(fn, "-") == 0) {
return (stdout);
} else {
if((file = fopen(fn, mode)) == NULL)
@@ -132,35 +107,12 @@
list = sort_realloc(list, sizeof(struct qentry*) * ++listlen);
}
list[count++] = item;
- if ((bufcnt -= sizeof(item)) < 0)
- return (1);
}
fclose (file);
return (0);
}
/*
- * Calculates the optimal buffer size.
- */
-int
-optbufsize(char *fn) {
- struct stat st;
-
- if (bufsiz > 0)
- return (bufsiz);
-
- if (stat(fn, &st) != 0)
- err(2, NULL);
-
- if (st.st_size/OPT_SLICES < OPT_BUFSIZE)
- return (OPT_BUFSIZE);
- else if (st.st_size/OPT_SLICES > MAX_BUFSIZE)
- return (MAX_BUFSIZE);
- else
- return (st.st_size/OPT_SLICES);
-}
-
-/*
* Prints the content of f1 to f2, which can also be
* stdout.
*/
@@ -183,7 +135,7 @@
* stdout.
*/
void
-merge_files(char *fn1, char *fn2, char *fn3) {
+merge_files(const char *fn1, const char *fn2, const char *fn3) {
FILE *f1, *f2, *f3;
wchar_t *s1, *s2, *tmp;
size_t len;
==== //depot/projects/soc2008/gabor_textproc/newsort/sort.c#5 (text+ko) ====
@@ -51,7 +51,7 @@
* Default messages to use when NLS is disabled or no catalogue
* is found.
*/
-char *nlsstr[] = {
+const char *nlsstr[] = {
"",
/* 1*/ "you cannot use -%c and -%c together",
/* 2*/ "extra argument not allowed with -c"
@@ -60,11 +60,9 @@
extern char *__progname;
-int count, bufcnt, listlen = 0, tempno = 0;
+int count, listlen = 0;
wchar_t **list;
wchar_t field_sep = L' ';
-char **tempfiles;
-char *tmpdir = "/var/tmp";
unsigned long sfield, lfield;
unsigned long long bufsiz = 0;
bool cflag;
@@ -100,7 +98,7 @@
{"reverse", no_argument, NULL, 'r'},
{"buffer-size", required_argument, NULL, 'S'},
{"field-separator", required_argument, NULL, 't'},
- {"temporary-directory", required_argument, NULL, 'T'},
+// {"temporary-directory", required_argument, NULL, 'T'},
{"unique", no_argument, NULL, 'u'},
{"version", no_argument, NULL, 'V'},
{"zero-terminated", no_argument, NULL, 'z'},
@@ -118,22 +116,12 @@
main(int argc, char *argv[]) {
char c;
char *sptr;
- char *outfile = "-";
- struct stat st;
+ char *outfile;
+
+ outfile = strdup("-");
setlocale(LC_ALL, "");
- if ((sptr = getenv("TMPDIR")) != NULL) {
- stat(sptr, &st);
-
- if (!S_ISDIR(st.st_mode)) {
- errno = ENOTDIR;
- err(2, NULL);
- }
-
- tmpdir = sptr;
- }
-
while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL)) != -1)) {
switch (c) {
case 'b':
@@ -226,17 +214,6 @@
printf("field_sep = %lc\n", field_sep);
}
break;
- case 'T':
- stat(optarg, &st);
-
- if (!S_ISDIR(st.st_mode)) {
- errno = ENOTDIR;
- err(2, NULL);
- }
-
- tmpdir = sort_malloc(sizeof(char) * (strlen(optarg) + 2));
- strlcpy(tmpdir, optarg, strlen(optarg) + 1);
- break;
case 'u':
uflag = true;
break;
@@ -287,44 +264,15 @@
FILE *file;
file = openfile(*argv, "r");
- bufcnt = optbufsize(*argv);
- while (procfile(file)) {
- list = merge_sort(list, count);
- print(NULL);
- file = openfile(*argv, "r");
- for (int i = 0; i < count; i++)
- free(list[i]);
- free(list);
- count = 0;
- }
- list = merge_sort(list, count);
- print(NULL);
+ procfile(file);
--argc;
++argv;
}
- if (tempno == 0) {
- list = merge_sort(list, count);
- print(outfile);
- } else {
- for (int i = 0; i < tempno;) {
- if (i == (tempno - 2)) {
- merge_files(tempfiles[i], tempfiles[i + 1], "-");
- unlink(tempfiles[i]);
- unlink(tempfiles[i + 1]);
- break;
- } else if ( i == (tempno - 1)) {
- printfile(openfile(tempfiles[i], "r"), stdout);
- unlink(tempfiles[i]);
- break;
- } else {
- merge_files(tempfiles[i], tempfiles[i + 1], NULL);
- unlink(tempfiles[i]);
- unlink(tempfiles[i + 1]);
- i += 2;
- }
- }
- }
+ qsort(list, count, sizeof(wchar_t *), coll);
+
+ for (int i = 0; i < count; i++)
+ printf("%ls\n", list[i]);
return (0);
} else if (cflag) {
==== //depot/projects/soc2008/gabor_textproc/newsort/sort.h#3 (text+ko) ====
@@ -43,7 +43,7 @@
#define getstr(n) catgets(catalog, 1, n, nlsstr[n])
#endif
-extern char *nlsstr[];
+extern const char *nlsstr[];
/*
* sort.c
@@ -59,16 +59,14 @@
/*
* coll.c
*/
-int coll(const wchar_t *, const wchar_t *);
+int coll(const void *, const void *);
/*
* file.c
*/
-int check(char *);
-void merge_files(char *, char *, char *);
-FILE *openfile(char *, char *);
-int optbufsize(char *);
-void print(char *);
+int check(const char *);
+void merge_files(const char *, const char *, const char *);
+FILE *openfile(const char *, const char *);
void printfile(FILE *, FILE *);
int procfile(FILE *);
More information about the p4-projects
mailing list