diff options
author | Namyoon Woo <namyoon@chromium.org> | 2019-04-17 12:54:48 -0700 |
---|---|---|
committer | Vadim Bendebury <vbendeb@chromium.org> | 2019-09-21 19:11:22 -0700 |
commit | cccf90df56790e7cd09ddc6df826011cdd280789 (patch) | |
tree | 4efe3196cd744f6f5499e5551be09dfa2ee20419 | |
parent | 16529375314e51eafc1382d84700ab897a18e649 (diff) | |
download | chrome-ec-cccf90df56790e7cd09ddc6df826011cdd280789.tar.gz |
cr50: add buffer_units_mask member into struct queue (take two)
"q->buffer_units - 1" is performed many times to wrap head and/or
tail. It should be calculated once.
BUG=None
BRANCH=cr50
TEST=None
Signed-off-by: Namyoon Woo <namyoon@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1572444
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
(cherry picked from commit 8e8dd8a8e4a0556b67f2ceed6b0fe1547989d924)
Change-Id: Ieb7dbbeb292eed2c83f5c5d06122d85ed94a65cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1644291
Tested-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
Commit-Queue: Vadim Bendebury <vbendeb@chromium.org>
(cherry picked from commit 77fd02501cffcc9c0880892415b53d389b274b79)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1705712
(cherry picked from commit 88efafc7618c6575d12351469437cd2ef7cd2fe2)
-rw-r--r-- | common/queue.c | 18 | ||||
-rw-r--r-- | include/queue.h | 2 |
2 files changed, 11 insertions, 9 deletions
diff --git a/common/queue.c b/common/queue.c index 67934ca74e..584d0fbdfa 100644 --- a/common/queue.c +++ b/common/queue.c @@ -70,8 +70,8 @@ int queue_is_full(struct queue const *q) struct queue_chunk queue_get_write_chunk(struct queue const *q) { - size_t head = q->state->head & (q->buffer_units - 1); - size_t tail = q->state->tail & (q->buffer_units - 1); + 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_full(q) ? tail : /* Full */ ((tail < head) ? head : /* Wrapped */ q->buffer_units)); /* Normal | Empty */ @@ -84,8 +84,8 @@ struct queue_chunk queue_get_write_chunk(struct queue const *q) struct queue_chunk queue_get_read_chunk(struct queue const *q) { - size_t head = q->state->head & (q->buffer_units - 1); - size_t tail = q->state->tail & (q->buffer_units - 1); + 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 */ @@ -120,7 +120,7 @@ size_t queue_advance_tail(struct queue const *q, size_t count) size_t queue_add_unit(struct queue const *q, const void *src) { - size_t tail = q->state->tail & (q->buffer_units - 1); + size_t tail = q->state->tail & q->buffer_units_mask; if (queue_space(q) == 0) return 0; @@ -146,7 +146,7 @@ size_t queue_add_memcpy(struct queue const *q, size_t n)) { size_t transfer = MIN(count, queue_space(q)); - size_t tail = q->state->tail & (q->buffer_units - 1); + 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, @@ -183,7 +183,7 @@ static void queue_read_safe(struct queue const *q, size_t queue_remove_unit(struct queue const *q, void *dest) { - size_t head = q->state->head & (q->buffer_units - 1); + size_t head = q->state->head & q->buffer_units_mask; if (queue_count(q) == 0) return 0; @@ -209,7 +209,7 @@ size_t queue_remove_memcpy(struct queue const *q, size_t n)) { size_t transfer = MIN(count, queue_count(q)); - size_t head = q->state->head & (q->buffer_units - 1); + size_t head = q->state->head & q->buffer_units_mask; queue_read_safe(q, dest, head, transfer, memcpy); @@ -236,7 +236,7 @@ size_t queue_peek_memcpy(struct queue const *q, size_t transfer = MIN(count, available - i); if (i < available) { - size_t head = (q->state->head + i) & (q->buffer_units - 1); + size_t head = (q->state->head + i) & q->buffer_units_mask; queue_read_safe(q, dest, head, transfer, memcpy); } diff --git a/include/queue.h b/include/queue.h index ef7d001f47..2ac5c0966a 100644 --- a/include/queue.h +++ b/include/queue.h @@ -75,6 +75,7 @@ struct queue { struct queue_policy const *policy; size_t buffer_units; /* size of buffer (in units) */ + size_t buffer_units_mask; /* size of buffer (in units) - 1*/ size_t unit_bytes; /* size of unit (in byte) */ uint8_t *buffer; }; @@ -89,6 +90,7 @@ struct queue { .state = &((struct queue_state){}), \ .policy = &POLICY, \ .buffer_units = SIZE, \ + .buffer_units_mask = SIZE - 1, \ .unit_bytes = sizeof(TYPE), \ .buffer = (uint8_t *) &((TYPE[SIZE]){}), \ }) |