From: Neil Brown Date: Mon, 7 May 2012 21:59:27 +0000 (+1000) Subject: Introduce xmalloc and use it. X-Git-Tag: v0.9~38 X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=e5c1505a466f5f5ec261a6ef72c9540922d1ba29;p=wiggle.git Introduce xmalloc and use it. Instead of sometimes calling die() when malloc fails and sometimes not, introduce xmalloc() which always reports a message and dies, and use it uniformly. Signed-off-by: NeilBrown --- diff --git a/bestmatch.c b/bestmatch.c index ce1ed05..70cde6c 100644 --- a/bestmatch.c +++ b/bestmatch.c @@ -176,7 +176,7 @@ static void find_best(struct file *a, struct file *b, int klo, khi, k; int f; - struct v *valloc = malloc(sizeof(struct v)*((ahi-alo)+(bhi-blo)+5)); + struct v *valloc = xmalloc(sizeof(struct v)*((ahi-alo)+(bhi-blo)+5)); struct v *v = valloc + (bhi-alo+2); k = klo = khi = alo-blo; @@ -289,7 +289,7 @@ static struct csl *csl_join(struct csl *c1, struct csl *c2) cnt++; for (c = c2; c->len; c++) cnt++; - cd = rv = malloc(sizeof(*rv)*cnt); + cd = rv = xmalloc(sizeof(*rv)*cnt); for (c = c1; c->len; c++) *cd++ = *c; for (c = c2; c->len; c++) @@ -331,7 +331,7 @@ static struct file reduce(struct file orig) return orig; rv.elcnt = cnt; - rv.list = malloc(cnt*sizeof(struct elmnt)); + rv.list = xmalloc(cnt*sizeof(struct elmnt)); cnt = 0; for (i = 0; i < orig.elcnt; i++) if (!is_skipped(orig.list[i])) @@ -465,7 +465,7 @@ static void find_best_inorder(struct file *a, struct file *b, struct csl *pdiff(struct file a, struct file b, int chunks) { struct csl *csl1, *csl2; - struct best *best = malloc(sizeof(struct best)*(chunks+1)); + struct best *best = xmalloc(sizeof(struct best)*(chunks+1)); int i; struct file asmall, bsmall; @@ -493,7 +493,7 @@ struct csl *pdiff(struct file a, struct file b, int chunks) csl2->a = a.elcnt; csl2->b = b.elcnt; } else { - csl1 = malloc(sizeof(*csl1)); + csl1 = xmalloc(sizeof(*csl1)); csl1->len = 0; csl1->a = a.elcnt; csl1->b = b.elcnt; diff --git a/diff.c b/diff.c index f664d57..239d409 100644 --- a/diff.c +++ b/diff.c @@ -353,7 +353,7 @@ static struct csl *lcsl(struct file *a, int alo, int ahi, if (csl == NULL) { /* 'len+1' to hold a sentinel */ - rv = csl = malloc((len+1)*sizeof(*csl)); + rv = csl = xmalloc((len+1)*sizeof(*csl)); csl->len = 0; } if (len) { @@ -541,7 +541,7 @@ struct csl *diff(struct file a, struct file b) { struct v *v; struct csl *csl; - v = malloc(sizeof(struct v)*(a.elcnt+b.elcnt+2)); + v = xmalloc(sizeof(struct v)*(a.elcnt+b.elcnt+2)); v += b.elcnt+1; csl = lcsl(&a, 0, a.elcnt, @@ -550,7 +550,7 @@ struct csl *diff(struct file a, struct file b) free(v-(b.elcnt+1)); fixup(&a, &b, csl); if (!csl) { - csl = malloc(sizeof(*csl)); + csl = xmalloc(sizeof(*csl)); csl->len = 0; csl->a = a.elcnt; csl->b = b.elcnt; @@ -566,7 +566,7 @@ struct csl *diff_partial(struct file a, struct file b, { struct v *v; struct csl *csl; - v = malloc(sizeof(struct v)*(ahi-alo+bhi-blo+2)); + v = xmalloc(sizeof(struct v)*(ahi-alo+bhi-blo+2)); v += bhi-alo+1; csl = lcsl(&a, alo, ahi, @@ -583,7 +583,7 @@ main(int argc, char *argv[]) { struct file a, b; struct csl *csl; - struct elmnt *lst = malloc(argc*sizeof(*lst)); + struct elmnt *lst = xmalloc(argc*sizeof(*lst)); int arg; struct v *v; int ln; diff --git a/extract.c b/extract.c index 5cb8ad2..ba81ec0 100644 --- a/extract.c +++ b/extract.c @@ -70,11 +70,8 @@ int split_patch(struct stream f, struct stream *f1, struct stream *f2) f1->body = f2->body = NULL; - r1.body = malloc(f.len); - r2.body = malloc(f.len); - if (!r1.body || !r2.body) - die(); - + r1.body = xmalloc(f.len); + r2.body = xmalloc(f.len); r1.len = r2.len = 0; cp = f.body; @@ -205,12 +202,9 @@ int split_merge(struct stream f, struct stream *f1, struct stream *f2, struct st f1->body = NULL; f2->body = NULL; - r1.body = malloc(f.len); - r2.body = malloc(f.len); - r3.body = malloc(f.len); - if (!r1.body || !r2.body || !r3.body) - die(); - + r1.body = xmalloc(f.len); + r2.body = xmalloc(f.len); + r3.body = xmalloc(f.len); r1.len = r2.len = r3.len = 0; cp = f.body; diff --git a/load.c b/load.c index 5978dd7..6d691fe 100644 --- a/load.c +++ b/load.c @@ -75,15 +75,10 @@ static struct stream load_regular(int fd) fstat(fd, &stb); s.len = stb.st_size; - s.body = malloc(s.len+1); - if (s.body) { - if (read(fd, s.body, s.len) != s.len) { - free(s.body); - s.body = NULL; - die(); - } - } else + s.body = xmalloc(s.len+1); + if (read(fd, s.body, s.len) != s.len) die(); + s.body[s.len] = 0; return s; } @@ -95,9 +90,7 @@ static struct stream load_other(int fd) int i = 0; while (1) { - list[i].body = malloc(8192); - if (!list[i].body) - die(); + list[i].body = xmalloc(8192); list[i].len = read(fd, list[i].body, 8192); if (list[i].len < 0) die(); diff --git a/merge2.c b/merge2.c index a7945e2..504ee79 100644 --- a/merge2.c +++ b/merge2.c @@ -236,9 +236,7 @@ struct ci make_merger(struct file af, struct file bf, struct file cf, /* maybe a bit of slack at each end */ l = l * 4 + 10; - rv.merger = malloc(sizeof(struct merge)*l); - if (!rv.merger) - return rv; + rv.merger = xmalloc(sizeof(struct merge)*l); a = b = c = c1 = c2 = 0; i = 0; diff --git a/patch_depends.c b/patch_depends.c index 86724ea..9b3600c 100644 --- a/patch_depends.c +++ b/patch_depends.c @@ -76,7 +76,7 @@ void add_depends(struct patch *new, struct patch *old) void add_chunk(struct patch *p, struct file *f, int os, int oe, int ns, int ne) { - struct chunk *c = malloc(sizeof(struct chunk)); + struct chunk *c = xmalloc(sizeof(struct chunk)); c->patch = p; c->file = f; c->old_start = os; diff --git a/split.c b/split.c index bce8a13..9455768 100644 --- a/split.c +++ b/split.c @@ -111,9 +111,7 @@ struct file split_stream(struct stream s, int type) c = s.body; cnt = split_internal(c, end, type, NULL); - f.list = malloc(cnt*sizeof(struct elmnt)); - if (!f.list) - die(); + f.list = xmalloc(cnt*sizeof(struct elmnt)); f.elcnt = split_internal(c, end, type, f.list); return f; diff --git a/vpatch.c b/vpatch.c index fbd6bf7..4d41aa7 100644 --- a/vpatch.c +++ b/vpatch.c @@ -1663,7 +1663,7 @@ static void merge_window(struct plist *p, FILE *f, int reverse) !same_mpos(anchor->pos, pos) || anchor->searchlen != searchlen || anchor->col != col) { - struct search_anchor *a = malloc(sizeof(*a)); + struct search_anchor *a = xmalloc(sizeof(*a)); a->pos = pos; a->row = row; a->col = col; @@ -1730,7 +1730,7 @@ static struct plist *patch_add_file(struct plist *pl, int *np, char *file, asize += asize; npl = realloc(pl, asize * sizeof(struct plist)); if (!npl) { - fprintf(stderr, "malloc failed - skipping %s\n", file); + fprintf(stderr, "realloc failed - skipping %s\n", file); return pl; } pl = npl; @@ -1827,14 +1827,9 @@ static struct stream load_segment(FILE *f, { struct stream s; s.len = end - start; - s.body = malloc(s.len); - if (s.body) { - fseek(f, start, 0); - if (fread(s.body, 1, s.len, f) != (size_t)s.len) { - free(s.body); - s.body = NULL; - } - } else + s.body = xmalloc(s.len); + fseek(f, start, 0); + if (fread(s.body, 1, s.len, f) != (size_t)s.len) die(); return s; } diff --git a/wiggle.c b/wiggle.c index a103cbf..77e0082 100644 --- a/wiggle.c +++ b/wiggle.c @@ -98,6 +98,17 @@ void die() exit(3); } +void *xmalloc(int size) +{ + void *rv = malloc(size); + if (size && !rv) { + char *msg = "Failed to allocate memory - aborting\n"; + write(2, msg, strlen(msg)); + exit(3); + } + return rv; +} + void printword(FILE *f, struct elmnt e) { if (e.start[0]) @@ -501,12 +512,8 @@ static int do_merge(int argc, char *argv[], int obj, } if (replace) { int fd; - replacename = malloc(strlen(argv[optind]) + 20); - if (!replacename) - die(); - orignew = malloc(strlen(argv[optind]) + 20); - if (!orignew) - die(); + replacename = xmalloc(strlen(argv[optind]) + 20); + orignew = xmalloc(strlen(argv[optind]) + 20); strcpy(replacename, argv[optind]); strcpy(orignew, argv[optind]); strcat(orignew, ".porig"); diff --git a/wiggle.h b/wiggle.h index 53b001f..cc37801 100644 --- a/wiggle.h +++ b/wiggle.h @@ -144,6 +144,7 @@ extern struct ci make_merger(struct file a, struct file b, struct file c, int ignore_already); extern void die(void); +extern void *xmalloc(int len); extern int vpatch(int argc, char *argv[], int patch, int strip, int reverse, int replace);