summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-03-28 21:23:39 +0100
committerAidan MacDonald <amachronic@protonmail.com>2022-09-19 15:09:51 -0400
commitbcaa9465e99051bef52eeb2124b91dacaf2d7ee3 (patch)
treec1894cc8b57b8ad4c1c8aecb797aaa99f42a0dfb
parent6beebd75e779522bfc99ec570bb397041fdef49c (diff)
downloadrockbox-bcaa9465e99051bef52eeb2124b91dacaf2d7ee3.tar.gz
rockbox-bcaa9465e99051bef52eeb2124b91dacaf2d7ee3.zip
buflib: optimize find_block_before
Exiting the loop implies next_block == block, so remove that check. The check ret < block is false only if block is the first block, which can be checked before the loop, saving a few cycles in that case. Change-Id: Id493b5259a23a35a70b09dfe4bc4eacaf420760c
-rw-r--r--firmware/buflib.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/firmware/buflib.c b/firmware/buflib.c
index c6ec011653..4551fd8dca 100644
--- a/firmware/buflib.c
+++ b/firmware/buflib.c
@@ -664,23 +664,22 @@ find_block_before(struct buflib_context *ctx, union buflib_data* block,
664 union buflib_data *ret = ctx->buf_start, 664 union buflib_data *ret = ctx->buf_start,
665 *next_block = ret; 665 *next_block = ret;
666 666
667 /* no previous block */
668 if (next_block == block)
669 return NULL;
670
667 /* find the block that's before the current one */ 671 /* find the block that's before the current one */
668 while (next_block < block) 672 while (next_block != block)
669 { 673 {
670 ret = next_block; 674 ret = next_block;
671 next_block += abs(ret->val); 675 next_block += abs(ret->val);
672 } 676 }
673 677
674 /* If next_block == block, the above loop didn't go anywhere. If it did, 678 /* don't return it if the found block isn't free */
675 * and the block before this one is empty, that is the wanted one 679 if (is_free && ret->val >= 0)
676 */ 680 return NULL;
677 if (next_block == block && ret < block) 681
678 { 682 return ret;
679 if (is_free && ret->val >= 0) /* NULL if found block isn't free */
680 return NULL;
681 return ret;
682 }
683 return NULL;
684} 683}
685 684
686/* Free the buffer associated with handle_num. */ 685/* Free the buffer associated with handle_num. */