Unlimited mode is now usable in a meaningful timeframe!

Modify the sliding mmap window to have a 64k smaller buffer which matches the size of the search size, and change the larger lower buffer to make it slide with the main hash search progress. This makes for a MUCH faster unlimited mode, making it actually usable.
Limit windows to 2GB again on 32 bit, but do it when determining the largest size possible in rzip.c.
Implement a linux-kernel like unlikely() wrapper for inbuilt expect, and modify most fatal warnings to be unlikely, and a few places where it's also suitable.
Minor cleanups.
This commit is contained in:
Con Kolivas 2010-11-05 12:16:43 +11:00
parent 040b1d416c
commit 296534921a
6 changed files with 249 additions and 223 deletions

81
main.c
View file

@ -67,7 +67,7 @@ static void write_magic(int fd_in, int fd_out)
magic[4] = LRZIP_MAJOR_VERSION;
magic[5] = LRZIP_MINOR_VERSION;
if (fstat(fd_in, &st) != 0)
if (unlikely(fstat(fd_in, &st)))
fatal("bad magic file descriptor!?\n");
memcpy(&magic[6], &control.st_size, 8);
@ -78,10 +78,10 @@ static void write_magic(int fd_in, int fd_out)
magic[i + 16] = (char)control.lzma_properties[i];
}
if (lseek(fd_out, 0, SEEK_SET) != 0)
if (unlikely(lseek(fd_out, 0, SEEK_SET)))
fatal("Failed to seek to BOF to write Magic Header\n");
if (write(fd_out, magic, sizeof(magic)) != sizeof(magic))
if (unlikely(write(fd_out, magic, sizeof(magic)) != sizeof(magic)))
fatal("Failed to write magic header\n");
}
@ -91,14 +91,13 @@ static void read_magic(int fd_in, i64 *expected_size)
uint32_t v;
int i;
if (read(fd_in, magic, sizeof(magic)) != sizeof(magic))
if (unlikely(read(fd_in, magic, sizeof(magic)) != sizeof(magic)))
fatal("Failed to read magic header\n");
*expected_size = 0;
if (strncmp(magic, "LRZI", 4) != 0) {
if (unlikely(strncmp(magic, "LRZI", 4)))
fatal("Not an lrzip file\n");
}
memcpy(&control.major_version, &magic[4], 1);
memcpy(&control.minor_version, &magic[5], 1);
@ -128,10 +127,10 @@ static void preserve_perms(int fd_in, int fd_out)
{
struct stat st;
if (fstat(fd_in, &st) != 0)
if (unlikely(fstat(fd_in, &st)))
fatal("Failed to fstat input file\n");
if (fchmod(fd_out, (st.st_mode & 0777)) != 0)
fatal("Failed to set permissions on %s\n", control.outfile);
if (fchmod(fd_out, (st.st_mode & 0777)))
print_output("Warning, unable to set permissions on %s\n", control.outfile);
/* chown fail is not fatal */
fchown(fd_out, st.st_uid, st.st_gid);
@ -146,11 +145,11 @@ static int open_tmpoutfile(void)
print_verbose("Outputting to stdout.\n");
control.outfile = realloc(NULL, 16);
strcpy(control.outfile, "lrzipout.XXXXXX");
if (!control.outfile)
if (unlikely(!control.outfile))
fatal("Failed to allocate outfile name\n");
fd_out = mkstemp(control.outfile);
if (fd_out == -1)
if (unlikely(fd_out == -1))
fatal("Failed to create out tmpfile: %s\n", strerror(errno));
return fd_out;
}
@ -165,7 +164,7 @@ static void dump_tmpoutfile(int fd_out)
/* flush anything not yet in the temporary file */
fsync(fd_out);
tmpoutfp = fdopen(fd_out, "r");
if (tmpoutfp == NULL)
if (unlikely(tmpoutfp == NULL))
fatal("Failed to fdopen out tmpfile: %s\n", strerror(errno));
rewind(tmpoutfp);
@ -182,11 +181,11 @@ static int open_tmpinfile(void)
control.infile = malloc(15);
strcpy(control.infile, "lrzipin.XXXXXX");
if (!control.infile)
if (unlikely(!control.infile))
fatal("Failed to allocate infile name\n");
fd_in = mkstemp(control.infile);
if (fd_in == -1)
if (unlikely(fd_in == -1))
fatal("Failed to create in tmpfile: %s\n", strerror(errno));
return fd_in;
}
@ -200,7 +199,7 @@ static void read_tmpinfile(int fd_in)
if (control.flags & FLAG_SHOW_PROGRESS)
fprintf(control.msgout, "Copying from stdin.\n");
tmpinfp = fdopen(fd_in, "w+");
if (tmpinfp == NULL)
if (unlikely(tmpinfp == NULL))
fatal("Failed to fdopen in tmpfile: %s\n", strerror(errno));
while ((tmpchar = getchar()) != EOF)
@ -225,7 +224,7 @@ static void decompress_file(void)
* because manipulations may be made to input filename, set local ptr
*/
infilecopy = malloc(strlen(control.infile) + strlen(control.suffix) + 1);
if (infilecopy == NULL)
if (unlikely(infilecopy == NULL))
fatal("Failed to allocate memory for infile suffix\n");
else {
strcpy(infilecopy, control.infile);
@ -255,7 +254,7 @@ static void decompress_file(void)
*tmp='\0';
control.outfile = malloc((control.outdir == NULL? 0: strlen(control.outdir)) + strlen(tmpoutfile) + 1);
if (!control.outfile)
if (unlikely(!control.outfile))
fatal("Failed to allocate outfile name\n");
if (control.outdir) { /* prepend control.outdir */
@ -275,7 +274,7 @@ static void decompress_file(void)
read_tmpinfile(fd_in);
} else {
fd_in = open(infilecopy, O_RDONLY);
if (fd_in == -1) {
if (unlikely(fd_in == -1)) {
fatal("Failed to open %s: %s\n",
infilecopy,
strerror(errno));
@ -287,7 +286,7 @@ static void decompress_file(void)
fd_out = open(control.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
else
fd_out = open(control.outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_out == -1)
if (unlikely(fd_out == -1))
fatal("Failed to create %s: %s\n", control.outfile, strerror(errno));
if (!NO_SET_PERMS)
@ -296,7 +295,7 @@ static void decompress_file(void)
fd_out = open_tmpoutfile();
fd_hist = open(control.outfile, O_RDONLY);
if (fd_hist == -1)
if (unlikely(fd_hist == -1))
fatal("Failed to open history file %s\n", control.outfile);
read_magic(fd_in, &expected_size);
@ -313,19 +312,19 @@ static void decompress_file(void)
print_output("Output filename is: %s: ", control.outfile);
print_progress("[OK] - %lld bytes \n", expected_size);
if (close(fd_hist) != 0 || close(fd_out) != 0)
if (unlikely(close(fd_hist) != 0 || close(fd_out) != 0))
fatal("Failed to close files\n");
if (TEST_ONLY | STDOUT) {
/* Delete temporary files generated for testing or faking stdout */
if (unlink(control.outfile) != 0)
if (unlikely(unlink(control.outfile)))
fatal("Failed to unlink tmpfile: %s\n", strerror(errno));
}
close(fd_in);
if (!(KEEP_FILES | TEST_ONLY) || STDIN) {
if (unlink(control.infile) != 0)
if (unlikely(unlink(control.infile)))
fatal("Failed to unlink %s: %s\n", infilecopy, strerror(errno));
}
@ -347,7 +346,7 @@ static void get_fileinfo(void)
if (!STDIN) {
if ((tmp = strrchr(control.infile, '.')) && strcmp(tmp,control.suffix)) {
infilecopy = malloc(strlen(control.infile) + strlen(control.suffix) + 1);
if (infilecopy == NULL)
if (unlikely(infilecopy == NULL))
fatal("Failed to allocate memory for infile suffix\n");
else {
strcpy(infilecopy, control.infile);
@ -361,12 +360,12 @@ static void get_fileinfo(void)
fd_in = 0;
else {
fd_in = open(infilecopy, O_RDONLY);
if (fd_in == -1)
if (unlikely(fd_in == -1))
fatal("Failed to open %s: %s\n", infilecopy, strerror(errno));
}
/* Get file size */
if (fstat(fd_in, &st) != 0)
if (unlikely(fstat(fd_in, &st)))
fatal("bad magic file descriptor!?\n");
memcpy(&infile_size, &st.st_size, 8);
@ -378,7 +377,7 @@ static void get_fileinfo(void)
seekspot = 50;
else
seekspot = 74;
if (lseek(fd_in, seekspot, SEEK_SET) == -1)
if (unlikely(lseek(fd_in, seekspot, SEEK_SET) == -1))
fatal("Failed to lseek in get_fileinfo: %s\n", strerror(errno));
/* Read the compression type of the first block. It's possible that
@ -407,7 +406,7 @@ static void get_fileinfo(void)
print_output("Compression ratio: %.3Lf\n", cratio);
if (STDIN) {
if (unlink(control.infile) != 0)
if (unlikely(unlink(control.infile)))
fatal("Failed to unlink %s: %s\n", infilecopy, strerror(errno));
}
@ -436,7 +435,7 @@ static void compress_file(void)
}
fd_in = open(control.infile, O_RDONLY);
if (fd_in == -1)
if (unlikely(fd_in == -1))
fatal("Failed to open %s: %s\n", control.infile, strerror(errno));
} else
fd_in = 0;
@ -448,7 +447,7 @@ static void compress_file(void)
control.outfile = strdup(control.outname);
else if ((tmp=strrchr(control.outname, '.')) && strcmp(tmp, control.suffix)) {
control.outfile = malloc(strlen(control.outname) + strlen(control.suffix) + 1);
if (!control.outfile)
if (unlikely(!control.outfile))
fatal("Failed to allocate outfile name\n");
strcpy(control.outfile, control.outname);
strcat(control.outfile, control.suffix);
@ -466,7 +465,7 @@ static void compress_file(void)
tmpinfile = control.infile;
control.outfile = malloc((control.outdir == NULL? 0: strlen(control.outdir)) + strlen(tmpinfile) + strlen(control.suffix) + 1);
if (!control.outfile)
if (unlikely(!control.outfile))
fatal("Failed to allocate outfile name\n");
if (control.outdir) { /* prepend control.outdir */
@ -482,7 +481,7 @@ static void compress_file(void)
fd_out = open(control.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
else
fd_out = open(control.outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd_out == -1)
if (unlikely(fd_out == -1))
fatal("Failed to create %s: %s\n", control.outfile, strerror(errno));
} else
fd_out = open_tmpoutfile();
@ -491,7 +490,7 @@ static void compress_file(void)
preserve_perms(fd_in, fd_out);
/* write zeroes to 24 bytes at beginning of file */
if (write(fd_out, header, sizeof(header)) != sizeof(header))
if (unlikely(write(fd_out, header, sizeof(header)) != sizeof(header)))
fatal("Cannot write file header\n");
rzip_fd(fd_in, fd_out);
@ -502,17 +501,17 @@ static void compress_file(void)
if (STDOUT)
dump_tmpoutfile(fd_out);
if (close(fd_in) != 0 || close(fd_out) != 0)
if (unlikely(close(fd_in) != 0 || close(fd_out)))
fatal("Failed to close files\n");
if (STDOUT) {
/* Delete temporary files generated for testing or faking stdout */
if (unlink(control.outfile) != 0)
if (unlikely(unlink(control.outfile)))
fatal("Failed to unlink tmpfile: %s\n", strerror(errno));
}
if (!KEEP_FILES) {
if (unlink(control.infile) != 0)
if (unlikely(unlink(control.infile)))
fatal("Failed to unlink %s: %s\n", control.infile, strerror(errno));
}
@ -724,14 +723,6 @@ int main(int argc, char *argv[])
if (argc < 1)
control.flags |= FLAG_STDIN;
#if 0
/* malloc limited to 2GB on 32bit */
if (sizeof(long) == 4 && control.window > 20) {
control.window = 20;
print_verbose("Limiting control window to 2GB due to 32bit limitations.\n");
}
#endif
/* OK, if verbosity set, print summary of options selected */
if (VERBOSE && !INFO) {
print_err("The following options are in effect for this %s.\n",
@ -783,7 +774,7 @@ int main(int argc, char *argv[])
print_err("\n");
}
if (setpriority(PRIO_PROCESS, 0, control.nice_val) == -1)
if (unlikely(setpriority(PRIO_PROCESS, 0, control.nice_val) == -1))
fatal("Unable to set nice value\n");
/* One extra iteration for the case of no parameters means we will default to stdin/out */

View file

@ -24,7 +24,7 @@ static inline uchar read_u8(void *ss, int stream)
{
uchar b;
if (read_stream(ss, stream, &b, 1) != 1)
if (unlikely(read_stream(ss, stream, &b, 1) != 1))
fatal("Stream read u8 failed\n");
return b;
}
@ -33,7 +33,7 @@ static inline u32 read_u32(void *ss, int stream)
{
u32 ret;
if (read_stream(ss, stream, (uchar *)&ret, 4) != 4)
if (unlikely(read_stream(ss, stream, (uchar *)&ret, 4) != 4))
fatal("Stream read u32 failed\n");
return ret;
}
@ -68,17 +68,17 @@ static i64 unzip_literal(void *ss, i64 len, int fd_out, uint32 *cksum)
{
uchar *buf;
if (len < 0)
if (unlikely(len < 0))
fatal("len %lld is negative in unzip_literal!\n",len);
/* We use anonymous mmap instead of malloc to allow us to allocate up
* to 2^44 even on 32 bits */
buf = (uchar *)mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (buf == MAP_FAILED)
if (unlikely(buf == MAP_FAILED))
fatal("Failed to allocate literal buffer of size %lld\n", len);
read_stream(ss, 1, buf, len);
if (write_1g(fd_out, buf, (size_t)len) != (ssize_t)len)
if (unlikely(write_1g(fd_out, buf, (size_t)len) != (ssize_t)len))
fatal("Failed to write literal buffer of size %lld\n", len);
*cksum = CrcUpdate(*cksum, buf, len);
@ -91,17 +91,17 @@ static i64 unzip_match(void *ss, i64 len, int fd_out, int fd_hist, uint32 *cksum
{
i64 offset, n, total, cur_pos;
if (len < 0)
if (unlikely(len < 0))
fatal("len %lld is negative in unzip_match!\n",len);
total = 0;
cur_pos = lseek(fd_out, 0, SEEK_CUR);
if (cur_pos == -1)
if (unlikely(cur_pos == -1))
fatal("Seek failed on out file in unzip_match.\n");
/* Note the offset is in a different format v0.40+ */
offset = read_vchars(ss, 0, chunk_bytes);
if (lseek(fd_hist, cur_pos - offset, SEEK_SET) == -1)
if (unlikely(lseek(fd_hist, cur_pos - offset, SEEK_SET) == -1))
fatal("Seek failed by %d from %d on history file in unzip_match - %s\n",
offset, cur_pos, strerror(errno));
@ -110,13 +110,13 @@ static i64 unzip_match(void *ss, i64 len, int fd_out, int fd_hist, uint32 *cksum
n = MIN(len, offset);
buf = (uchar *)mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (buf == MAP_FAILED)
if (unlikely(buf == MAP_FAILED))
fatal("Failed to allocate match buffer of size %lld\n", n);
if (read_1g(fd_hist, buf, (size_t)n) != (ssize_t)n)
if (unlikely(read_1g(fd_hist, buf, (size_t)n) != (ssize_t)n))
fatal("Failed to read %d bytes in unzip_match\n", n);
if (write_1g(fd_out, buf, (size_t)n) != (ssize_t)n)
if (unlikely(write_1g(fd_out, buf, (size_t)n) != (ssize_t)n))
fatal("Failed to write %d bytes in unzip_match\n", n);
*cksum = CrcUpdate(*cksum, buf, n);
@ -168,7 +168,7 @@ static i64 runzip_chunk(int fd_in, int fd_out, int fd_hist, i64 expected_size, i
chunk_bytes = 8;
else {
/* Read in the stored chunk byte width from the file */
if (read(fd_in, &chunk_bytes, 1) != 1)
if (unlikely(read(fd_in, &chunk_bytes, 1) != 1))
fatal("Failed to read chunk_bytes size in runzip_chunk\n");
}
if (!tally)
@ -176,14 +176,14 @@ static i64 runzip_chunk(int fd_in, int fd_out, int fd_hist, i64 expected_size, i
print_maxverbose("\nChunk byte width: %d\n", chunk_bytes);
ofs = lseek(fd_in, 0, SEEK_CUR);
if (ofs == -1)
if (unlikely(ofs == -1))
fatal("Failed to seek input file in runzip_fd\n");
if (fstat(fd_in, &st) != 0 || st.st_size - ofs == 0)
return 0;
ss = open_stream_in(fd_in, NUM_STREAMS);
if (!ss)
if (unlikely(!ss))
fatal("Failed to open_stream_in in runzip_chunk\n");
while ((len = read_header(ss, &head)) || head) {
@ -206,10 +206,10 @@ static i64 runzip_chunk(int fd_in, int fd_out, int fd_hist, i64 expected_size, i
}
good_cksum = read_u32(ss, 0);
if (good_cksum != cksum)
if (unlikely(good_cksum != cksum))
fatal("Bad checksum 0x%08x - expected 0x%08x\n", cksum, good_cksum);
if (close_stream_in(ss) != 0)
if (unlikely(close_stream_in(ss)))
fatal("Failed to close stream!\n");
return total;

114
rzip.c
View file

@ -90,25 +90,49 @@ struct sliding_buffer {
uchar *buf_low; /* The low window buffer */
uchar *buf_high;/* "" high "" */
i64 orig_offset;/* Where the original buffer started */
i64 offset_high;/* What the current offset the high buffer has */
i64 offset_low; /* What the current offset the low buffer has */
i64 offset_high;/* "" high buffer "" */
i64 offset_search;/* Where the search is up to */
i64 orig_size; /* How big the full buffer would be */
i64 size_low; /* How big the low buffer is */
i64 size_high;
i64 size_high; /* "" high "" */
i64 high_length;/* How big the high buffer should be */
int fd; /* The fd of the mmap */
} sb; /* Sliding buffer */
static void remap_high_sb(i64 p)
static void remap_low_sb(void)
{
if (munmap(sb.buf_high, sb.size_high) != 0)
static int top = 0;
i64 new_offset;
if (top)
return;
new_offset = sb.offset_search;
if (new_offset + sb.size_low > sb.orig_size) {
new_offset = sb.orig_size - sb.size_low;
top = 1;
}
new_offset -= new_offset % 4096; /* Round to page size */
print_maxverbose("Sliding main buffer\n");
if (unlikely(munmap(sb.buf_low, sb.size_low)))
fatal("Failed to munmap in remap_low_sb\n");
sb.offset_low = new_offset;
sb.buf_low = (uchar *)mmap(sb.buf_low, sb.size_low, PROT_READ, MAP_SHARED, sb.fd, sb.orig_offset + sb.offset_low);
if (unlikely(sb.buf_low == MAP_FAILED))
fatal("Failed to re mmap in remap_low_sb\n");
}
static inline void remap_high_sb(i64 p)
{
if (unlikely(munmap(sb.buf_high, sb.size_high) != 0))
fatal("Failed to munmap in remap_high_sb\n");
sb.size_high = 4096; /* In case we shrunk it when we hit the end of the file */
sb.size_high = sb.high_length; /* In case we shrunk it when we hit the end of the file */
sb.offset_high = p;
if ((sb.offset_high + sb.orig_offset) % 4096)
sb.offset_high -= (sb.offset_high + sb.orig_offset) % 4096;
if (sb.offset_high + sb.size_high > sb.orig_size)
sb.offset_high -= (sb.offset_high + sb.orig_offset) % 4096;
if (unlikely(sb.offset_high + sb.size_high > sb.orig_size))
sb.size_high = sb.orig_size - sb.offset_high;
sb.buf_high = (uchar *)mmap(NULL, sb.size_high, PROT_READ, MAP_SHARED, sb.fd, sb.orig_offset + sb.offset_high);
if (sb.buf_high == MAP_FAILED)
sb.buf_high = (uchar *)mmap(sb.buf_high, sb.size_high, PROT_READ, MAP_SHARED, sb.fd, sb.orig_offset + sb.offset_high);
if (unlikely(sb.buf_high == MAP_FAILED))
fatal("Failed to re mmap in remap_high_sb\n");
}
@ -120,8 +144,12 @@ static void remap_high_sb(i64 p)
* compression windows. */
static uchar *get_sb(i64 p)
{
if (p < sb.size_low)
return (sb.buf_low + p);
i64 low_end = sb.offset_low + sb.size_low;
if (unlikely(sb.offset_search > low_end))
remap_low_sb();
if (p >= sb.offset_low && p < low_end)
return (sb.buf_low + p - sb.offset_low);
if (p >= sb.offset_high && p < (sb.offset_high + sb.size_high))
return (sb.buf_high + (p - sb.offset_high));
/* (p > sb.size_low && p < sb.offset_high) */
@ -131,13 +159,13 @@ static uchar *get_sb(i64 p)
static inline void put_u8(void *ss, int stream, uchar b)
{
if (write_stream(ss, stream, &b, 1) != 0)
if (unlikely(write_stream(ss, stream, &b, 1) != 0))
fatal("Failed to put_u8\n");
}
static inline void put_u32(void *ss, int stream, uint32_t s)
{
if (write_stream(ss, stream, (uchar *)&s, 4))
if (unlikely(write_stream(ss, stream, (uchar *)&s, 4)))
fatal("Failed to put_u32\n");
}
@ -198,7 +226,7 @@ int write_sbstream(void *ss, int stream, i64 p, i64 len)
p += n;
len -= n;
if (sinfo->s[stream].buflen == sinfo->bufsize) {
if (flush_buffer(sinfo, stream) != 0)
if (unlikely(flush_buffer(sinfo, stream) != 0))
return -1;
}
}
@ -217,7 +245,7 @@ static void put_literal(struct rzip_state *st, i64 last, i64 p)
put_header(st->ss, 0, len);
if (len && write_sbstream(st->ss, 1, last, len) != 0)
if (unlikely(len && write_sbstream(st->ss, 1, last, len)))
fatal("Failed to write_stream in put_literal\n");
last += len;
} while (p > last);
@ -482,7 +510,7 @@ static void hash_search(struct rzip_state *st, double pct_base, double pct_multi
st->hash_table = calloc(sizeof(st->hash_table[0]), (1 << st->hash_bits));
}
if (!st->hash_table)
if (unlikely(!st->hash_table))
fatal("Failed to allocate hash table in hash_search\n");
st->minimum_tag_mask = tag_mask;
@ -505,6 +533,7 @@ static void hash_search(struct rzip_state *st, double pct_base, double pct_multi
i64 reverse;
p++;
sb.offset_search = p;
t = next_tag(st, p, t);
/* Don't look for a match if there are no tags with
@ -540,7 +569,7 @@ static void hash_search(struct rzip_state *st, double pct_base, double pct_multi
t = full_tag(st, p);
}
if (p % 100 == 0) {
if (unlikely(p % 100 == 0)) {
pct = pct_base + (pct_multiple * (100.0 * p) /
st->chunk_size);
if (pct != lastpct) {
@ -612,21 +641,20 @@ static void mmap_stdin(uchar *buf, struct rzip_state *st)
i64 total;
total = 0;
print_verbose("Reading stdin into mmapped ram...\n");
while (len > 0) {
if (len > one_g)
ret = one_g;
else
ret = len;
ret = read(0, offset_buf, (size_t)ret);
if (ret < 0)
if (unlikely(ret < 0))
fatal("Failed to read in mmap_stdin\n");
total += ret;
if (ret == 0) {
/* Should be EOF */
print_maxverbose("Shrinking chunk to %lld\n", total);
buf = mremap(buf, st->chunk_size, total, 0);
if (buf == MAP_FAILED)
if (unlikely(buf == MAP_FAILED))
fatal("Failed to remap to smaller buf in mmap_stdin\n");
st->chunk_size = total;
control.st_size += total;
@ -638,10 +666,16 @@ static void mmap_stdin(uchar *buf, struct rzip_state *st)
}
}
static const i64 two_gig = 2 * 1000 * 1000 * 1000;
static void init_sliding_mmap(struct rzip_state *st, int fd_in, i64 offset)
{
i64 size = st->chunk_size;
if (sizeof(long) == 4 && size > two_gig) {
print_verbose("Limiting to 2G due to 32 bit limitations\n");
size = two_gig;
}
sb.orig_offset = offset;
retry:
/* Mmapping anonymously first will tell us how much ram we can use in
@ -656,44 +690,52 @@ retry:
if (sb.buf_low == MAP_FAILED) {
size = size / 10 * 9;
size -= size % 4096; /* Round to page size */
if (!size)
if (unlikely(!size))
fatal("Unable to mmap any ram\n");
goto retry;
}
print_maxverbose("Succeeded in preallocating %lld sized mmap\n", size);
if (!STDIN) {
if (munmap(sb.buf_low, size) != 0)
if (unlikely(munmap(sb.buf_low, size) != 0))
fatal("Failed to munmap\n");
} else
st->chunk_size = size;
}
print_verbose("Reading file into mmapped ram...\n");
if (!STDIN) {
sb.buf_low = (uchar *)mmap(sb.buf_low, size, PROT_READ, MAP_SHARED, fd_in, offset);
if (sb.buf_low == MAP_FAILED) {
size = size / 10 * 9;
size -= size % 4096; /* Round to page size */
if (!size)
if (unlikely(!size))
fatal("Unable to mmap any ram\n");
goto retry;
}
} else
mmap_stdin(sb.buf_low, st);
print_maxverbose("Succeeded in allocating %lld sized mmap\n", size);
if (size < st->chunk_size) {
if (UNLIMITED && !STDIN)
print_verbose("File is beyond window size, will proceed MUCH slower in unlimited mode beyond\nthe window size with a sliding_mmap\n");
print_verbose("File is beyond window size, will proceed MUCH slower in unlimited mode with a sliding_mmap buffer\n");
else {
print_verbose("Needed to shrink window size to %lld\n", size);
st->chunk_size = size;
}
}
/* Initialise the high buffer */
if (UNLIMITED && !STDIN) {
sb.buf_high = (uchar *)mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd_in, offset);
if (sb.buf_high == MAP_FAILED)
sb.high_length = 65536;
sb.buf_high = (uchar *)mmap(NULL, sb.high_length, PROT_READ, MAP_SHARED, fd_in, offset);
if (unlikely(sb.buf_high == MAP_FAILED))
fatal("Unable to mmap buf_high in init_sliding_mmap\n");
sb.size_high = 4096;
sb.size_high = sb.high_length;
sb.offset_high = 0;
}
sb.offset_low = 0;
sb.offset_search = 0;
sb.size_low = size;
sb.orig_size = st->chunk_size;
sb.fd = fd_in;
@ -706,18 +748,18 @@ static void rzip_chunk(struct rzip_state *st, int fd_in, int fd_out, i64 offset,
{
init_sliding_mmap(st, fd_in, offset);
st->ss = open_stream_out(fd_out, NUM_STREAMS, st->chunk_size);
if (!st->ss)
if (unlikely(!st->ss))
fatal("Failed to open streams in rzip_chunk\n");
hash_search(st, pct_base, pct_multiple);
/* unmap buffer before closing and reallocating streams */
if (munmap(sb.buf_low, sb.size_low) != 0)
if (unlikely(munmap(sb.buf_low, sb.size_low)))
fatal("Failed to munmap in rzip_chunk\n");
if (UNLIMITED && !STDIN) {
if (munmap(sb.buf_high, sb.size_high) != 0)
if (unlikely(munmap(sb.buf_high, sb.size_high)))
fatal("Failed to munmap in rzip_chunk\n");
}
if (close_stream_out(st->ss) != 0)
if (unlikely(close_stream_out(st->ss)))
fatal("Failed to flush/close streams in rzip_chunk\n");
}
@ -740,15 +782,15 @@ void rzip_fd(int fd_in, int fd_out)
double finish_time, elapsed_time, chunkmbs;
st = calloc(sizeof(*st), 1);
if (!st)
if (unlikely(!st))
fatal("Failed to allocate control state in rzip_fd\n");
if (LZO_COMPRESS) {
if (lzo_init() != LZO_E_OK)
if (unlikely(lzo_init() != LZO_E_OK))
fatal("lzo_init() failed\n");
}
if (fstat(fd_in, &s))
if (unlikely(fstat(fd_in, &s)))
fatal("Failed to stat fd_in in rzip_fd - %s\n", strerror(errno));
if (!STDIN) {
@ -806,7 +848,7 @@ void rzip_fd(int fd_in, int fd_out)
if (bits % 8)
st->chunk_bytes++;
print_maxverbose("Byte width: %d\n", st->chunk_bytes);
if (write(fd_out, &st->chunk_bytes, 1) != 1)
if (unlikely(write(fd_out, &st->chunk_bytes, 1) != 1))
fatal("Failed to write chunk_bytes size in rzip_fd\n");
pct_base = (100.0 * (s.st_size - len)) / s.st_size;

4
rzip.h
View file

@ -129,6 +129,9 @@ extern char *sys_errlist[];
extern int errno;
#endif
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
typedef long long int i64;
typedef uint16_t u16;
typedef uint32_t u32;
@ -222,7 +225,6 @@ struct stream_info {
void fatal(const char *format, ...);
void sighandler();
void err_msg(const char *format, ...);
i64 runzip_fd(int fd_in, int fd_out, int fd_hist, i64 expected_size);
void rzip_fd(int fd_in, int fd_out);
void *open_stream_out(int f, int n, i64 limit);

232
stream.c
View file

@ -33,13 +33,13 @@ static inline FILE *fake_fmemopen(void *buf, size_t buflen, char *mode)
{
FILE *in;
if (strcmp(mode, "r"))
fatal("fake_fmemopen only supports mode \"r\".");
if (unlikely(strcmp(mode, "r")))
fatal("fake_fmemopen only supports mode \"r\".");
in = tmpfile();
if (!in)
return NULL;
if (fwrite(buf, buflen, 1, in) != 1)
return NULL;
if (unlikely(!in))
return NULL;
if (unlikely(fwrite(buf, buflen, 1, in) != 1))
return NULL;
rewind(in);
return in;
}
@ -48,10 +48,10 @@ static inline FILE *fake_open_memstream(char **buf, size_t *length)
{
FILE *out;
if (buf == NULL || length == NULL)
fatal("NULL parameter to fake_open_memstream");
if (unlikely(buf == NULL || length == NULL))
fatal("NULL parameter to fake_open_memstream");
out = tmpfile();
if (!out)
if (unlikely(!out))
return NULL;
return out;
}
@ -60,17 +60,17 @@ static inline int fake_open_memstream_update_buffer(FILE *fp, uchar **buf, size_
{
long original_pos = ftell(fp);
if (fseek(fp, 0, SEEK_END) != 0)
if (unlikely(fseek(fp, 0, SEEK_END)))
return -1;
*length = ftell(fp);
rewind(fp);
*buf = (uchar *)malloc(*length);
if (!*buf)
return -1;
if (fread(*buf, *length, 1, fp) != 1)
return -1;
if (fseek(fp, original_pos, SEEK_SET) != 0)
return -1;
if (unlikely(!*buf))
return -1;
if (unlikely(fread(*buf, *length, 1, fp) != 1))
return -1;
if (unlikely(fseek(fp, original_pos, SEEK_SET)))
return -1;
return 0;
}
@ -94,15 +94,15 @@ static void zpaq_compress_buf(struct stream *s, int *c_type, i64 *c_len)
return;
in = fmemopen(s->buf, s->buflen, "r");
if (!in)
if (unlikely(!in))
fatal("Failed to fmemopen in zpaq_compress_buf\n");
out = open_memstream((char **)&c_buf, &dlen);
if (!out)
if (unlikely(!out))
fatal("Failed to open_memstream in zpaq_compress_buf\n");
zpipe_compress(in, out, control.msgout, s->buflen, (int)(SHOW_PROGRESS));
if (memstream_update_buffer(out, &c_buf, &dlen) != 0)
if (unlikely(memstream_update_buffer(out, &c_buf, &dlen) != 0))
fatal("Failed to memstream_update_buffer in zpaq_compress_buf");
fclose(in);
@ -203,19 +203,19 @@ static void lzma_compress_buf(struct stream *s, int *c_type, i64 *c_len)
if (lzma_ret != SZ_OK) {
switch (lzma_ret) {
case SZ_ERROR_MEM:
err_msg("\nLZMA ERROR: %d. Try a smaller compression window.\n", SZ_ERROR_MEM);
print_err("\nLZMA ERROR: %d. Try a smaller compression window.\n", SZ_ERROR_MEM);
break;
case SZ_ERROR_PARAM:
err_msg("\nLZMA Parameter ERROR: %d. This should not happen.\n", SZ_ERROR_PARAM);
print_err("\nLZMA Parameter ERROR: %d. This should not happen.\n", SZ_ERROR_PARAM);
break;
case SZ_ERROR_OUTPUT_EOF:
err_msg("\nHarmless LZMA Output Buffer Overflow error: %d. Incompressible block.\n", SZ_ERROR_OUTPUT_EOF);
print_err("\nHarmless LZMA Output Buffer Overflow error: %d. Incompressible block.\n", SZ_ERROR_OUTPUT_EOF);
break;
case SZ_ERROR_THREAD:
err_msg("\nLZMA Multi Thread ERROR: %d. This should not happen.\n", SZ_ERROR_THREAD);
print_err("\nLZMA Multi Thread ERROR: %d. This should not happen.\n", SZ_ERROR_THREAD);
break;
default:
err_msg("Unidentified LZMA ERROR: %d. This should not happen.\n", lzma_ret);
print_err("Unidentified LZMA ERROR: %d. This should not happen.\n", lzma_ret);
break;
}
/* can pass -1 if not compressible! Thanks Lasse Collin */
@ -287,19 +287,19 @@ static int zpaq_decompress_buf(struct stream *s)
FILE *in, *out;
in = fmemopen(s->buf, s->buflen, "r");
if (!in) {
err_msg("Failed to fmemopen in zpaq_decompress_buf\n");
if (unlikely(!in)) {
print_err("Failed to fmemopen in zpaq_decompress_buf\n");
return -1;
}
out = open_memstream((char **)&c_buf, &dlen);
if (!out) {
err_msg("Failed to open_memstream in zpaq_decompress_buf\n");
if (unlikely(!out)) {
print_err("Failed to open_memstream in zpaq_decompress_buf\n");
return -1;
}
zpipe_decompress(in, out, control.msgout, s->buflen, (int)(SHOW_PROGRESS));
if (memstream_update_buffer(out, &c_buf, &dlen) != 0)
if (unlikely(memstream_update_buffer(out, &c_buf, &dlen)))
fatal("Failed to memstream_update_buffer in zpaq_decompress_buf");
fclose(in);
@ -307,8 +307,8 @@ static int zpaq_decompress_buf(struct stream *s)
free(s->buf);
s->buf = c_buf;
if ((i64)dlen != s->buflen) {
err_msg("Inconsistent length after decompression. Got %d bytes, expected %d\n", dlen, s->buflen);
if (unlikely((i64)dlen != s->buflen)) {
print_err("Inconsistent length after decompression. Got %d bytes, expected %lld\n", dlen, s->buflen);
return -1;
}
@ -323,19 +323,19 @@ static int bzip2_decompress_buf(struct stream *s, i64 c_len)
c_buf = s->buf;
s->buf = malloc(dlen);
if (!s->buf) {
err_msg("Failed to allocate %d bytes for decompression\n", dlen);
if (unlikely(!s->buf)) {
print_err("Failed to allocate %d bytes for decompression\n", dlen);
return -1;
}
bzerr = BZ2_bzBuffToBuffDecompress((char*)s->buf, &dlen, (char*)c_buf, c_len, 0, 0);
if (bzerr != BZ_OK) {
err_msg("Failed to decompress buffer - bzerr=%d\n", bzerr);
if (unlikely(bzerr != BZ_OK)) {
print_err("Failed to decompress buffer - bzerr=%d\n", bzerr);
return -1;
}
if (dlen != s->buflen) {
err_msg("Inconsistent length after decompression. Got %d bytes, expected %d\n", dlen, s->buflen);
if (unlikely(dlen != s->buflen)) {
print_err("Inconsistent length after decompression. Got %d bytes, expected %lld\n", dlen, s->buflen);
return -1;
}
@ -351,19 +351,19 @@ static int gzip_decompress_buf(struct stream *s, i64 c_len)
c_buf = s->buf;
s->buf = malloc(dlen);
if (!s->buf) {
err_msg("Failed to allocate %d bytes for decompression\n", dlen);
if (unlikely(!s->buf)) {
print_err("Failed to allocate %ld bytes for decompression\n", dlen);
return -1;
}
gzerr = uncompress(s->buf, &dlen, c_buf, c_len);
if (gzerr != Z_OK) {
err_msg("Failed to decompress buffer - bzerr=%d\n", gzerr);
if (unlikely(gzerr != Z_OK)) {
print_err("Failed to decompress buffer - bzerr=%d\n", gzerr);
return -1;
}
if ((i64)dlen != s->buflen) {
err_msg("Inconsistent length after decompression. Got %d bytes, expected %d\n", dlen, s->buflen);
if (unlikely((i64)dlen != s->buflen)) {
print_err("Inconsistent length after decompression. Got %ld bytes, expected %lld\n", dlen, s->buflen);
return -1;
}
@ -379,21 +379,21 @@ static int lzma_decompress_buf(struct stream *s, size_t c_len)
c_buf = s->buf;
s->buf = malloc(dlen);
if (!s->buf) {
err_msg("Failed to allocate %d bytes for decompression\n", dlen);
if (unlikely(!s->buf)) {
print_err("Failed to allocate %d bytes for decompression\n", dlen);
return -1;
}
/* With LZMA SDK 4.63 we pass control.lzma_properties
* which is needed for proper uncompress */
lzmaerr = LzmaUncompress(s->buf, &dlen, c_buf, &c_len, control.lzma_properties, 5);
if (lzmaerr != 0) {
err_msg("Failed to decompress buffer - lzmaerr=%d\n", lzmaerr);
if (unlikely(lzmaerr != 0)) {
print_err("Failed to decompress buffer - lzmaerr=%d\n", lzmaerr);
return -1;
}
if ((i64)dlen != s->buflen) {
err_msg("Inconsistent length after decompression. Got %d bytes, expected %d\n", dlen, s->buflen);
if (unlikely((i64)dlen != s->buflen)) {
print_err("Inconsistent length after decompression. Got %d bytes, expected %lld\n", dlen, s->buflen);
return -1;
}
@ -409,19 +409,19 @@ static int lzo_decompress_buf(struct stream *s, i64 c_len)
c_buf = s->buf;
s->buf = malloc(dlen);
if (!s->buf) {
err_msg("Failed to allocate %d bytes for decompression\n", dlen);
if (unlikely(!s->buf)) {
print_err("Failed to allocate %d bytes for decompression\n", (int)dlen);
return -1;
}
lzerr = lzo1x_decompress((uchar*)c_buf,c_len,(uchar*)s->buf,&dlen,NULL);
if (lzerr != LZO_E_OK) {
err_msg("Failed to decompress buffer - lzerr=%d\n", lzerr);
if (unlikely(lzerr != LZO_E_OK)) {
print_err("Failed to decompress buffer - lzerr=%d\n", lzerr);
return -1;
}
if ((i64)dlen != s->buflen) {
err_msg("Inconsistent length after decompression. Got %d bytes, expected %d\n", dlen, s->buflen);
if (unlikely((i64)dlen != s->buflen)) {
print_err("Inconsistent length after decompression. Got %d bytes, expected %lld\n", (int)dlen, s->buflen);
return -1;
}
@ -449,7 +449,7 @@ ssize_t write_1g(int fd, void *buf, i64 len)
else
ret = len;
ret = write(fd, offset_buf, (size_t)ret);
if (ret < 0)
if (unlikely(ret < 0))
return ret;
len -= ret;
offset_buf += ret;
@ -472,7 +472,7 @@ ssize_t read_1g(int fd, void *buf, i64 len)
else
ret = len;
ret = read(fd, offset_buf, (size_t)ret);
if (ret < 0)
if (unlikely(ret < 0))
return ret;
len -= ret;
offset_buf += ret;
@ -487,12 +487,12 @@ static int write_buf(int f, uchar *p, i64 len)
ssize_t ret;
ret = write_1g(f, p, (size_t)len);
if (ret == -1) {
err_msg("Write of length %d failed - %s\n", len, strerror(errno));
if (unlikely(ret == -1)) {
print_err("Write of length %lld failed - %s\n", len, strerror(errno));
return -1;
}
if (ret != (ssize_t)len) {
err_msg("Partial write!? asked for %d bytes but got %d\n", len, ret);
if (unlikely(ret != (ssize_t)len)) {
print_err("Partial write!? asked for %lld bytes but got %d\n", len, ret);
return -1;
}
return 0;
@ -507,7 +507,7 @@ static int write_u8(int f, uchar v)
/* write a i64 */
static int write_i64(int f, i64 v)
{
if (write_buf(f, (uchar *)&v, 8))
if (unlikely(write_buf(f, (uchar *)&v, 8)))
return -1;
return 0;
@ -518,12 +518,12 @@ static int read_buf(int f, uchar *p, i64 len)
ssize_t ret;
ret = read_1g(f, p, (size_t)len);
if (ret == -1) {
err_msg("Read of length %d failed - %s\n", len, strerror(errno));
if (unlikely(ret == -1)) {
print_err("Read of length %lld failed - %s\n", len, strerror(errno));
return -1;
}
if (ret != (ssize_t)len) {
err_msg("Partial read!? asked for %d bytes but got %d\n", len, ret);
if (unlikely(ret != (ssize_t)len)) {
print_err("Partial read!? asked for %lld bytes but got %lld\n", len, (i64)ret);
return -1;
}
return 0;
@ -536,14 +536,14 @@ static int read_u8(int f, uchar *v)
static int read_u32(int f, u32 *v)
{
if (read_buf(f, (uchar *)v, 4))
if (unlikely(read_buf(f, (uchar *)v, 4)))
return -1;
return 0;
}
static int read_i64(int f, i64 *v)
{
if (read_buf(f, (uchar *)v, 8))
if (unlikely(read_buf(f, (uchar *)v, 8)))
return -1;
return 0;
}
@ -553,8 +553,8 @@ static int seekto(struct stream_info *sinfo, i64 pos)
{
i64 spos = pos + sinfo->initial_pos;
if (lseek(sinfo->fd, spos, SEEK_SET) != spos) {
err_msg("Failed to seek to %d in stream\n", pos);
if (unlikely(lseek(sinfo->fd, spos, SEEK_SET) != spos)) {
print_err("Failed to seek to %lld in stream\n", pos);
return -1;
}
return 0;
@ -570,7 +570,7 @@ void *open_stream_out(int f, int n, i64 limit)
int i;
sinfo = malloc(sizeof(*sinfo));
if (!sinfo)
if (unlikely(!sinfo))
return NULL;
sinfo->bufsize = 0;
@ -607,7 +607,7 @@ void *open_stream_out(int f, int n, i64 limit)
sinfo->initial_pos = lseek(f, 0, SEEK_CUR);
sinfo->s = (struct stream *)calloc(sizeof(sinfo->s[0]), n);
if (!sinfo->s) {
if (unlikely(!sinfo->s)) {
free(sinfo);
return NULL;
}
@ -626,7 +626,7 @@ retest_malloc:
for (i = 0; i < n; i++) {
sinfo->s[i].buf = malloc(sinfo->bufsize);
if (!sinfo->s[i].buf)
if (unlikely(!sinfo->s[i].buf))
fatal("Unable to malloc buffer of size %lld in open_stream_out\n", sinfo->bufsize);
}
@ -650,7 +650,7 @@ void *open_stream_in(int f, int n)
int i;
sinfo = calloc(sizeof(*sinfo), 1);
if (!sinfo)
if (unlikely(!sinfo))
return NULL;
sinfo->num_streams = n;
@ -658,7 +658,7 @@ void *open_stream_in(int f, int n)
sinfo->initial_pos = lseek(f, 0, SEEK_CUR);
sinfo->s = (struct stream *)calloc(sizeof(sinfo->s[0]), n);
if (!sinfo->s) {
if (unlikely(!sinfo->s)) {
free(sinfo);
return NULL;
}
@ -668,7 +668,7 @@ void *open_stream_in(int f, int n)
i64 v1, v2;
again:
if (read_u8(f, &c) != 0)
if (unlikely(read_u8(f, &c)))
goto failed;
/* Compatibility crap for versions < 0.40 */
@ -687,33 +687,33 @@ again:
sinfo->s[i].last_head = last_head32;
header_length = 13;
} else {
if (read_i64(f, &v1) != 0)
if (unlikely(read_i64(f, &v1)))
goto failed;
if (read_i64(f, &v2) != 0)
if (unlikely(read_i64(f, &v2)))
goto failed;
if (read_i64(f, &sinfo->s[i].last_head) != 0)
if (unlikely(read_i64(f, &sinfo->s[i].last_head)))
goto failed;
header_length = 25;
}
if (c == CTYPE_NONE && v1 == 0 && v2 == 0 && sinfo->s[i].last_head == 0 && i == 0) {
err_msg("Enabling stream close workaround\n");
if (unlikely(c == CTYPE_NONE && v1 == 0 && v2 == 0 && sinfo->s[i].last_head == 0 && i == 0)) {
print_err("Enabling stream close workaround\n");
sinfo->initial_pos += header_length;
goto again;
}
sinfo->total_read += header_length;
if (c != CTYPE_NONE) {
err_msg("Unexpected initial tag %d in streams\n", c);
if (unlikely(c != CTYPE_NONE)) {
print_err("Unexpected initial tag %d in streams\n", c);
goto failed;
}
if (v1 != 0) {
err_msg("Unexpected initial c_len %lld in streams %lld\n", v1, v2);
if (unlikely(v1 != 0)) {
print_err("Unexpected initial c_len %lld in streams %lld\n", v1, v2);
goto failed;
}
if (v2 != 0) {
err_msg("Unexpected initial u_len %lld in streams\n", v2);
if (unlikely(v2 != 0)) {
print_err("Unexpected initial u_len %lld in streams\n", v2);
goto failed;
}
}
@ -732,13 +732,13 @@ int flush_buffer(struct stream_info *sinfo, int stream)
i64 c_len = sinfo->s[stream].buflen;
int c_type = CTYPE_NONE;
if (seekto(sinfo, sinfo->s[stream].last_head) != 0)
if (unlikely(seekto(sinfo, sinfo->s[stream].last_head)))
return -1;
if (write_i64(sinfo->fd, sinfo->cur_pos) != 0)
if (unlikely(write_i64(sinfo->fd, sinfo->cur_pos)))
return -1;
sinfo->s[stream].last_head = sinfo->cur_pos + 17;
if (seekto(sinfo, sinfo->cur_pos) != 0)
if (unlikely(seekto(sinfo, sinfo->cur_pos)))
return -1;
if (!(NO_COMPRESS)) {
@ -755,15 +755,15 @@ int flush_buffer(struct stream_info *sinfo, int stream)
else fatal("Dunno wtf compression to use!\n");
}
if (write_u8(sinfo->fd, c_type) != 0 ||
write_i64(sinfo->fd, c_len) != 0 ||
write_i64(sinfo->fd, sinfo->s[stream].buflen) != 0 ||
write_i64(sinfo->fd, 0) != 0) {
if (unlikely(write_u8(sinfo->fd, c_type) ||
write_i64(sinfo->fd, c_len) ||
write_i64(sinfo->fd, sinfo->s[stream].buflen) ||
write_i64(sinfo->fd, 0))) {
return -1;
}
sinfo->cur_pos += 25;
if (write_buf(sinfo->fd, sinfo->s[stream].buf, c_len) != 0)
if (unlikely(write_buf(sinfo->fd, sinfo->s[stream].buf, c_len)))
return -1;
fsync(sinfo->fd);
sinfo->cur_pos += c_len;
@ -771,7 +771,7 @@ int flush_buffer(struct stream_info *sinfo, int stream)
sinfo->s[stream].buflen = 0;
sinfo->s[stream].buf = realloc(sinfo->s[stream].buf, sinfo->bufsize);
if (!sinfo->s[stream].buf)
if (unlikely(!sinfo->s[stream].buf))
fatal("Failed to realloc in flush_buffer\n");
return 0;
}
@ -782,10 +782,10 @@ static int fill_buffer(struct stream_info *sinfo, int stream)
i64 header_length, u_len, c_len;
uchar c_type;
if (seekto(sinfo, sinfo->s[stream].last_head) != 0)
if (unlikely(seekto(sinfo, sinfo->s[stream].last_head)))
return -1;
if (read_u8(sinfo->fd, &c_type) != 0)
if (unlikely(read_u8(sinfo->fd, &c_type)))
return -1;
/* Compatibility crap for versions < 0.4 */
if (control.major_version == 0 && control.minor_version < 4) {
@ -802,11 +802,11 @@ static int fill_buffer(struct stream_info *sinfo, int stream)
sinfo->s[stream].last_head = last_head32;
header_length = 13;
} else {
if (read_i64(sinfo->fd, &c_len) != 0)
if (unlikely(read_i64(sinfo->fd, &c_len)))
return -1;
if (read_i64(sinfo->fd, &u_len) != 0)
if (unlikely(read_i64(sinfo->fd, &u_len)))
return -1;
if (read_i64(sinfo->fd, &sinfo->s[stream].last_head) != 0)
if (unlikely(read_i64(sinfo->fd, &sinfo->s[stream].last_head)))
return -1;
header_length = 25;
}
@ -817,9 +817,9 @@ static int fill_buffer(struct stream_info *sinfo, int stream)
sinfo->s[stream].buf = realloc(sinfo->s[stream].buf, u_len);
else
sinfo->s[stream].buf = malloc(u_len);
if (!sinfo->s[stream].buf)
if (unlikely(!sinfo->s[stream].buf))
fatal("Unable to malloc buffer of size %lld in fill_buffer\n", u_len);
if (read_buf(sinfo->fd, sinfo->s[stream].buf, c_len) != 0)
if (unlikely(read_buf(sinfo->fd, sinfo->s[stream].buf, c_len)))
return -1;
sinfo->total_read += c_len;
@ -829,19 +829,19 @@ static int fill_buffer(struct stream_info *sinfo, int stream)
if (c_type != CTYPE_NONE) {
if (c_type == CTYPE_LZMA) {
if (lzma_decompress_buf(&sinfo->s[stream], (size_t)c_len))
if (unlikely(lzma_decompress_buf(&sinfo->s[stream], (size_t)c_len)))
return -1;
} else if (c_type == CTYPE_LZO) {
if (lzo_decompress_buf(&sinfo->s[stream], c_len))
if (unlikely(lzo_decompress_buf(&sinfo->s[stream], c_len)))
return -1;
} else if (c_type == CTYPE_BZIP2) {
if (bzip2_decompress_buf(&sinfo->s[stream], c_len))
if (unlikely(bzip2_decompress_buf(&sinfo->s[stream], c_len)))
return -1;
} else if (c_type == CTYPE_GZIP) {
if (gzip_decompress_buf(&sinfo->s[stream], c_len))
if (unlikely(gzip_decompress_buf(&sinfo->s[stream], c_len)))
return -1;
} else if (c_type == CTYPE_ZPAQ) {
if (zpaq_decompress_buf(&sinfo->s[stream]))
if (unlikely(zpaq_decompress_buf(&sinfo->s[stream])))
return -1;
} else fatal("Dunno wtf decompression type to use!\n");
}
@ -865,7 +865,7 @@ int write_stream(void *ss, int stream, uchar *p, i64 len)
len -= n;
if (sinfo->s[stream].buflen == sinfo->bufsize) {
if (flush_buffer(sinfo, stream) != 0)
if (unlikely(flush_buffer(sinfo, stream)))
return -1;
}
}
@ -893,7 +893,7 @@ i64 read_stream(void *ss, int stream, uchar *p, i64 len)
}
if (len && sinfo->s[stream].bufp == sinfo->s[stream].buflen) {
if (fill_buffer(sinfo, stream) != 0)
if (unlikely(fill_buffer(sinfo, stream)))
return -1;
if (sinfo->s[stream].bufp == sinfo->s[stream].buflen)
break;
@ -912,12 +912,12 @@ int close_stream_out(void *ss)
/* reallocate buffers to try and save space */
for (i = 0; i < sinfo->num_streams; i++) {
if (sinfo->s[i].buflen != 0) {
if (!realloc(sinfo->s[i].buf, sinfo->s[i].buflen))
if (unlikely(!realloc(sinfo->s[i].buf, sinfo->s[i].buflen)))
fatal("Error Reallocating Output Buffer %d\n", i);
}
}
for (i = 0; i < sinfo->num_streams; i++) {
if (sinfo->s[i].buflen != 0 && flush_buffer(sinfo, i) != 0)
if (unlikely(sinfo->s[i].buflen != 0 && flush_buffer(sinfo, i)))
return -1;
if (sinfo->s[i].buf)
free(sinfo->s[i].buf);
@ -933,8 +933,8 @@ int close_stream_in(void *ss)
struct stream_info *sinfo = ss;
int i;
if (lseek(sinfo->fd, sinfo->initial_pos + sinfo->total_read,
SEEK_SET) != sinfo->initial_pos + sinfo->total_read)
if (unlikely(lseek(sinfo->fd, sinfo->initial_pos + sinfo->total_read,
SEEK_SET) != sinfo->initial_pos + sinfo->total_read))
return -1;
for (i = 0; i < sinfo->num_streams; i++) {
if (sinfo->s[i].buf)
@ -966,14 +966,14 @@ static int lzo_compresses(struct stream *s)
if (control.threshold > 1)
return 1;
wrkmem = (lzo_bytep) malloc(LZO1X_1_MEM_COMPRESS);
if (wrkmem == NULL)
if (unlikely(wrkmem == NULL))
fatal("Unable to allocate wrkmem in lzo_compresses\n");
in_len = MIN(test_len, buftest_size);
dlen = STREAM_BUFSIZE + STREAM_BUFSIZE / 16 + 64 + 3;
c_buf = malloc(dlen);
if (!c_buf)
if (unlikely(!c_buf))
fatal("Unable to allocate c_buf in lzo_compresses\n");
print_progress("\tlzo testing for incompressible data...");

9
util.c
View file

@ -32,15 +32,6 @@
#include "rzip.h"
void err_msg(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
void fatal(const char *format, ...)
{
va_list ap;