Changeset 1560
- Timestamp:
- 04/20/10 14:06:31 (3 years ago)
- File:
-
- 1 edited
-
trunk/lib/ior-peek.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ior-peek.c
r1558 r1560 88 88 } 89 89 90 /* Read at least "len" bytes from the child io into the internal buffer, and return how many 91 bytes was actually read. 92 */ 93 static 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 90 129 static off_t peek_read(io_t *io, void *buffer, off_t len) 91 130 { … … 105 144 len -= ret; 106 145 } 146 107 147 /* Use the child reader to get the rest of the required data */ 108 148 if (len>0) { … … 110 150 assert(DATA(io)->length-DATA(io)->offset == 0); 111 151 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 */ 113 155 if (len % MIN_READ_SIZE == 0) { 114 156 bytes_read = DATA(io)->child->source->read( … … 124 166 } 125 167 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); 144 169 if (bytes_read < 1) { 170 /* Return if we have managed to get some data ok */ 145 171 if (ret > 0) 146 172 return ret; 173 /* Return the error upstream */ 147 174 return bytes_read; 148 175 } … … 150 177 len = len < bytes_read ? len : bytes_read; 151 178 memcpy(buffer, DATA(io)->buffer, len); 179 152 180 DATA(io)->offset = len; 153 181 bytes_read = len;
Note: See TracChangeset
for help on using the changeset viewer.
