Branch: Tag:

2017-11-01

2017-11-01 11:18:29 by Arne Goedeke <el@laramies.com>

Stdio.Buffer: fixed growth strategy

Unless trim() was called manually or unless the buffer was empty,
the growth strategy would never reuse empty space at the beginning
of the buffer. Instead, it would always allocate more space.

The code now works as described in the comment. When at least half of
the buffer space in the beginning of the allocated region is unused,
the data is moved to the front before new memory is being allocated.

247:    */    if( LIKELY(!io->locked_move) )    { -  if( UNLIKELY((force && io->offset) || (io->offset > io->len)) ) +  if( UNLIKELY((force && io->offset) || (io_len(io) && io->offset > io->allocated / 2)) )    {    /* more than 50% of the buffer is available before the read pointer,    * and we can discard that data. Move the data to the beginning, making    * room for more data. -  +  * +  * NOTE: since the empty space at the beginning is bigger than half of the full +  * allocated space, src and dst do not overlap. We can therefore use memcpy().    */ -  memmove( io->buffer, io_read_pointer(io), io_len(io) ); +  memcpy( io->buffer, io_read_pointer(io), io_len(io) );    io->num_move++;    io->len -= io->offset;    io->offset = 0;