Addressing sanity check
This commit is contained in:
parent
70aded6fee
commit
ab03a7c2cc
3 changed files with 54 additions and 10 deletions
19
driver.c
19
driver.c
|
@ -14,14 +14,23 @@ main(void)
|
|||
rb_init(&rb, 10, malloc, sizeof(int));
|
||||
|
||||
int data[] = { 5, 6, 7, 8 };
|
||||
rb_push(&rb, (void *)data, 4, memcpy);
|
||||
printf("Address at first element: %p\n", &data[0]);
|
||||
rb_push(&rb, data, 4, memcpy);
|
||||
|
||||
// Print addresses of data
|
||||
for (int i = 0; i < 4; i++) {
|
||||
printf("Address: %u\n", &data[i]);
|
||||
}
|
||||
|
||||
print_seq(data, 4);
|
||||
print_seq(rb.buffer, 4);
|
||||
|
||||
int dest[5];
|
||||
rb_read(&rb, (void *)dest, 5);
|
||||
// rb_read(&rb, (void *)dest, 5);
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
printf("Data: %d\n", dest[i]);
|
||||
}
|
||||
// for (int i = 0; i < 5; i++) {
|
||||
// printf("Data: %d\n", dest[i]);
|
||||
// }
|
||||
|
||||
rb_destroy(&rb, free);
|
||||
|
||||
|
|
39
ringbuf.c
39
ringbuf.c
|
@ -7,7 +7,8 @@
|
|||
#include "ringbuf.h"
|
||||
|
||||
void
|
||||
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn, rb_size_t struct_size)
|
||||
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
|
||||
rb_size_t struct_size)
|
||||
{
|
||||
rb->struct_size = struct_size;
|
||||
rb->capacity = capacity;
|
||||
|
@ -32,9 +33,30 @@ rb_destroy(struct RingBuf *rb, void(free)())
|
|||
}
|
||||
|
||||
enum WriteResult
|
||||
rb_push(struct RingBuf *rb, void *data[], rb_size_t amount, MEMCPY_T memcpy_fn)
|
||||
rb_push(struct RingBuf *rb, void *data, rb_size_t amount, MEMCPY_T memcpy_fn)
|
||||
{
|
||||
printf("\nWriting %d elements\n", amount);
|
||||
|
||||
// We need to do this iteratively, since a memcpy of
|
||||
// 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);
|
||||
printf("Buffer: %p\n", rb->buffer);
|
||||
|
||||
printf("struct_size: %d\n", rb->struct_size);
|
||||
|
||||
// For each byte in the struct, copy it over
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -42,5 +64,16 @@ void
|
|||
rb_read(struct RingBuf *rb, void *dest, int amount)
|
||||
{
|
||||
printf("\nReading %d elements\n", amount);
|
||||
for(int i = 0; i < amount; i++) { }
|
||||
for(int i = 0; i < amount; i++) {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
print_seq(void *begin, int amount)
|
||||
{
|
||||
printf("\nPrinting sequence\n");
|
||||
for(int i = 0; i < amount; i++) {
|
||||
int *val = (int *)(begin + 4 * i);
|
||||
printf("V: %d, A: %u\n", *val, val);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ typedef void *(*MEMCPY_T)(void *, const void *, rb_size_t);
|
|||
struct RingBuf {
|
||||
rb_size_t struct_size; /* Size of the struct */
|
||||
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 */
|
||||
rb_size_t write_idx; /* The write head */
|
||||
rb_size_t read_idx; /* THe read head */
|
||||
void **buffer; /* The actual data */
|
||||
|
@ -29,7 +29,7 @@ void rb_init(struct RingBuf *rb, rb_size_t capacity, void *(*alloc)(rb_size_t),
|
|||
rb_size_t struct_size);
|
||||
|
||||
/** Insert data to the ring buffer */
|
||||
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));
|
||||
|
||||
/** Read data from the ring buffer */
|
||||
|
@ -37,3 +37,5 @@ void rb_read(struct RingBuf *rb, void *dest, int amount);
|
|||
|
||||
/** Free the ring buffer */
|
||||
void rb_destroy(struct RingBuf *rb, void(free)());
|
||||
|
||||
void print_seq(void *begin, int amount);
|
||||
|
|
Loading…
Reference in a new issue