Ringbuffer: add null checks and more const
This commit is contained in:
parent
269a45839c
commit
3fb475dc70
1 changed files with 38 additions and 6 deletions
|
@ -4,7 +4,21 @@
|
||||||
#include "ringbuffer.h"
|
#include "ringbuffer.h"
|
||||||
#include "../memory.c"
|
#include "../memory.c"
|
||||||
|
|
||||||
struct ringbuffer* rbfr_create(const int capacity, void (*destroy_element)(void*)) {
|
#define ERROR(msg) \
|
||||||
|
do { \
|
||||||
|
/* TODO Do something error-ish */ \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#define ASSERT_NONNULL_ARG(argument) { \
|
||||||
|
if (argument == NULL) { \
|
||||||
|
ERROR(__func__ " got NULL for argument '" #argument "'"); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct ringbuffer* rbfr_create(const int capacity, void (*const destroy_element)(void*)) {
|
||||||
|
ASSERT_NONNULL_ARG(destroy_element);
|
||||||
|
|
||||||
struct ringbuffer* const this = alloc(sizeof (struct ringbuffer));
|
struct ringbuffer* const this = alloc(sizeof (struct ringbuffer));
|
||||||
|
|
||||||
this->buffer = alloc(capacity * sizeof (void*));
|
this->buffer = alloc(capacity * sizeof (void*));
|
||||||
|
@ -17,21 +31,31 @@ struct ringbuffer* rbfr_create(const int capacity, void (*destroy_element)(void*
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rbfr_destroy(struct ringbuffer* this) {
|
void rbfr_destroy(struct ringbuffer* const this) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
|
||||||
rbfr_clear(this);
|
rbfr_clear(this);
|
||||||
|
|
||||||
free(this->buffer);
|
free(this->buffer);
|
||||||
|
this->buffer = NULL;
|
||||||
|
|
||||||
free(this);
|
free(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rbfr_size(const struct ringbuffer* this) {
|
int rbfr_size(const struct ringbuffer* const this) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
|
||||||
return this->size;
|
return this->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int rbfr_capacity(const struct ringbuffer* this) {
|
int rbfr_capacity(const struct ringbuffer* this) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
return this->buffer_n;
|
return this->buffer_n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rbfr_enqueue(struct ringbuffer* this, void* element) {
|
void rbfr_enqueue(struct ringbuffer* this, void* const element) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
|
||||||
if (this->buffer_n == 0) {
|
if (this->buffer_n == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +73,10 @@ void rbfr_enqueue(struct ringbuffer* this, void* element) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rbfr_peek(const struct ringbuffer* this, void** element) {
|
bool rbfr_peek(const struct ringbuffer *const this, void** element) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
ASSERT_NONNULL_ARG(element);
|
||||||
|
|
||||||
if (this->size == 0) {
|
if (this->size == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +84,10 @@ bool rbfr_peek(const struct ringbuffer* this, void** element) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rbfr_dequeue(struct ringbuffer* this, void** element) {
|
bool rbfr_dequeue(struct ringbuffer *const this, void** element) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
ASSERT_NONNULL_ARG(element);
|
||||||
|
|
||||||
if (!rbfr_peek(this, element)) {
|
if (!rbfr_peek(this, element)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -70,6 +100,8 @@ bool rbfr_dequeue(struct ringbuffer* this, void** element) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void rbfr_clear(struct ringbuffer* this) {
|
void rbfr_clear(struct ringbuffer* this) {
|
||||||
|
ASSERT_NONNULL_ARG(this);
|
||||||
|
|
||||||
void* element;
|
void* element;
|
||||||
while (rbfr_dequeue(this, &element)) {
|
while (rbfr_dequeue(this, &element)) {
|
||||||
this->destroy_element(element);
|
this->destroy_element(element);
|
||||||
|
|
Loading…
Reference in a new issue