Commit cda3078f authored by Titouan Soulard's avatar Titouan Soulard

common: automatically resize circular buffer

parent 9eb5b337
......@@ -3,13 +3,15 @@
struct CommonCBBuffer *common_circular_buffer_create(uint32_t len, bool is_infinite) {
struct CommonCBBuffer *buffer = (struct CommonCBBuffer *) malloc(sizeof(struct CommonCBBuffer));
if(!buffer) return NULL;
buffer->base_addr = malloc(len);
buffer->read_counter = 0;
buffer->write_counter = 0;
buffer->total_len = len;
buffer->readable_len = is_infinite ? -1 : 0;
return buffer;
return buffer->base_addr ? buffer : NULL;
}
void common_circular_buffer_write(struct CommonCBBuffer *buffer, void *bytes, uint32_t byte_count) {
......@@ -37,8 +39,14 @@ void common_circular_buffer_write(struct CommonCBBuffer *buffer, void *bytes, ui
buffer->readable_len += byte_count;
if(buffer->readable_len > buffer->total_len) {
printf("circular_buffer: overflow, increase buffer size\n");
buffer->total_len *= 2;
printf("circular_buffer: overflow, increasing buffer size to %u\n", buffer->total_len);
buffer->base_addr = realloc(buffer->base_addr, buffer->total_len);
// Reset the buffer completely to avoid garbage
buffer->readable_len = 0;
buffer->read_counter = 0;
buffer->write_counter = 0;
}
}
}
......
......@@ -123,9 +123,9 @@ int trx_rdma_start(TRXState *s, const TRXDriverParams2 *p) {
// Samples will be received (and sent) using a separate thread which manipulates a finite circular
// buffer. The size of the buffer depends on the latency of the network.
// XXX: this function should definitely return an error on failure
sdr_context->recv_buffer = common_circular_buffer_create(TRX_RDMA_BUFFER_SAMPLES * sizeof(TRXComplex), false);
sdr_context->send_buffer = common_circular_buffer_create(TRX_RDMA_BUFFER_SAMPLES * sizeof(TRXComplex), false);
if(!sdr_context->recv_buffer || !sdr_context->send_buffer) return -1;
pthread_mutex_init(&sdr_context->recv_buffer_mutex, NULL);
pthread_mutex_init(&sdr_context->send_buffer_mutex, NULL);
......
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