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;
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++)
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]))
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;
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;
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) {
{
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,
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;
{
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,
{
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;
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;
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;
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;
}
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();
/* 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;
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;
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;
!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;
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;
{
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;
}
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])
}
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");
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);