razf.c: replaced many calloc() calls with one large calloc() using RAZF_STRUCT_SIZE...
[samtools.git] / razf.c
diff --git a/razf.c b/razf.c
index e7499f9f61e6c2fae4ec71a5f4a500ed5b151fe8..d6aede2aa01d783a349c352cb6f54fb28545d25d 100644 (file)
--- a/razf.c
+++ b/razf.c
@@ -79,10 +79,19 @@ static inline int is_big_endian(){
 
 #ifndef _RZ_READONLY
 static void add_zindex(RAZF *rz, int64_t in, int64_t out){
+       uint32_t *cores;
+       int64_t *bores;
+
        if(rz->index->size == rz->index->cap){
                rz->index->cap = rz->index->cap * 1.5 + 2;
-               rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
-               rz->index->bin_offsets  = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
+               cores = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap);
+               bores = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1));
+               if ((cores == NULL) || (bores == NULL)) {
+                       fprintf(stderr, "[%s] failure to allocate space for new zindex.\n", __func__);
+                       abort();
+               }
+               rz->index->cell_offsets = cores;
+               rz->index->bin_offsets = bores;
        }
        if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out;
        rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE];
@@ -90,21 +99,39 @@ static void add_zindex(RAZF *rz, int64_t in, int64_t out){
 }
 
 static void save_zindex(RAZF *rz, int fd){
+       size_t count;
        int32_t i, v32;
        int is_be;
        is_be = is_big_endian();
-       if(is_be) write(fd, &rz->index->size, sizeof(int));
+       if(is_be) {
+               if (write(fd, &rz->index->size, sizeof(int)) < 0) {
+                       fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
+                       abort();
+               }
+       }
        else {
                v32 = byte_swap_4((uint32_t)rz->index->size);
-               write(fd, &v32, sizeof(uint32_t));
+               if (write(fd, &v32, sizeof(uint32_t)) < 0) {
+                       fprintf(stderr, "[%s] failure to write zindex size.\n", __func__);
+                       abort();
+               }
        }
        v32 = rz->index->size / RZ_BIN_SIZE + 1;
        if(!is_be){
                for(i=0;i<v32;i++) rz->index->bin_offsets[i]  = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
                for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
        }
-       write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
-       write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size);
+       count = sizeof(int64_t) * v32;
+       if (write(fd, rz->index->bin_offsets, count) < 0) {
+               fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__);
+               abort();
+       }
+
+       count = sizeof(int32_t) * rz->index->size;
+       if (write(fd, rz->index->cell_offsets, count) < 0) {
+               fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__);
+               abort();
+       }
 }
 #endif
 
@@ -115,29 +142,52 @@ static void load_zindex(RAZF *rz, int fd){
 #endif
        int32_t i, v32;
        int is_be;
+       size_t count;
        if(!rz->load_index) return;
-       if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
+       if(rz->index == NULL) {
+               if ((rz->index = malloc(sizeof(ZBlockIndex))) == NULL) {
+                       fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
+                       abort();
+               }
+       }
        is_be = is_big_endian();
 #ifdef _USE_KNETFILE
-       knet_read(fp, &rz->index->size, sizeof(int));
+       if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) {
 #else
-       read(fd, &rz->index->size, sizeof(int));
+       if (read(fd, &rz->index->size, sizeof(int)) < 0) {
 #endif
+               fprintf(stderr, "[%s] failure to read zindex size.\n", __func__);
+               abort();
+       }
        if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size);
        rz->index->cap = rz->index->size;
        v32 = rz->index->size / RZ_BIN_SIZE + 1;
-       rz->index->bin_offsets  = malloc(sizeof(int64_t) * v32);
+       count = sizeof(int64_t) * v32;
+       if ((rz->index->bin_offsets = malloc(count)) == NULL) {
+               fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__);
+               abort();
+       }
 #ifdef _USE_KNETFILE
-       knet_read(fp, rz->index->bin_offsets, sizeof(int64_t) * v32);
+       if (knet_read(fp, rz->index->bin_offsets, count) < 0) {
 #else
-       read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32);
+       if (read(fd, rz->index->bin_offsets, count) < 0) {
 #endif
-       rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size);
+               fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__);
+               abort();
+       }
+       count = sizeof(int) * rz->index->size;
+       if ((rz->index->cell_offsets = malloc(count)) == NULL) {
+               fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__);
+               abort();
+       }
 #ifdef _USE_KNETFILE
