Commit 41a95732 authored by Joanne Hugé's avatar Joanne Hugé

wip: go back to fixed blocked size in rbufs

TODO: modify trx_ecpri_read and trx_ecpri_write (then all the rest)
parent 5b846e30
...@@ -37,95 +37,63 @@ ...@@ -37,95 +37,63 @@
typedef struct { typedef struct {
volatile char * buffer; volatile char * buffer;
volatile size_t * block_buffer;
char name[64]; char name[64];
size_t len; size_t buf_len;
size_t block_buf_len; size_t block_len;
size_t min_block_size;
volatile int write_index; volatile int write_index;
volatile int read_index; volatile int read_index;
volatile int block_write_index;
volatile int block_read_index;
} ring_buffer_t; } ring_buffer_t;
static void init_rbuf(ring_buffer_t * rbuf, char * name, size_t len, size_t min_block_size) { static void init_rbuf(ring_buffer_t * rbuf, char * name, size_t buf_len,
size_t block_len) {
//log_debug("TRX_ECPRI", "Allocating %s with %d bytes\n", _name, len); //log_debug("TRX_ECPRI", "Allocating %s with %d bytes\n", _name, len);
rbuf->len = len; if (!(buf_len % block_len))
rbuf->block_buf_len = len / min_block_size; log_exit("buf_len needs to be a multiple of block_len");
rbuf->buffer = (char *) malloc(len); rbuf->buf_len = buf_len;
rbuf->block_buffer = (size_t *) malloc(rbuf->block_buf_len * sizeof(size_t)); rbuf->block_len = block_len;
rbuf->min_block_size = min_block_size; rbuf->buffer = (char *) malloc(buf_len);
rbuf->write_index = 0; rbuf->write_index = 0;
rbuf->read_index = 0; rbuf->read_index = 0;
rbuf->block_write_index = 0;
rbuf->block_read_index = 0;
strcpy(rbuf->name, name); strcpy(rbuf->name, name);
} }
static int rbuf_read_amount(ring_buffer_t * rbuf) { static int rbuf_read_amount(ring_buffer_t * rbuf) {
return (rbuf->write_index + rbuf->len - rbuf->read_index) % rbuf->len; return (rbuf->write_index + rbuf->buf_len - rbuf->read_index) % rbuf->buf_len;
} }
static int rbuf_write_amount(ring_buffer_t * rbuf) { static int rbuf_write_amount(ring_buffer_t * rbuf) {
// Don't write everything to avoid write index catching up to read index // Don't write everything to avoid write index catching up to read index
// That we way we don't have to use locks // That we way we don't have to use locks
return ((rbuf->read_index + rbuf->len - rbuf->write_index - 1) % rbuf->len); return ((rbuf->read_index + rbuf->buf_len - rbuf->write_index - 1) % rbuf->buf_len);
} }
static volatile char * rbuf_write(ring_buffer_t * rbuf) { static volatile char * rbuf_write(ring_buffer_t * rbuf) {
return rbuf->buffer + rbuf->write_index; return rbuf->buffer + rbuf->write_index;
} }
static void rbuf_increment_write(ring_buffer_t * rbuf, size_t size, int block) { static void rbuf_increment_write(ring_buffer_t * rbuf, size_t size) {
rbuf->write_index = (rbuf->write_index + size) % rbuf->len; if(!(size % rbuf->block_len))
if(!block) log_exit("size needs to be a multiple of buffer block length");
return; rbuf->write_index = (rbuf->write_index + size) % rbuf->buf_len;
rbuf->block_buffer[rbuf->block_write_index] = size;
rbuf->block_write_index = (rbuf->block_write_index + 1) % rbuf->block_buf_len;
} }
static void rbuf_increment_read(ring_buffer_t * rbuf, size_t size) { static void rbuf_increment_read(ring_buffer_t * rbuf, size_t size) {
rbuf->read_index = (rbuf->read_index + size) % rbuf->len; if(!(size % rbuf->block_len))
log_exit("size needs to be a multiple of buffer block length");
rbuf->read_index = (rbuf->read_index + size) % rbuf->buf_len;
} }
static volatile char * rbuf_read(ring_buffer_t * rbuf, size_t * size) { static volatile char * rbuf_read(ring_buffer_t * rbuf, size_t * size) {
volatile char * data = rbuf->buffer + rbuf->read_index; if(!(size % rbuf->block_len))
*size = rbuf->block_buffer[rbuf->block_read_index]; log_exit("size needs to be a multiple of buffer block length");
rbuf->read_index = (rbuf->read_index + *size) % rbuf->len; return rbuf->buffer + rbuf->read_index;
rbuf->block_read_index = (rbuf->block_read_index + 1) % rbuf->block_buf_len;
return data;
} }
static int rbuf_contiguous_copy(ring_buffer_t * rbuf1, ring_buffer_t * rbuf2, int n) {
static size_t rbuf_contiguous_copy_single(ring_buffer_t * rbuf, int ret = n;
size_t block_size, size_t max_blocks) { if(rbuf1) {
size_t n_blocks; n = rbuf1->buf_len - rbuf1->read_index;
if(!rbuf) ret = n < ret ? n : ret;
return 0;
n_blocks = (rbuf->len - rbuf->read_index) / block_size;
if (!n_blocks) {
if (n_blocks > max_blocks)
return max_blocks;
return n_blocks;
}
/* Returns maximum contiguous size on which rbuf_src can be copied to rbuf_dst */
static size_t rbuf_contiguous_copy(ring_buffer_t * rbuf_src,
ring_buffer_t * rbuf_dst,
size_t block_size, size_t max_blocks) {
size_t n_blocks;
if(rbuf_src) {
max_size = rbuf_src->len - rbuf_src->read_index;
if(size > max_size) {
size = max_size;
}
else if (size < min_size) {
index = 0;
size =
}
size = max_size < size ? max_size : size;
} }
if(rbuf_dst) if(rbuf2)
n = rbuf_dst->len - rbuf_dst->write_index; n = rbuf2->buf_len - rbuf2->write_index;
return n < ret ? n : ret; return n < ret ? n : ret;
} }
#ifdef TEST #ifdef TEST
static ring_buffer_t test_rbuf; static ring_buffer_t test_rbuf;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment