From: Davidlohr Bueso <dave@xxxxxxx> Date: Fri, 22 Oct 2010 12:29:42 -0300 Subject: [PATCH 7/7] hexdump: use xalloc for memory allocation. Continue to use xmalloc/memset combos instead of xcalloc because malloc is simply faster, although it shouldn't really matter too much for a program like this. Signed-off-by: Davidlohr Bueso <dave@xxxxxxx> --- text-utils/column.c | 55 +++++++++++++++++++++++-------------------------- text-utils/display.c | 23 +++++--------------- text-utils/hexdump.h | 1 - text-utils/parse.c | 18 ++++++++++----- 4 files changed, 44 insertions(+), 53 deletions(-) diff --git a/text-utils/column.c b/text-utils/column.c index 87a6fc7..56be6d6 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -50,8 +50,9 @@ #include <err.h> #include <errno.h> #include <getopt.h> -#include "nls.h" +#include "nls.h" +#include "xalloc.h" #include "widechar.h" #ifdef HAVE_WIDECHAR @@ -70,7 +71,6 @@ static char *mtsafe_strtok(char *, const char *, char **); #define MAXLINELEN (LINE_MAX + 1) static void c_columnate __P((void)); -static void *emalloc __P((int)); static void input __P((FILE *)); static void maketbl __P((void)); static void print __P((void)); @@ -274,27 +274,34 @@ maketbl() wchar_t **cols; wchar_t *wcstok_state; - t = tbl = emalloc(entries * sizeof(TBL)); - cols = emalloc((maxcols = DEFCOLS) * sizeof(wchar_t *)); - lens = emalloc(maxcols * sizeof(int)); + t = xmalloc(entries * sizeof(TBL)); + memset(t, 0, entries * sizeof(TBL)); + tbl = xmalloc(entries * sizeof(TBL)); + memset(tbl, 0, entries * sizeof(TBL)); + cols = xmalloc((maxcols = DEFCOLS) * sizeof(wchar_t *)); + memset(cols, 0, (maxcols = DEFCOLS) * sizeof(wchar_t *)); + lens = xmalloc(maxcols * sizeof(int)); + memset(cols, 0, maxcols * sizeof(wchar_t *)); + for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) { for (coloff = 0, p = *lp; (cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL; p = NULL) { if (++coloff == maxcols) { - cols = realloc(cols, ((u_int)maxcols + DEFCOLS) - * sizeof(wchar_t *)); - lens = realloc(lens, ((u_int)maxcols + DEFCOLS) - * sizeof(int)); - if (!cols || !lens) - err(EXIT_FAILURE, _("out of memory?")); - memset((char *)lens + maxcols * sizeof(int), - 0, DEFCOLS * sizeof(int)); + cols = xrealloc(cols, ((u_int)maxcols + DEFCOLS) + * sizeof(wchar_t *)); + lens = xrealloc(lens, ((u_int)maxcols + DEFCOLS) + * sizeof(int)); + memset(lens + maxcols * sizeof(int), + 0, DEFCOLS * sizeof(int)); maxcols += DEFCOLS; } } - t->list = emalloc(coloff * sizeof(wchar_t *)); - t->len = emalloc(coloff * sizeof(int)); + t->list = xmalloc(coloff * sizeof(wchar_t *)); + memset(t->list, 0, coloff * sizeof(wchar_t *)); + t->len = xmalloc(coloff * sizeof(int)); + memset(t->len, 0, coloff * sizeof(int)); + for (t->cols = coloff; --coloff >= 0;) { t->list[coloff] = cols[coloff]; t->len[coloff] = wcs_width(cols[coloff]); @@ -321,8 +328,10 @@ input(fp) int len, lineno = 1, reportedline = 0; wchar_t *p, buf[MAXLINELEN]; - if (!list) - list = emalloc((maxentry = DEFNUM) * sizeof(wchar_t *)); + if (!list) { + list = xmalloc((maxentry = DEFNUM) * sizeof(wchar_t *)); + memset(list, 0, (maxentry = DEFNUM) * sizeof(wchar_t *)); + } while (fgetws(buf, MAXLINELEN, fp)) { for (p = buf; *p && iswspace(*p); ++p); if (!*p) @@ -393,15 +402,3 @@ static char *mtsafe_strtok(char *str, const char *delim, char **ptr) } } #endif - -static void * -emalloc(size) - int size; -{ - char *p; - - if (!(p = malloc(size))) - err(EXIT_FAILURE, _("out of memory?")); - memset(p, 0, size); - return (p); -} diff --git a/text-utils/display.c b/text-utils/display.c index 7a35e46..5e3221a 100644 --- a/text-utils/display.c +++ b/text-utils/display.c @@ -39,6 +39,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + +#include "xalloc.h" #include "hexdump.h" static void doskip(const char *, int); @@ -232,8 +234,10 @@ get(void) u_char *tmpp; if (!curp) { - curp = emalloc(blocksize); - savp = emalloc(blocksize); + curp = xmalloc(blocksize); + memset(curp, 0, blocksize); + savp = xmalloc(blocksize); + memset(savp, 0, blocksize); } else { tmpp = curp; curp = savp; @@ -352,18 +356,3 @@ doskip(const char *fname, int statok) address += skip; skip = 0; } - -void * -emalloc(int sz) { - void *p; - - if (!(p = malloc((u_int)sz))) - nomem(); - memset(p, 0, sz); - return(p); -} - -void nomem() { - (void)fprintf(stderr, "hexdump: %s.\n", strerror(errno)); - exit(1); -} diff --git a/text-utils/hexdump.h b/text-utils/hexdump.h index f9e963b..23b39e0 100644 --- a/text-utils/hexdump.h +++ b/text-utils/hexdump.h @@ -81,7 +81,6 @@ extern off_t skip; /* bytes to skip */ enum _vflag { ALL, DUP, FIRST, WAIT }; /* -v values */ extern enum _vflag vflag; -void *emalloc(int); int size(FS *); void add(const char *); void rewrite(FS *); diff --git a/text-utils/parse.c b/text-utils/parse.c index 8164c60..bb0b99b 100644 --- a/text-utils/parse.c +++ b/text-utils/parse.c @@ -41,7 +41,9 @@ #include <stdlib.h> #include <ctype.h> #include <string.h> + #include "hexdump.h" +#include "xalloc.h" #include "nls.h" static void escape(char *p1); @@ -87,7 +89,8 @@ void add(const char *fmt) const char *savep; /* Start new linked list of format units. */ - tfs = emalloc(sizeof(FS)); + tfs = xmalloc(sizeof(FS)); + memset(tfs, 0, sizeof(FS)); if (!fshead) fshead = tfs; else @@ -103,7 +106,8 @@ void add(const char *fmt) break; /* Allocate a new format unit and link it in. */ - tfu = emalloc(sizeof(FU)); + tfu = xmalloc(sizeof(FU)); + memset(tfu, 0, sizeof(FU)); *nextfu = tfu; nextfu = &tfu->nextfu; tfu->reps = 1; @@ -140,8 +144,8 @@ void add(const char *fmt) for (savep = ++p; *p != '"';) if (*p++ == 0) badfmt(fmt); - if (!(tfu->fmt = malloc(p - savep + 1))) - nomem(); + tfu->fmt = xmalloc(p - savep + 1); + (void) strncpy(tfu->fmt, savep, p - savep); tfu->fmt[p - savep] = '\0'; escape(tfu->fmt); @@ -221,7 +225,8 @@ void rewrite(FS *fs) * conversion character gets its own. */ for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) { - pr = emalloc(sizeof(PR)); + pr = xmalloc(sizeof(PR)); + memset(pr, 0, sizeof(PR)); if (!fu->nextpr) fu->nextpr = pr; else @@ -388,7 +393,8 @@ isint2: switch(fu->bcnt) { */ savech = *p2; p1[0] = '\0'; - pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1); + pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1); + memset(pr->fmt, 0, strlen(fmtp) + strlen(cs) + 1); (void)strcpy(pr->fmt, fmtp); (void)strcat(pr->fmt, cs); *p2 = savech; -- 1.7.0.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html