-       knet_read(fp, rz->index->cell_offsets, sizeof(int) * rz->index->size);
+       if (knet_read(fp, rz->index->cell_offsets, count) < count) {
 #else
-       read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size);
+       if (read(fd, rz->index->cell_offsets, count) < count) {
 #endif
+               fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__);
+               abort();
+       }
        if(!is_be){
                for(i=0;i<v32;i++) rz->index->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]);
                for(i=0;i<rz->index->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]);
@@ -156,25 +206,28 @@ static RAZF* razf_open_w(int fd){
 #ifdef _WIN32
        setmode(fd, O_BINARY);
 #endif
-       rz = calloc(1, sizeof(RAZF));
+       if ((rz = calloc(1, RAZF_STRUCT_SIZE)) == NULL) {
+               fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
+               return NULL;
+       }
+       rz->stream = (z_stream *)rz + sizeof(RAZF);
+       rz->inbuf = rz->stream + sizeof(z_stream);
+       rz->outbuf = rz->inbuf + RZ_BUFFER_SIZE;
+       rz->index = rz->outbuf + RZ_BUFFER_SIZE;
+       rz->header = (gz_header *)rz->index + sizeof(ZBlockIndex);
+       rz->header->extra = (Bytef *)rz->header + sizeof(gz_header);
        rz->mode = 'w';
 #ifdef _USE_KNETFILE
-    rz->x.fpw = fd;
+       rz->x.fpw = fd;
 #else
        rz->filedes = fd;
 #endif
-       rz->stream = calloc(sizeof(z_stream), 1);
-       rz->inbuf  = malloc(RZ_BUFFER_SIZE);
-       rz->outbuf = malloc(RZ_BUFFER_SIZE);
-       rz->index = calloc(sizeof(ZBlockIndex), 1);
        deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
        rz->stream->avail_out = RZ_BUFFER_SIZE;
        rz->stream->next_out  = rz->outbuf;
-       rz->header = calloc(sizeof(gz_header), 1);
        rz->header->os    = 0x03; //Unix
        rz->header->text  = 0;
        rz->header->time  = 0;
-       rz->header->extra = malloc(7);
        strncpy((char*)rz->header->extra, "RAZF", 4);
        rz->header->extra[4] = 1; // obsolete field
        // block size = RZ_BLOCK_SIZE, Big-Endian
@@ -190,6 +243,7 @@ static RAZF* razf_open_w(int fd){
 
 static void _razf_write(RAZF* rz, const void *data, int size){
        int tout;
+       size_t count;
        rz->stream->avail_in = size;
        rz->stream->next_in  = (void*)data;
        while(1){
@@ -197,11 +251,15 @@ static void _razf_write(RAZF* rz, const void *data, int size){
                deflate(rz->stream, Z_NO_FLUSH);
                rz->out += tout - rz->stream->avail_out;
                if(rz->stream->avail_out) break;
+               count = RZ_BUFFER_SIZE - rz->stream->avail_out;
 #ifdef _USE_KNETFILE
-               write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+               if (write(rz->x.fpw, rz->outbuf, count) < 0) {
 #else
-               write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+               if (write(rz->filedes, rz->outbuf, count) < 0) {
 #endif
+                       fprintf(stderr, "[%s] failed to write output buffer.\n", __func__);
+                       abort();
+               }
                rz->stream->avail_out = RZ_BUFFER_SIZE;
                rz->stream->next_out  = rz->outbuf;
                if(rz->stream->avail_in == 0) break;
@@ -211,17 +269,22 @@ static void _razf_write(RAZF* rz, const void *data, int size){
 }
 
 static void razf_flush(RAZF *rz){
+       size_t count;
        uint32_t tout;
        if(rz->buf_len){
                _razf_write(rz, rz->inbuf, rz->buf_len);
                rz->buf_off = rz->buf_len = 0;
        }
        if(rz->stream->avail_out){
+               count = RZ_BUFFER_SIZE - rz->stream->avail_out;
 #ifdef _USE_KNETFILE    
-               write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+               if (write(rz->x.fpw, rz->outbuf, count) < 0) {
 #else        
-               write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+               if (write(rz->filedes, rz->outbuf, count) < 0) {
 #endif
+                       fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+                       abort();
+               }
                rz->stream->avail_out = RZ_BUFFER_SIZE;
                rz->stream->next_out  = rz->outbuf;
        }
@@ -230,11 +293,15 @@ static void razf_flush(RAZF *rz){
                deflate(rz->stream, Z_FULL_FLUSH);
                rz->out += tout - rz->stream->avail_out;
                if(rz->stream->avail_out == 0){
+                       count = RZ_BUFFER_SIZE - rz->stream->avail_out;
 #ifdef _USE_KNETFILE    
-                       write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+                       if (write(rz->x.fpw, rz->outbuf, count) < 0) {
 #else            
-                       write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+                       if (write(rz->filedes, rz->outbuf, count) < 0) {
 #endif
+                               fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+                               abort();
+                       }
                        rz->stream->avail_out = RZ_BUFFER_SIZE;
                        rz->stream->next_out  = rz->outbuf;
                } else break;
@@ -244,6 +311,7 @@ static void razf_flush(RAZF *rz){
 }
 
 static void razf_end_flush(RAZF *rz){
+       size_t count;
        uint32_t tout;
        if(rz->buf_len){
                _razf_write(rz, rz->inbuf, rz->buf_len);
@@ -254,11 +322,15 @@ static void razf_end_flush(RAZF *rz){
                deflate(rz->stream, Z_FINISH);
                rz->out += tout - rz->stream->avail_out;
                if(rz->stream->avail_out < RZ_BUFFER_SIZE){
+                       count = RZ_BUFFER_SIZE - rz->stream->avail_out;
 #ifdef _USE_KNETFILE        
-                       write(rz->x.fpw, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+                       if (write(rz->x.fpw, rz->outbuf, count) < 0) {
 #else            
-                       write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out);
+                       if (write(rz->filedes, rz->outbuf, count) < 0) {
 #endif
+                               fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__);
+                               abort();
+                       }
                        rz->stream->avail_out = RZ_BUFFER_SIZE;
                        rz->stream->next_out  = rz->outbuf;
                } else break;
@@ -355,7 +427,16 @@ static RAZF* razf_open_r(int fd, int _load_index){
        int n, is_be, ret;
        int64_t end;
        unsigned char c[] = "RAZF";
-       rz = calloc(1, sizeof(RAZF));
+       if ((rz = calloc(1, RAZF_STRUCT_SIZE)) == NULL) {
+               fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__);
+               return NULL;
+       }
+       rz->stream = (z_stream *)rz + sizeof(RAZF);
+       rz->inbuf = rz->stream + sizeof(z_stream);
+       rz->outbuf = rz->inbuf + RZ_BUFFER_SIZE;
+       rz->index = rz->outbuf + RZ_BUFFER_SIZE;
+       rz->header = (gz_header *)rz->index + sizeof(ZBlockIndex);
+       rz->header->extra = (Bytef *)rz->header + sizeof(gz_header);
        rz->mode = 'r';
 #ifdef _USE_KNETFILE
     rz->x.fpr = fp;
@@ -365,9 +446,6 @@ static RAZF* razf_open_r(int fd, int _load_index){
 #endif
        rz->filedes = fd;
 #endif
-       rz->stream = calloc(sizeof(z_stream), 1);
-       rz->inbuf  = malloc(RZ_BUFFER_SIZE);
-       rz->outbuf = malloc(RZ_BUFFER_SIZE);
        rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
 #ifdef _USE_KNETFILE
     n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);
@@ -801,24 +879,48 @@ void razf_close(RAZF *rz){
 #ifdef _USE_KNETFILE
                save_zindex(rz, rz->x.fpw);
                if(is_big_endian()){
-                       write(rz->x.fpw, &rz->in, sizeof(int64_t));
-                       write(rz->x.fpw, &rz->out, sizeof(int64_t));
+                       if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+                               abort();
+                       }
+                       if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+                               abort();
+                       }
                } else {
                        uint64_t v64 = byte_swap_8((uint64_t)rz->in);
-                       write(rz->x.fpw, &v64, sizeof(int64_t));
+                       if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+                               abort();
+                       }
                        v64 = byte_swap_8((uint64_t)rz->out);
-                       write(rz->x.fpw, &v64, sizeof(int64_t));
+                       if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+                               abort();
+                       }
                }
 #else
                save_zindex(rz, rz->filedes);
                if(is_big_endian()){
-                       write(rz->filedes, &rz->in, sizeof(int64_t));
-                       write(rz->filedes, &rz->out, sizeof(int64_t));
+                       if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+                               abort();
+                       }
+                       if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+                               abort();
+                       }
                } else {
                        uint64_t v64 = byte_swap_8((uint64_t)rz->in);
-                       write(rz->filedes, &v64, sizeof(int64_t));
+                       if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.in.\n", __func__);
+                               abort();
+                       }
                        v64 = byte_swap_8((uint64_t)rz->out);
-                       write(rz->filedes, &v64, sizeof(int64_t));
+                       if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) {
+                               fprintf(stderr, "[%s] failed to write rz.out.\n", __func__);
+                               abort();
+                       }
                }
 #endif
 #endif