..............
This commit is contained in:
parent
ab03a7c2cc
commit
8207a82ffc
3 changed files with 36 additions and 26 deletions
8
driver.c
8
driver.c
|
@ -14,11 +14,13 @@ main(void)
|
||||||
rb_init(&rb, 10, malloc, sizeof(int));
|
rb_init(&rb, 10, malloc, sizeof(int));
|
||||||
|
|
||||||
int data[] = { 5, 6, 7, 8 };
|
int data[] = { 5, 6, 7, 8 };
|
||||||
printf("Address at first element: %p\n", &data[0]);
|
printf("Address at data first element: %p\n", &data[0]);
|
||||||
rb_push(&rb, data, 4, memcpy);
|
printf("Address at internal first element: %p\n", &rb.buffer);
|
||||||
|
// rb_push(&rb, data, 4, memcpy);
|
||||||
|
rb_push_back(&rb, data, memcpy);
|
||||||
|
|
||||||
// Print addresses of data
|
// Print addresses of data
|
||||||
for (int i = 0; i < 4; i++) {
|
for(int i = 0; i < 4; i++) {
|
||||||
printf("Address: %u\n", &data[i]);
|
printf("Address: %u\n", &data[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
44
ringbuf.c
44
ringbuf.c
|
@ -15,11 +15,12 @@ rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
|
||||||
rb->write_idx = 0;
|
rb->write_idx = 0;
|
||||||
rb->read_idx = 0;
|
rb->read_idx = 0;
|
||||||
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
|
||||||
|
rb->buffer_end = rb->buffer + (capacity * struct_size);
|
||||||
|
|
||||||
// Read from buffer at max position to force a segfault if theres an issue
|
// Read from buffer at max position to force a segfault if theres an issue
|
||||||
printf("Reading from buffer at position %d\n",
|
printf("Reading from buffer at position %d\n",
|
||||||
rb->capacity * rb->struct_size);
|
rb->capacity * rb->struct_size);
|
||||||
void *top = rb->buffer[rb->capacity * rb->struct_size];
|
void *top = rb->buffer + (rb->capacity * rb->struct_size);
|
||||||
printf("Buffer top successfully read at virtual address: %p\n", &top);
|
printf("Buffer top successfully read at virtual address: %p\n", &top);
|
||||||
|
|
||||||
printf("Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n",
|
printf("Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n",
|
||||||
|
@ -33,33 +34,34 @@ rb_destroy(struct RingBuf *rb, void(free)())
|
||||||
}
|
}
|
||||||
|
|
||||||
enum WriteResult
|
enum WriteResult
|
||||||
rb_push(struct RingBuf *rb, void *data, rb_size_t amount, MEMCPY_T memcpy_fn)
|
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
{
|
{
|
||||||
printf("\nWriting %d elements\n", amount);
|
// if(rb->count == rb->capacity){
|
||||||
|
// // handle error
|
||||||
|
// }
|
||||||
|
|
||||||
// We need to do this iteratively, since a memcpy of
|
memcpy_fn(rb->write_head, item, rb->size);
|
||||||
// multiple elements may overshoot the buffer.
|
|
||||||
for(int i = 0; i < amount; i++) {
|
|
||||||
void *src = data + i * rb->struct_size; // One element
|
|
||||||
void *dest = rb->buffer + (rb->write_idx + i) * rb->struct_size;
|
|
||||||
|
|
||||||
printf("Destination: %p\n", dest);
|
rb->write_head = (char *)rb->write_head + rb->size;
|
||||||
printf("Buffer: %p\n", rb->buffer);
|
|
||||||
|
|
||||||
printf("struct_size: %d\n", rb->struct_size);
|
if(rb->write_head == rb->buffer_end)
|
||||||
|
rb->write_head = rb->buffer;
|
||||||
|
|
||||||
// For each byte in the struct, copy it over
|
rb->count++;
|
||||||
for (int j = 0; j < rb->struct_size; j++) {
|
|
||||||
printf("Copying from %p to %p\n", src + j, dest + j);
|
|
||||||
printf("j: %d\n", j);
|
|
||||||
memcpy_fn(dest+j, src+j, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// memcpy_fn(dest, src, rb->struct_size);
|
|
||||||
}
|
|
||||||
return Ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// void rb_pop_front(struct RingBuf *rb, void *item)
|
||||||
|
// {
|
||||||
|
// if(cb->count == 0){
|
||||||
|
// // handle error
|
||||||
|
// }
|
||||||
|
// memcpy(item, cb->tail, cb->sz);
|
||||||
|
// cb->tail = (char*)cb->tail + cb->sz;
|
||||||
|
// if(cb->tail == cb->buffer_end)
|
||||||
|
// cb->tail = cb->buffer;
|
||||||
|
// cb->count--;
|
||||||
|
// }
|
||||||
|
|
||||||
void
|
void
|
||||||
rb_read(struct RingBuf *rb, void *dest, int amount)
|
rb_read(struct RingBuf *rb, void *dest, int amount)
|
||||||
{
|
{
|
||||||
|
|
10
ringbuf.h
10
ringbuf.h
|
@ -17,10 +17,13 @@ struct RingBuf {
|
||||||
rb_size_t struct_size; /* Size of the struct */
|
rb_size_t struct_size; /* Size of the struct */
|
||||||
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
|
rb_size_t capacity; /* The physical capacity of the entire ringbuf */
|
||||||
rb_size_t size; /* The current size of the ring buffer */
|
rb_size_t size; /* The current size of the ring buffer */
|
||||||
|
void *write_head; /* Address of the write head */
|
||||||
rb_size_t write_idx; /* The write head */
|
rb_size_t write_idx; /* The write head */
|
||||||
rb_size_t read_idx; /* THe read head */
|
rb_size_t read_idx; /* THe read head */
|
||||||
void **buffer; /* The actual data */
|
rb_size_t count; /* The number of elements in the buffer */
|
||||||
};
|
void *buffer; /* The actual data */
|
||||||
|
void *buffer_end; /* The end of the buffer */
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
enum WriteResult { CollisionError, Ok };
|
enum WriteResult { CollisionError, Ok };
|
||||||
|
|
||||||
|
@ -32,6 +35,9 @@ void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
|
||||||
enum WriteResult rb_push(struct RingBuf *rb, void *data, rb_size_t amount,
|
enum WriteResult rb_push(struct RingBuf *rb, void *data, rb_size_t amount,
|
||||||
void *(*memcpy)(void *, const void *, rb_size_t));
|
void *(*memcpy)(void *, const void *, rb_size_t));
|
||||||
|
|
||||||
|
enum WriteResult rb_push_back(struct RingBuf *rb, const void *item,
|
||||||
|
MEMCPY_T memcpy_fn);
|
||||||
|
|
||||||
/** Read data from the ring buffer */
|
/** Read data from the ring buffer */
|
||||||
void rb_read(struct RingBuf *rb, void *dest, int amount);
|
void rb_read(struct RingBuf *rb, void *dest, int amount);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue