Changeset 1560


Ignore:
Timestamp:
04/20/10 14:06:31 (3 years ago)
Author:
perry
Message:

Clean up peeking ahead of files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/ior-peek.c

    r1558 r1560  
    8888} 
    8989 
     90/* Read at least "len" bytes from the child io into the internal buffer, and return how many 
     91   bytes was actually read. 
     92 */ 
     93static off_t refill_buffer(io_t *io, off_t len) 
     94{ 
     95        off_t bytes_read; 
     96        assert(DATA(io)->length - DATA(io)->offset == 0); 
     97        /* Select the largest of "len", PEEK_SIZE and the current peek buffer size 
     98         * and then round up to the nearest multiple of MIN_READ_SIZE  
     99         */ 
     100        bytes_read = len < PEEK_SIZE ? PEEK_SIZE : len; 
     101        bytes_read = bytes_read < DATA(io)->length ? DATA(io)->length : bytes_read; 
     102        bytes_read += MIN_READ_SIZE - (bytes_read % MIN_READ_SIZE); 
     103        /* Is the current buffer big enough? */ 
     104        if (DATA(io)->length < bytes_read) { 
     105                if (DATA(io)->buffer) 
     106                        free(DATA(io)->buffer); 
     107                DATA(io)->length = bytes_read; 
     108                DATA(io)->offset = 0; 
     109                DATA(io)->buffer = malloc(DATA(io)->length); 
     110        } 
     111        else 
     112                DATA(io)->length = bytes_read; 
     113 
     114        /* Now actually attempt to read that many bytes */ 
     115        bytes_read = DATA(io)->child->source->read(      
     116                        DATA(io)->child, DATA(io)->buffer, bytes_read); 
     117 
     118        DATA(io)->offset = 0; 
     119        DATA(io)->length = bytes_read; 
     120 
     121        /* Error? */ 
     122        if (bytes_read < 1) 
     123                return bytes_read; 
     124 
     125        return bytes_read; 
     126         
     127} 
     128 
    90129static off_t peek_read(io_t *io, void *buffer, off_t len) 
    91130{ 
     
    105144                len -= ret; 
    106145        } 
     146 
    107147        /* Use the child reader to get the rest of the required data */ 
    108148        if (len>0) { 
     
    110150                assert(DATA(io)->length-DATA(io)->offset == 0); 
    111151                off_t bytes_read; 
    112                 /* If they're reading a block size, use that */ 
     152                /* If they're reading exactly a block size, just use that, no point in malloc'ing  
     153                 * and memcpy()ing needlessly  
     154                 */ 
    113155                if (len % MIN_READ_SIZE  == 0) { 
    114156                        bytes_read = DATA(io)->child->source->read( 
     
    124166                } 
    125167                else { 
    126                         /* Select the largest of "len", PEEK_SIZE and the current peek buffer size 
    127                          * and then round up to the nearest multiple of MIN_READ_SIZE  
    128                          */ 
    129                         bytes_read = len < PEEK_SIZE ? PEEK_SIZE : len; 
    130                         bytes_read = bytes_read < DATA(io)->length ? DATA(io)->length : bytes_read; 
    131                         bytes_read += MIN_READ_SIZE - (bytes_read % MIN_READ_SIZE); 
    132                         /* Is the current buffer big enough? */ 
    133                         if (DATA(io)->length < bytes_read) { 
    134                                 if (DATA(io)->buffer) 
    135                                         free(DATA(io)->buffer); 
    136                                 DATA(io)->length = bytes_read; 
    137                                 DATA(io)->offset = 0; 
    138                                 DATA(io)->buffer = malloc(DATA(io)->length); 
    139                         } 
    140                         /* Now actually attempt to read that many bytes */ 
    141                         bytes_read = DATA(io)->child->source->read(      
    142                                         DATA(io)->child, DATA(io)->buffer, bytes_read); 
    143                         /* Error? */ 
     168                        bytes_read = refill_buffer(io, len); 
    144169                        if (bytes_read < 1) { 
     170                                /* Return if we have managed to get some data ok */ 
    145171                                if (ret > 0) 
    146172                                        return ret; 
     173                                /* Return the error upstream */ 
    147174                                return bytes_read; 
    148175                        } 
     
    150177                        len = len < bytes_read ? len : bytes_read; 
    151178                        memcpy(buffer, DATA(io)->buffer, len); 
     179 
    152180                        DATA(io)->offset = len; 
    153181                        bytes_read = len; 
Note: See TracChangeset for help on using the changeset viewer.