Compare commits
2 commits
07bb60c8ae
...
c4f1ede6b0
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c4f1ede6b0 | ||
![]() |
df9cd367a3 |
4 changed files with 76 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
||||||
*.o
|
*.o
|
||||||
*.s
|
*.s
|
||||||
|
*.so
|
||||||
|
*.a
|
||||||
build
|
build
|
||||||
driver
|
driver
|
||||||
|
|
12
Makefile
12
Makefile
|
@ -39,8 +39,18 @@ driver: $(OBJECTS)
|
||||||
run: driver
|
run: driver
|
||||||
@./driver
|
@./driver
|
||||||
|
|
||||||
|
lib: $(OBJECTS)
|
||||||
|
@ar rcs librbuf.a ringbuf.o
|
||||||
|
|
||||||
|
dylib: $(OBJECTS)
|
||||||
|
@$(CC) $(CFLAGS) -fPIC -shared -o librbuf.so ringbuf.o
|
||||||
|
|
||||||
|
install:
|
||||||
|
@cp librbuf.a /usr/local/lib
|
||||||
|
@cp ringbuf.h /usr/local/include
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJECTS) $(ASMS) driver
|
rm -f $(OBJECTS) $(ASMS) driver librbuf.a librbuf.so
|
||||||
|
|
||||||
asm: $(ASMS) $(OBJECTS)
|
asm: $(ASMS) $(OBJECTS)
|
||||||
wc -l $(ASMS)
|
wc -l $(ASMS)
|
||||||
|
|
41
ringbuf.c
41
ringbuf.c
|
@ -63,6 +63,47 @@ rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
|
||||||
return WriteOk;
|
return WriteOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum WriteResult
|
||||||
|
rb_push_many(struct RingBuf *rb, const void *items, MEMCPY_T memcpy_fn,
|
||||||
|
rb_size_t n)
|
||||||
|
{
|
||||||
|
if(rb->count + n > rb->capacity)
|
||||||
|
return Full; // Perhaps rename to InsufficientSpace
|
||||||
|
|
||||||
|
// If the write head will move past the end of the buffer
|
||||||
|
// we need to to the write in two steps.
|
||||||
|
void *end = (char *)rb->write_head + rb->struct_size * n;
|
||||||
|
|
||||||
|
if(end > rb->buffer_end) {
|
||||||
|
|
||||||
|
// Calculate the number of items that can be written in the first chunk
|
||||||
|
rb_size_t first_chunk = (char *)rb->buffer_end - (char *)rb->write_head;
|
||||||
|
|
||||||
|
DEBUG_PRINT("Multi-chunk write. First chunk: %d\n", first_chunk);
|
||||||
|
|
||||||
|
// Write the first chunk
|
||||||
|
memcpy_fn(rb->write_head, items, rb->struct_size * first_chunk);
|
||||||
|
|
||||||
|
// Set the write head to the beginning of the buffer
|
||||||
|
rb->write_head = rb->buffer;
|
||||||
|
rb->count += first_chunk;
|
||||||
|
n -= first_chunk;
|
||||||
|
} else {
|
||||||
|
DEBUG_PRINT("Single-chunk write. No need to wrap around.%s\n", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG_PRINT("Writing %d items\n", n);
|
||||||
|
memcpy_fn(rb->write_head, items, rb->struct_size * n);
|
||||||
|
if(rb->write_head == rb->buffer_end)
|
||||||
|
rb->write_head = rb->buffer;
|
||||||
|
|
||||||
|
// Advance the write head
|
||||||
|
rb->write_head = (char *)rb->write_head + rb->struct_size * n;
|
||||||
|
rb->count+=n;
|
||||||
|
|
||||||
|
return WriteOk;
|
||||||
|
}
|
||||||
|
|
||||||
enum ReadResult
|
enum ReadResult
|
||||||
rb_pop_front(struct RingBuf *rb, void *item)
|
rb_pop_front(struct RingBuf *rb, void *item)
|
||||||
{
|
{
|
||||||
|
|
22
ringbuf.h
22
ringbuf.h
|
@ -49,6 +49,28 @@ void rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T alloc,
|
||||||
enum WriteResult rb_push_back(struct RingBuf *rb, const void *item,
|
enum WriteResult rb_push_back(struct RingBuf *rb, const void *item,
|
||||||
MEMCPY_T memcpy_fn);
|
MEMCPY_T memcpy_fn);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Insert multiple data to the ring buffer
|
||||||
|
*
|
||||||
|
* @details This function is more efficient than calling rb_push_back multiple
|
||||||
|
* times. It only advances the write head once, and attempts to write all the
|
||||||
|
* memory in one go.
|
||||||
|
*
|
||||||
|
* If n is greater than the capacity, it will return Full.
|
||||||
|
* If the full write will overflow, it will wrap around.
|
||||||
|
*
|
||||||
|
* If the buffer is full, it will return Full and not write
|
||||||
|
* anything.
|
||||||
|
*
|
||||||
|
* @param rb The ring buffer
|
||||||
|
* @param items The items to insert
|
||||||
|
* @param memcpy_fn The memcpy function
|
||||||
|
* @param n The number of items to insert
|
||||||
|
* @return WriteResult
|
||||||
|
*/
|
||||||
|
enum WriteResult rb_push_many(struct RingBuf *rb, const void *items,
|
||||||
|
MEMCPY_T memcpy_fn, rb_size_t n);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read data from the ring buffer
|
* @brief Read data from the ring buffer
|
||||||
* @param rb The ring buffer
|
* @param rb The ring buffer
|
||||||
|
|
Loading…
Reference in a new issue