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 94fa85492f343e0ba5af92b6dbcfb59895a5a022..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];
@@ -135,10 +144,11 @@ static void load_zindex(RAZF *rz, int fd){
        int is_be;
        size_t count;
        if(!rz->load_index) return;
-       if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex));
        if(rz->index == NULL) {
-               fprintf(stderr, "[%s] failure to allocate index.\n", __func__);
-               abort();
+               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
@@ -196,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
@@ -414,10 +427,16 @@ static RAZF* razf_open_r(int fd, int _load_index){
        int n, is_be, ret;
        int64_t end;
        unsigned char c[] = "RAZF";
-       if ((rz = calloc(1, sizeof(RAZF))) == NULL) {
+       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;
@@ -427,24 +446,6 @@ static RAZF* razf_open_r(int fd, int _load_index){
 #endif
        rz->filedes = fd;
 #endif
-       if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) {
-               fprintf(stderr, "[%s] failure to allocate z_stream.\n", __func__);
-               free(rz);
-               return NULL;
-       }
-       if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
-               fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__);
-               free(rz->stream);
-               free(rz);
-               return NULL;
-       }
-       if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) {
-               fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__);
-               free(rz->inbuf);
-               free(rz->stream);
-               free(rz);
-               return NULL;
-       }
        rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL;
 #ifdef _USE_KNETFILE
     n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE);