]> git.neil.brown.name Git - wiggle.git/commitdiff
Introduce xmalloc and use it.
authorNeil Brown <neilb@suse.de>
Mon, 7 May 2012 21:59:27 +0000 (07:59 +1000)
committerNeil Brown <neilb@suse.de>
Mon, 7 May 2012 21:59:27 +0000 (07:59 +1000)
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 <neilb@suse.de>
bestmatch.c
diff.c
extract.c
load.c
merge2.c
patch_depends.c
split.c
vpatch.c
wiggle.c
wiggle.h

index ce1ed0559ada74ab10d5e5d73031b5872335d8ba..70cde6c169612656879ca7cf5fb0c6b41e08c13c 100644 (file)
@@ -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 f664d573a42fbadd8e5e44550a70db5a33493e6e..239d4099db67ac14aa4bc80c6cf70f845d03dc17 100644 (file)
--- 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;
index 5cb8ad239008477892db760cc7dc9c3f450ef0a4..ba81ec0282436ed7276c193b3f4a33da19594a30 100644 (file)
--- 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 5978dd749ab45415fbf78030e3b138e0588e1276..6d691fe072e1eda1af646590bf92746f971594c8 100644 (file)
--- 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();
index a7945e2b156e5eeded107286324b2e3f101a8c9f..504ee792e879b3960ddb23e310a15682e940df48 100644 (file)
--- 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;
index 86724ea42f32ea0f096cc1a420299034b0dec66d..9b3600c7fe1b5453b576a1365fe7223d8367c332 100644 (file)
@@ -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 bce8a13d1730f5aa5a551f133bead021653849d9..9455768f08fa7b98a868f1c471533e3368ce681a 100644 (file)
--- 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;
index fbd6bf78bea31a7e2098b82cade453f44c096997..4d41aa70f8100fa3ed5d26ee13875152fca21f05 100644 (file)
--- 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;
 }
index a103cbf5333f332bc07613664026c0ebe4c9c9ed..77e0082d1032e0240571184776e78b3fcf5a1661 100644 (file)
--- 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");
index 53b001fa061c6ae967f8db743431c36b9ad45198..cc3780155df91af13bccafea33b9a751bba15ce5 100644 (file)
--- 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);