Commit 370ef0bc authored by Joanne Hugé's avatar Joanne Hugé

wip

parent 4e72db49
//#define TEST
#ifdef TEST
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <getopt.h>
#include <immintrin.h>
#include <inttypes.h>
#include <limits.h>
#include <linux/if_packet.h>
#include <math.h>
#include <netdb.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <net/if.h>
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#endif
typedef struct {
volatile char * buffer;
volatile size_t * block_buffer;
char name[64];
size_t len;
size_t block_buf_len;
size_t min_block_size;
volatile int write_index;
volatile int read_index;
volatile int block_write_index;
volatile int block_read_index;
} ring_buffer_t;
static void init_rbuf(ring_buffer_t * rbuf, char * name, size_t len, size_t min_block_size) {
//log_debug("TRX_ECPRI", "Allocating %s with %d bytes\n", _name, len);
rbuf->len = len;
rbuf->block_buf_len = len / min_block_size;
rbuf->buffer = (char *) malloc(len);
rbuf->block_buffer = (size_t *) malloc(rbuf->block_buf_len * sizeof(size_t));
rbuf->min_block_size = min_block_size;
rbuf->write_index = 0;
rbuf->read_index = 0;
rbuf->block_write_index = 0;
rbuf->block_read_index = 0;
strcpy(rbuf->name, name);
}
static int rbuf_read_amount(ring_buffer_t * rbuf) {
return (rbuf->write_index + rbuf->len - rbuf->read_index) % rbuf->len;
}
static int rbuf_write_amount(ring_buffer_t * rbuf) {
// Don't write everything to avoid write index catching up to read index
// That we way we don't have to use locks
return ((rbuf->read_index + rbuf->len - rbuf->write_index - 1) % rbuf->len);
}
static volatile char * rbuf_write(ring_buffer_t * rbuf) {
return rbuf->buffer + rbuf->write_index;
}
static void rbuf_increment_write(ring_buffer_t * rbuf, size_t size) {
rbuf->write_index = (rbuf->write_index + size) % rbuf->len;
rbuf->block_buffer[rbuf->block_write_index] = size;
rbuf->block_write_index = (rbuf->block_write_index + 1) % rbuf->block_buf_len;
}
static volatile char * rbuf_read(ring_buffer_t * rbuf, size_t * size) {
volatile char * data = rbuf->buffer + rbuf->read_index;
*size = rbuf->block_buffer[rbuf->block_read_index];
rbuf->read_index = (rbuf->read_index + *size) % rbuf->len;
rbuf->block_read_index = (rbuf->block_read_index + 1) % rbuf->block_buf_len;
return data;
}
#ifdef TEST
static ring_buffer_t test_rbuf;
int main(int argc, char ** argv) {
char volatile * data;
size_t size;
int i, j;
init_rbuf(&test_rbuf, "TEST", 64, 4);
for(j = 0; j < 10; j++) {
data = rbuf_write(&test_rbuf);
strcpy(data, "hello");
rbuf_increment_write(&test_rbuf, 6);
data = rbuf_write(&test_rbuf);
strcpy(data, "world");
rbuf_increment_write(&test_rbuf, 6);
data = rbuf_read(&test_rbuf, &size);
for(i = 0; i < size; i++) {
printf("%c", data[i]);
}
printf("\n");
data = rbuf_read(&test_rbuf, &size);
for(i = 0; i < size; i++) {
printf("%c", data[i]);
}
printf("\n");
}
return 0;
}
#endif
#ifdef ARCHIVE
static void rbuf_update_write_index(ring_buffer_t * rbuf) {
rbuf->write_index = (rbuf->write_index + 1) % rbuf->buf_len;
}
static void rbuf_update_read_index(ring_buffer_t * rbuf) {
rbuf->read_index = (rbuf->read_index + 1) % rbuf->buf_len;
}
static int rbuf_read_amount(ring_buffer_t * rbuf) {
return (rbuf->write_index + rbuf->buf_len - rbuf->read_index) % rbuf->buf_len;
}
static int rbuf_write_amount(ring_buffer_t * rbuf) {
// Don't write everything to avoid write index catching up to read index
// That we way we don't have to use locks
return ((rbuf->read_index + rbuf->buf_len - rbuf->write_index - 1) % rbuf->buf_len);
}
static int rbuf_contiguous_copy(ring_buffer_t * rbuf1, ring_buffer_t * rbuf2, int n) {
int ret = n;
if(rbuf1) {
n = rbuf1->buf_len - rbuf1->read_index;
ret = n < ret ? n : ret;
}
if(rbuf2)
n = rbuf2->buf_len - rbuf2->write_index;
return n < ret ? n : ret;
}
#define RBUF_READ0(rbuf, type) (((type *) rbuf.buffer) + (rbuf.read_index * rbuf.len))
#define RBUF_WRITE0(rbuf, type) (((type *) rbuf.buffer) + (rbuf.write_index * rbuf.len))
#define RBUF_READ(rbuf, i, type) (((type *) rbuf.buffer) + (((rbuf.read_index + i) % rbuf.buf_len) * rbuf.len))
#define RBUF_WRITE(rbuf, i, type) (((type *) rbuf.buffer) + (((rbuf.write_index + i) % rbuf.buf_len) * rbuf.len))
#define RBUF_INIT(rbuf, _name, _buf_len, _len, type) do\
{\
log_debug("TRX_ECPRI", "Allocating %s with %d bytes\n", _name, (_buf_len * _len * sizeof(type)));\
rbuf.buffer = (type *) malloc(_buf_len * _len * sizeof(type));\
strcpy(rbuf.name, _name);\
rbuf.buf_len = _buf_len;\
rbuf.len = _len;\
rbuf.write_index = 0;\
rbuf.read_index = 0;\
} while(0)
#endif
This diff is collapsed.
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