diff options
Diffstat (limited to 'common/queue.c')
-rw-r--r-- | common/queue.c | 82 |
1 files changed, 30 insertions, 52 deletions
diff --git a/common/queue.c b/common/queue.c index 7b083233ad..8ad08c8438 100644 --- a/common/queue.c +++ b/common/queue.c @@ -8,14 +8,14 @@ #include "queue.h" #include "util.h" -#define CPRINTS(format, args...) cprints(CC_MOTION_SENSE, format, ## args) +#define CPRINTS(format, args...) cprints(CC_MOTION_SENSE, format, ##args) static void queue_action_null(struct queue_policy const *policy, size_t count) { } struct queue_policy const queue_policy_null = { - .add = queue_action_null, + .add = queue_action_null, .remove = queue_action_null, }; @@ -74,17 +74,17 @@ struct queue_chunk queue_get_write_chunk(struct queue const *q, size_t offset) { size_t head = q->state->head & q->buffer_units_mask; size_t tail = (q->state->tail + offset) & q->buffer_units_mask; - size_t last = (tail < head) ? head : /* Wrapped */ - q->buffer_units; /* Normal | Empty */ + size_t last = (tail < head) ? head : /* Wrapped */ + q->buffer_units; /* Normal | Empty */ /* Make sure that the offset doesn't exceed free space. */ if (queue_space(q) <= offset) - return ((struct queue_chunk) { + return ((struct queue_chunk){ .count = 0, .buffer = NULL, }); - return ((struct queue_chunk) { + return ((struct queue_chunk){ .count = last - tail, .buffer = q->buffer + (tail * q->unit_bytes), }); @@ -95,10 +95,10 @@ struct queue_chunk queue_get_read_chunk(struct queue const *q) size_t head = q->state->head & q->buffer_units_mask; size_t tail = q->state->tail & q->buffer_units_mask; size_t last = (queue_is_empty(q) ? head : /* Empty */ - ((head < tail) ? tail : /* Normal */ - q->buffer_units)); /* Wrapped | Full */ + ((head < tail) ? tail : /* Normal */ + q->buffer_units)); /* Wrapped | Full */ - return ((struct queue_chunk) { + return ((struct queue_chunk){ .count = (last - head), .buffer = q->buffer + (head * q->unit_bytes), }); @@ -134,7 +134,7 @@ size_t queue_add_unit(struct queue const *q, const void *src) return 0; if (q->unit_bytes == 1) - q->buffer[tail] = *((uint8_t *) src); + q->buffer[tail] = *((uint8_t *)src); else memcpy(q->buffer + tail * q->unit_bytes, src, q->unit_bytes); @@ -146,46 +146,33 @@ size_t queue_add_units(struct queue const *q, const void *src, size_t count) return queue_add_memcpy(q, src, count, memcpy); } -size_t queue_add_memcpy(struct queue const *q, - const void *src, - size_t count, - void *(*memcpy)(void *dest, - const void *src, - size_t n)) +size_t queue_add_memcpy(struct queue const *q, const void *src, size_t count, + void *(*memcpy)(void *dest, const void *src, size_t n)) { size_t transfer = MIN(count, queue_space(q)); - size_t tail = q->state->tail & q->buffer_units_mask; - size_t first = MIN(transfer, q->buffer_units - tail); + size_t tail = q->state->tail & q->buffer_units_mask; + size_t first = MIN(transfer, q->buffer_units - tail); - memcpy(q->buffer + tail * q->unit_bytes, - src, - first * q->unit_bytes); + memcpy(q->buffer + tail * q->unit_bytes, src, first * q->unit_bytes); if (first < transfer) memcpy(q->buffer, - ((uint8_t const *) src) + first * q->unit_bytes, + ((uint8_t const *)src) + first * q->unit_bytes, (transfer - first) * q->unit_bytes); return queue_advance_tail(q, transfer); } -static void queue_read_safe(struct queue const *q, - void *dest, - size_t head, - size_t transfer, - void *(*memcpy)(void *dest, - const void *src, - size_t n)) +static void +queue_read_safe(struct queue const *q, void *dest, size_t head, size_t transfer, + void *(*memcpy)(void *dest, const void *src, size_t n)) { size_t first = MIN(transfer, q->buffer_units - head); - memcpy(dest, - q->buffer + head * q->unit_bytes, - first * q->unit_bytes); + memcpy(dest, q->buffer + head * q->unit_bytes, first * q->unit_bytes); if (first < transfer) - memcpy(((uint8_t *) dest) + first * q->unit_bytes, - q->buffer, + memcpy(((uint8_t *)dest) + first * q->unit_bytes, q->buffer, (transfer - first) * q->unit_bytes); } @@ -197,7 +184,7 @@ size_t queue_remove_unit(struct queue const *q, void *dest) return 0; if (q->unit_bytes == 1) - *((uint8_t *) dest) = q->buffer[head]; + *((uint8_t *)dest) = q->buffer[head]; else memcpy(dest, q->buffer + head * q->unit_bytes, q->unit_bytes); @@ -209,39 +196,30 @@ size_t queue_remove_units(struct queue const *q, void *dest, size_t count) return queue_remove_memcpy(q, dest, count, memcpy); } -size_t queue_remove_memcpy(struct queue const *q, - void *dest, - size_t count, - void *(*memcpy)(void *dest, - const void *src, +size_t queue_remove_memcpy(struct queue const *q, void *dest, size_t count, + void *(*memcpy)(void *dest, const void *src, size_t n)) { size_t transfer = MIN(count, queue_count(q)); - size_t head = q->state->head & q->buffer_units_mask; + size_t head = q->state->head & q->buffer_units_mask; queue_read_safe(q, dest, head, transfer, memcpy); return queue_advance_head(q, transfer); } -size_t queue_peek_units(struct queue const *q, - void *dest, - size_t i, +size_t queue_peek_units(struct queue const *q, void *dest, size_t i, size_t count) { return queue_peek_memcpy(q, dest, i, count, memcpy); } -size_t queue_peek_memcpy(struct queue const *q, - void *dest, - size_t i, +size_t queue_peek_memcpy(struct queue const *q, void *dest, size_t i, size_t count, - void *(*memcpy)(void *dest, - const void *src, - size_t n)) + void *(*memcpy)(void *dest, const void *src, size_t n)) { size_t available = queue_count(q); - size_t transfer = MIN(count, available - i); + size_t transfer = MIN(count, available - i); if (i < available) { size_t head = (q->state->head + i) & q->buffer_units_mask; @@ -258,7 +236,7 @@ void queue_begin(struct queue const *q, struct queue_iterator *it) it->ptr = NULL; else it->ptr = q->buffer + (q->state->head & q->buffer_units_mask) * - q->unit_bytes; + q->unit_bytes; it->_state.offset = 0; it->_state.head = q->state->head; it->_state.tail = q->state->tail; |