Changeset 924b0a0 for lib


Ignore:
Timestamp:
05/18/10 13:46:30 (11 years ago)
Author:
Perry Lorier <perry@…>
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
Message:

Enforce alignment constraints

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/ior-peek.c

    rbf7e954 r924b0a0  
    4040#include <string.h>
    4141#include <assert.h>
     42#include <stddef.h>
    4243
    4344/* Libtrace IO module implementing a peeking reader.
     
    107108                DATA(io)->length = bytes_read;
    108109                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
    109118                DATA(io)->buffer = malloc(DATA(io)->length);
     119#endif
    110120        }
    111121        else
    112122                DATA(io)->length = bytes_read;
     123
     124        assert(DATA(io)->buffer);
    113125
    114126        /* Now actually attempt to read that many bytes */
     
    151163                off_t bytes_read;
    152164                /* 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.
    154167                 */
    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);
    156170                        bytes_read = DATA(io)->child->source->read(
    157171                                        DATA(io)->child, buffer, len);
     
    196210}
    197211
     212static 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
    198229
    199230static off_t peek_peek(io_t *io, void *buffer, off_t len)
     
    207238                /* Round the read_amount up to the nearest MB */
    208239                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);
    210242                /* Use the child reader to read more data into our managed
    211243                 * buffer */
Note: See TracChangeset for help on using the changeset viewer.