Changeset 924b0a0
- Timestamp:
- 05/18/10 13:46:30 (12 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- 270fb67
- Parents:
- d5a42c2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/ior-peek.c
rbf7e954 r924b0a0 40 40 #include <string.h> 41 41 #include <assert.h> 42 #include <stddef.h> 42 43 43 44 /* Libtrace IO module implementing a peeking reader. … … 107 108 DATA(io)->length = bytes_read; 108 109 DATA(io)->offset = 0; 110 #ifdef _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 111 /* We need to do this as read() of O_DIRECT might happen into this buffer. 112 * The docs suggest 512 bytes is all we need to align to, but I'm suspicious 113 * I expect disks with 4k blocks will arrive soon, and thus 4k is the minimum I'm 114 * willing to live with. 115 */ 116 posix_memalign(&DATA(io)->buffer, 4096, DATA(io)->length); 117 #else 109 118 DATA(io)->buffer = malloc(DATA(io)->length); 119 #endif 110 120 } 111 121 else 112 122 DATA(io)->length = bytes_read; 123 124 assert(DATA(io)->buffer); 113 125 114 126 /* Now actually attempt to read that many bytes */ … … 151 163 off_t bytes_read; 152 164 /* If they're reading exactly a block size, just use that, no point in malloc'ing 153 * and memcpy()ing needlessly 165 * and memcpy()ing needlessly. However, if the buffer isn't aligned, we need to 166 * pass on an aligning buffer, skip this and do it into our own aligned buffer. 154 167 */ 155 if (len % MIN_READ_SIZE == 0) { 168 if ((len % MIN_READ_SIZE == 0) && ((ptrdiff_t)buffer % 4096)==0) { 169 assert(((ptrdiff_t)buffer % 4096) == 0); 156 170 bytes_read = DATA(io)->child->source->read( 157 171 DATA(io)->child, buffer, len); … … 196 210 } 197 211 212 static void *alignedrealloc(void *old, size_t oldsize, size_t size) 213 { 214 #if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 215 void *new; 216 /* Shortcut resizing */ 217 if (size < oldsize) 218 return old; 219 posix_memalign(&new, 4096, size); 220 assert(oldsize<size); 221 memcpy(new,old,oldsize); 222 free(old); 223 return new; 224 #else 225 return realloc(old,size); 226 #endif 227 } 228 198 229 199 230 static off_t peek_peek(io_t *io, void *buffer, off_t len) … … 207 238 /* Round the read_amount up to the nearest MB */ 208 239 read_amount += PEEK_SIZE - ((DATA(io)->length + read_amount) % PEEK_SIZE); 209 DATA(io)->buffer = realloc(DATA(io)->buffer, DATA(io)->length + read_amount); 240 DATA(io)->buffer = alignedrealloc(DATA(io)->buffer, DATA(io)->length, 241 DATA(io)->length + read_amount); 210 242 /* Use the child reader to read more data into our managed 211 243 * buffer */
Note: See TracChangeset
for help on using the changeset viewer.