Changeset 4624:7cdf88681b49 in roaraudio


Ignore:
Timestamp:
11/27/10 00:32:39 (11 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

updates for ringbuffer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/buffer.c

    r4615 r4624  
    235235 *buf = NULL; 
    236236 
    237  // currently we are limited to free running mode 
    238  if ( !free_running ) 
    239   return -1; 
    240  
    241237 if ( roar_buffer_new(&n, len) == -1 ) 
    242238  return -1; 
     
    576572 
    577573int roar_buffer_ring_write (struct roar_buffer *  buf, void * data, size_t * len) { 
     574 struct roar_buffer_ring * ring; 
     575 size_t havelen; 
     576 size_t done; 
     577 size_t tmp; 
     578 
    578579 if ( buf == NULL || len == NULL ) 
    579580  return -1; 
     
    611612  } 
    612613 } else { 
     614  ring = &(buf->meta.ring); 
     615  done = 0; 
     616 
     617  ROAR_DBG("roar_buffer_ring_write(buf=%p, data=%p, len=%p): write_pos=%u, read_pos=%u, user_len=%u", buf, data, len, (unsigned int)ring->write_pos, (unsigned int)ring->read_pos, (unsigned int)buf->user_len); 
     618 
     619  if ( ring->write_pos >= ring->read_pos ) { 
     620   havelen = buf->user_len - ring->write_pos; 
     621 
     622   if ( ring->read_pos == 0 ) 
     623    havelen--; 
     624 
     625   if ( havelen > *len ) 
     626    havelen = *len; 
     627 
     628   memcpy(buf->user_data+ring->write_pos, data, havelen); 
     629 
     630   done += havelen; 
     631   ring->write_pos += havelen; 
     632 
     633   if ( ring->write_pos == buf->user_len ) 
     634    ring->write_pos = 0; 
     635 
     636   if ( *len > done && ring->read_pos != 0 ) { 
     637    tmp = *len - done; 
     638    if ( roar_buffer_ring_write(buf, data+done, &tmp) == 0 ) { 
     639     done += tmp; 
     640    } 
     641   } 
     642 
     643   *len = done; 
     644   return 0; 
     645  } else { 
     646   // test for buffer-is-full: 
     647   if ( (ring->write_pos + 1) == ring->read_pos ) { 
     648    *len = 0; 
     649    return 0; 
     650   } 
     651 
     652   havelen = ring->read_pos - ring->write_pos - 1; 
     653 
     654   if ( havelen > *len ) 
     655    havelen = *len; 
     656 
     657   memcpy(buf->user_data+ring->write_pos, data, havelen); 
     658 
     659   *len = havelen; 
     660   return 0; 
     661  } 
     662 
    613663  return -1; 
    614664 } 
Note: See TracChangeset for help on using the changeset viewer.